-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathrun.go
More file actions
64 lines (50 loc) · 1.87 KB
/
run.go
File metadata and controls
64 lines (50 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package cmd
import (
"log/slog"
"os"
"os/signal"
"syscall"
"github.com/spf13/cobra"
"github.com/basecamp/kamal-proxy/internal/server"
)
type runCommand struct {
cmd *cobra.Command
debugLogsEnabled bool
}
func newRunCommand() *runCommand {
runCommand := &runCommand{}
runCommand.cmd = &cobra.Command{
Use: "run",
Short: "Run the server",
RunE: runCommand.run,
}
runCommand.cmd.Flags().BoolVar(&runCommand.debugLogsEnabled, "debug", getEnvBool("DEBUG", false), "Include debugging logs")
runCommand.cmd.Flags().IntVar(&globalConfig.HttpPort, "http-port", getEnvInt("HTTP_PORT", server.DefaultHttpPort), "Port to serve HTTP traffic on")
runCommand.cmd.Flags().IntVar(&globalConfig.HttpsPort, "https-port", getEnvInt("HTTPS_PORT", server.DefaultHttpsPort), "Port to serve HTTPS traffic on")
runCommand.cmd.Flags().IntVar(&globalConfig.MetricsPort, "metrics-port", getEnvInt("METRICS_PORT", 0), "Publish metrics on the specified port (default zero to disable)")
runCommand.cmd.Flags().BoolVar(&globalConfig.HTTP3Enabled, "http3", false, "Enable HTTP/3")
runCommand.cmd.Flags().StringVar(&globalConfig.DockerSocketPath, "docker-socket", getEnvString("DOCKER_SOCKET", server.DefaultDockerSocketPath), "Path to Docker socket")
return runCommand
}
func (c *runCommand) run(cmd *cobra.Command, args []string) error {
c.setLogger()
router := server.NewRouter(globalConfig.StatePath(), globalConfig.DockerSocketPath)
router.RestoreLastSavedState()
s := server.NewServer(&globalConfig, router)
err := s.Start()
if err != nil {
return err
}
defer s.Stop()
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGTERM, syscall.SIGINT)
<-ch
return nil
}
func (c *runCommand) setLogger() {
level := slog.LevelInfo
if c.debugLogsEnabled {
level = slog.LevelDebug
}
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: level})))
}