Skip to content

Commit 63e7b0e

Browse files
authored
Merge pull request #11 from owenthereal/launch
Better management of launchd sockets
2 parents fc87b70 + b62fac2 commit 63e7b0e

File tree

3 files changed

+22
-19
lines changed

3 files changed

+22
-19
lines changed

cmd/candy/cmd/launch_darwin.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package cmd
44

55
import (
6+
"context"
67
"fmt"
78
"os"
89
"path/filepath"
@@ -49,6 +50,7 @@ func launchRunE(c *cobra.Command, args []string) error {
4950
}
5051

5152
for _, proxy := range proxies {
53+
proxy := proxy
5254
g.Add(func() error {
5355
return proxy.Run()
5456
}, func(err error) {
@@ -58,9 +60,11 @@ func launchRunE(c *cobra.Command, args []string) error {
5860
}
5961

6062
{
63+
ctx, cancel := context.WithCancel(context.Background())
6164
g.Add(func() error {
62-
return startServer(c)
65+
return startServer(c, ctx)
6366
}, func(err error) {
67+
cancel()
6468
})
6569
}
6670

cmd/candy/cmd/root.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,34 +25,33 @@ var (
2525
)
2626

2727
func init() {
28-
var err error
29-
homeDir, err = userHomeDir()
30-
if err != nil {
31-
candy.Log().Fatal("error getting user home directory", zap.Error(err))
32-
}
33-
34-
rootCmd.PersistentFlags().StringVar(&flagRootCfgFile, "config", filepath.Join(homeDir, ".candyconfig"), "Config file")
28+
rootCmd.PersistentFlags().StringVar(&flagRootCfgFile, "config", filepath.Join(userHomeDir(), ".candyconfig"), "Config file")
3529
}
3630

37-
func userHomeDir() (string, error) {
31+
func userHomeDir() string {
32+
if homeDir != "" {
33+
return homeDir
34+
}
35+
3836
var (
3937
sudo = os.Getenv("SUDO_USER")
4038
euid = os.Geteuid()
39+
err error
4140
)
4241

4342
if sudo != "" && euid == 0 {
4443
u, err := user.Lookup(sudo)
4544
if err != nil {
46-
return "", nil
45+
candy.Log().Fatal("error looking up sudo user", zap.String("user", sudo), zap.Error(err))
4746
}
4847

49-
return u.HomeDir, nil
48+
return u.HomeDir
5049
}
5150

52-
homeDir, err := os.UserHomeDir()
51+
homeDir, err = os.UserHomeDir()
5352
if err != nil {
54-
return "", err
53+
candy.Log().Fatal("error getting user home directory", zap.Error(err))
5554
}
5655

57-
return homeDir, nil
56+
return homeDir
5857
}

cmd/candy/cmd/run.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func init() {
3232
}
3333

3434
func addServerFlags(cmd *cobra.Command) {
35-
cmd.Flags().String("host-root", filepath.Join(homeDir, ".candy"), "Path to the directory containing applications that will be served by Candy")
35+
cmd.Flags().String("host-root", filepath.Join(userHomeDir(), ".candy"), "Path to the directory containing applications that will be served by Candy")
3636
cmd.Flags().StringSlice("domain", defaultDomains, "The top-level domains for which Candy will respond to DNS queries")
3737
cmd.Flags().String("http-addr", ":28080", "The Proxy server HTTP address")
3838
cmd.Flags().String("https-addr", ":28443", "The Proxy server HTTPS address")
@@ -42,10 +42,10 @@ func addServerFlags(cmd *cobra.Command) {
4242
}
4343

4444
func runRunE(c *cobra.Command, args []string) error {
45-
return startServer(c)
45+
return startServer(c, context.Background())
4646
}
4747

48-
func startServer(c *cobra.Command) error {
48+
func startServer(c *cobra.Command, ctx context.Context) error {
4949
var cfg server.Config
5050
if err := candy.LoadConfig(
5151
flagRootCfgFile,
@@ -66,10 +66,10 @@ func startServer(c *cobra.Command) error {
6666
candy.Log().Info("using config", zap.Any("cfg", cfg))
6767

6868
if err := os.MkdirAll(cfg.HostRoot, 0o0755); err != nil {
69-
return fmt.Errorf("failed to create host directory: %w", err)
69+
return fmt.Errorf("failed to create host directory %s: %w", cfg.HostRoot, err)
7070
}
7171

7272
svr := server.New(cfg)
7373

74-
return svr.Run(context.Background())
74+
return svr.Run(ctx)
7575
}

0 commit comments

Comments
 (0)