github.com/hellofresh/janus@v0.0.0-20230925145208-ce8de8183c67/cmd/server.go (about) 1 package cmd 2 3 import ( 4 "context" 5 "fmt" 6 7 "github.com/hellofresh/janus/pkg/api" 8 "github.com/hellofresh/janus/pkg/server" 9 log "github.com/sirupsen/logrus" 10 "github.com/spf13/cobra" 11 12 // this is needed to call the init function on each plugin 13 _ "github.com/hellofresh/janus/pkg/plugin/basic" 14 _ "github.com/hellofresh/janus/pkg/plugin/bodylmt" 15 _ "github.com/hellofresh/janus/pkg/plugin/cb" 16 _ "github.com/hellofresh/janus/pkg/plugin/compression" 17 _ "github.com/hellofresh/janus/pkg/plugin/cors" 18 _ "github.com/hellofresh/janus/pkg/plugin/oauth2" 19 _ "github.com/hellofresh/janus/pkg/plugin/organization" 20 _ "github.com/hellofresh/janus/pkg/plugin/rate" 21 _ "github.com/hellofresh/janus/pkg/plugin/requesttransformer" 22 _ "github.com/hellofresh/janus/pkg/plugin/responsetransformer" 23 _ "github.com/hellofresh/janus/pkg/plugin/retry" 24 25 // dynamically registered auth providers 26 _ "github.com/hellofresh/janus/pkg/jwt/basic" 27 _ "github.com/hellofresh/janus/pkg/jwt/github" 28 ) 29 30 // ServerStartOptions are the command flags 31 type ServerStartOptions struct { 32 profilingEnabled bool 33 profilingPublic bool 34 } 35 36 // NewServerStartCmd creates a new http server command 37 func NewServerStartCmd(ctx context.Context, version string) *cobra.Command { 38 opts := &ServerStartOptions{} 39 40 cmd := &cobra.Command{ 41 Use: "start", 42 Short: "Starts a Janus web server", 43 RunE: func(cmd *cobra.Command, args []string) error { 44 return RunServerStart(ctx, opts, version) 45 }, 46 } 47 48 cmd.PersistentFlags().BoolVarP(&opts.profilingEnabled, "profiling-enabled", "", false, "Enable profiler, will be available on API port at /debug/pprof path") 49 cmd.PersistentFlags().BoolVarP(&opts.profilingPublic, "profiling-public", "", false, "Allow accessing profiler endpoint w/out authentication") 50 51 return cmd 52 } 53 54 // RunServerStart is the run command to start Janus 55 func RunServerStart(ctx context.Context, opts *ServerStartOptions, version string) error { 56 // all the logging configurations are initialised in initLog() later, 57 // but we try to initialise Writer (STDIN/STDERR/etc.) as early as possible manually 58 // to avoid loosing logs in systems heavily relying on them (e.g. running in docker) 59 initLogWriterEarly() 60 61 log.WithField("version", version).Info("Janus starting...") 62 63 initConfig() 64 initLog() 65 initStatsClient() 66 initStatsExporter() 67 initTracingExporter() 68 69 defer statsClient.Close() 70 defer globalConfig.Log.Flush() 71 72 repo, err := api.BuildRepository(globalConfig.Database.DSN, globalConfig.Cluster.UpdateFrequency) 73 if err != nil { 74 return fmt.Errorf("could not build a repository for the database: %w", err) 75 } 76 defer repo.Close() 77 78 svr := server.New( 79 server.WithGlobalConfig(globalConfig), 80 server.WithMetricsClient(statsClient), 81 server.WithProvider(repo), 82 server.WithProfiler(opts.profilingEnabled, opts.profilingPublic), 83 ) 84 85 ctx = ContextWithSignal(ctx) 86 svr.StartWithContext(ctx) 87 defer svr.Close() 88 89 svr.Wait() 90 log.Info("Shutting down") 91 92 return nil 93 }