package fx import ( "context" "fmt" "net/http" "go.uber.org/fx" ) func NewHTTPServer(lc fx.Lifecycle, handler http.Handler) *http.Server { p := new(http.Protocols) p.SetHTTP1(true) // Use h2c so we can serve HTTP/2 without TLS. p.SetUnencryptedHTTP2(true) srv := &http.Server{ Addr: ":3002", Protocols: p, Handler: handler, } lc.Append(fx.Hook{ OnStart: func(ctx context.Context) error { go func() { fmt.Println("Listening on :3002") if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { panic(err) } }() return nil }, OnStop: func(ctx context.Context) error { return srv.Shutdown(ctx) }, }) return srv }