Skip to content

Commit

Permalink
Merge pull request #11 from owenthereal/launch
Browse files Browse the repository at this point in the history
Better management of launchd sockets
  • Loading branch information
owenthereal authored Feb 3, 2021
2 parents fc87b70 + b62fac2 commit 63e7b0e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
6 changes: 5 additions & 1 deletion cmd/candy/cmd/launch_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package cmd

import (
"context"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -49,6 +50,7 @@ func launchRunE(c *cobra.Command, args []string) error {
}

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

{
ctx, cancel := context.WithCancel(context.Background())
g.Add(func() error {
return startServer(c)
return startServer(c, ctx)
}, func(err error) {
cancel()
})
}

Expand Down
25 changes: 12 additions & 13 deletions cmd/candy/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,33 @@ var (
)

func init() {
var err error
homeDir, err = userHomeDir()
if err != nil {
candy.Log().Fatal("error getting user home directory", zap.Error(err))
}

rootCmd.PersistentFlags().StringVar(&flagRootCfgFile, "config", filepath.Join(homeDir, ".candyconfig"), "Config file")
rootCmd.PersistentFlags().StringVar(&flagRootCfgFile, "config", filepath.Join(userHomeDir(), ".candyconfig"), "Config file")
}

func userHomeDir() (string, error) {
func userHomeDir() string {
if homeDir != "" {
return homeDir
}

var (
sudo = os.Getenv("SUDO_USER")
euid = os.Geteuid()
err error
)

if sudo != "" && euid == 0 {
u, err := user.Lookup(sudo)
if err != nil {
return "", nil
candy.Log().Fatal("error looking up sudo user", zap.String("user", sudo), zap.Error(err))
}

return u.HomeDir, nil
return u.HomeDir
}

homeDir, err := os.UserHomeDir()
homeDir, err = os.UserHomeDir()
if err != nil {
return "", err
candy.Log().Fatal("error getting user home directory", zap.Error(err))
}

return homeDir, nil
return homeDir
}
10 changes: 5 additions & 5 deletions cmd/candy/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func init() {
}

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

func runRunE(c *cobra.Command, args []string) error {
return startServer(c)
return startServer(c, context.Background())
}

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

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

svr := server.New(cfg)

return svr.Run(context.Background())
return svr.Run(ctx)
}

0 comments on commit 63e7b0e

Please sign in to comment.