generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use separate bind for http ingress server
- Loading branch information
1 parent
3b19d66
commit c00648b
Showing
6 changed files
with
86 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package http | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"net" | ||
"net/http" | ||
"net/url" | ||
"time" | ||
|
||
"github.com/TBD54566975/ftl/internal/log" | ||
) | ||
|
||
const ShutdownGracePeriod = 5 * time.Second | ||
|
||
type Server struct { | ||
listen *url.URL | ||
Server *http.Server | ||
} | ||
|
||
func NewServer(ctx context.Context, listen *url.URL, handler http.Handler) *Server { | ||
httpServer := &http.Server{ | ||
Addr: listen.Host, | ||
Handler: handler, | ||
ReadHeaderTimeout: 30 * time.Second, | ||
BaseContext: func(net.Listener) context.Context { | ||
return ctx | ||
}, | ||
} | ||
|
||
return &Server{ | ||
listen: listen, | ||
Server: httpServer, | ||
} | ||
} | ||
|
||
func (s *Server) Serve(ctx context.Context) error { | ||
go func() { | ||
<-ctx.Done() | ||
shutdownCtx, cancel := context.WithTimeout(context.Background(), ShutdownGracePeriod) | ||
defer cancel() | ||
err := s.Server.Shutdown(shutdownCtx) | ||
if err != nil { | ||
if errors.Is(err, context.Canceled) { | ||
_ = s.Server.Close() | ||
return | ||
} | ||
log.FromContext(ctx).Errorf(err, "server shutdown error") | ||
} | ||
}() | ||
|
||
err := s.Server.ListenAndServe() | ||
if errors.Is(err, http.ErrServerClosed) { | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
func Serve(ctx context.Context, listen *url.URL, handler http.Handler) error { | ||
server := NewServer(ctx, listen, handler) | ||
return server.Serve(ctx) | ||
} |