-
Notifications
You must be signed in to change notification settings - Fork 25
/
gmux.go
44 lines (37 loc) · 1.1 KB
/
gmux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package listener
import (
"net/http"
"github.com/elastic/gmux"
"go.uber.org/fx"
"github.com/fluxninja/aperture/v2/pkg/config"
"github.com/fluxninja/aperture/v2/pkg/log"
)
// GMuxConstructor holds fields to create an annotated instance gmux Listener.
type GMuxConstructor struct {
// Name of HTTP Server instance
HTTPServerName string
// Name of GRPC listener instance
ListenerName string
}
// Annotate creates an annotated instance of gmux Listener.
func (constructor GMuxConstructor) Annotate() fx.Option {
return fx.Provide(
fx.Annotate(
constructor.configureServer,
fx.ParamTags(config.NameTag(constructor.HTTPServerName)),
fx.ResultTags(config.NameTag(constructor.ListenerName)),
),
)
}
func (constructor GMuxConstructor) configureServer(server *http.Server) (*Listener, error) {
listener := &Listener{}
var err error
// Listener is automatically closed by http server on shutdown
listener.lis, err = gmux.ConfigureServer(server, nil)
if err != nil {
log.Error().Err(err).Msg("Unable to setup gmux listener!")
return nil, err
}
listener.addr = server.Addr
return listener, nil
}