Skip to content

Commit

Permalink
feat(issue-34): add http2 only support
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaba505 committed Dec 17, 2023
1 parent cc3ee27 commit d5459f4
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type runtimeOptions struct {
readiness *health.Readiness
liveness *health.Liveness
tlsConfig *tls.Config
http2Only bool
}

// RuntimeOption
Expand Down Expand Up @@ -86,6 +87,13 @@ func TLSConfig(cfg *tls.Config) RuntimeOption {
}
}

// Http2Only
func Http2Only() RuntimeOption {
return func(ro *runtimeOptions) {
ro.http2Only = true
}
}

// Runtime
type Runtime struct {
port uint
Expand All @@ -94,6 +102,7 @@ type Runtime struct {
log *slog.Logger

tlsConfig *tls.Config
http2Only bool
h http.Handler

started *health.Started
Expand All @@ -119,6 +128,7 @@ func NewRuntime(opts ...RuntimeOption) *Runtime {
listen: net.Listen,
log: slog.New(ros.logHandler),
tlsConfig: ros.tlsConfig,
http2Only: ros.http2Only,
h: ros.mux,
started: &health.Started{},
liveness: ros.liveness,
Expand Down Expand Up @@ -161,6 +171,10 @@ func (rt *Runtime) Run(ctx context.Context) error {
return err
}
if rt.tlsConfig != nil {
rt.tlsConfig.NextProtos = append([]string{"h2"}, rt.tlsConfig.NextProtos...)
if rt.http2Only {
rt.tlsConfig.NextProtos = []string{"h2"}
}
ls = tls.NewListener(ls, rt.tlsConfig)
}

Expand Down

0 comments on commit d5459f4

Please sign in to comment.