package main import ( "context" "errors" "fmt" "log/slog" "net/http" "os" "git.t-juice.club/torjus/ministream/server" "github.com/urfave/cli/v2" ) const Version = "v0.1.1" type ctxKey string const ( ctxKeyConfig ctxKey = "config" ) func main() { app := cli.App{ Name: "ministream", Usage: "Small livestreaming platform.", Commands: []*cli.Command{ { Name: "serve", Usage: "Start livestreaming server.", Before: loadConfig, Action: func(c *cli.Context) error { cfg := configFromCtx(c.Context) srv := server.NewServer(cfg) srv.Addr = cfg.HTTP.ListenAddr slog.Info("Starting HTTP-server", "addr", srv.Addr, "cfg", cfg) if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { return err } return nil }, }, }, Version: Version, } err := app.RunContext(context.Background(), os.Args) if err != nil { fmt.Println(err) } } func loadConfig(c *cli.Context) error { cfg, err := server.ConfigFromDefault() if err != nil { return err } c.Context = context.WithValue(c.Context, ctxKeyConfig, cfg) return nil } func configFromCtx(ctx context.Context) *server.Config { value := ctx.Value(ctxKeyConfig) if value == nil { panic("unable to load config") } config, ok := value.(*server.Config) if !ok { panic("config type assertion failed") } return config }