diff --git a/http/server.go b/http/server.go index c352b8f..3656dae 100644 --- a/http/server.go +++ b/http/server.go @@ -9,6 +9,16 @@ import ( "go.uber.org/fx" ) +func WithHttpFactory(addr string) fx.Option { + return fx.Provide( + newHttpServerFactory(addr), + fx.Annotate( + newServeMux, + fx.ParamTags(`group:"routes"`), + ), + ) +} + var WithHttp = fx.Provide( newHttpServer, fx.Annotate( @@ -17,6 +27,31 @@ var WithHttp = fx.Provide( ), ) +func newHttpServerFactory(addr string) func(fx.Lifecycle, *http.ServeMux, *log.Logger) *http.Server { + return func(lc fx.Lifecycle, mux *http.ServeMux, logger *log.Logger) *http.Server { + s := &http.Server{Addr: addr, Handler: mux} + + lc.Append(fx.Hook{ + OnStart: func(ctx context.Context) error { + ln, err := net.Listen("tcp", s.Addr) + if err != nil { + return err + } + + go s.Serve(ln) + logger.Info("started http server", "addr", s.Addr) + + return nil + }, + OnStop: func(ctx context.Context) error { + return s.Shutdown(ctx) + }, + }) + + return s + } +} + func newHttpServer(lc fx.Lifecycle, mux *http.ServeMux, logger *log.Logger) *http.Server { s := &http.Server{Addr: ":8080", Handler: mux}