-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
104 lines (93 loc) · 3.04 KB
/
http.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package api
import (
"context"
"errors"
"net/http"
"time"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/go-chi/chi/v5"
"github.com/nais/api/internal/auth"
"github.com/nais/api/internal/auth/authn"
"github.com/nais/api/internal/auth/middleware"
"github.com/nais/api/internal/database"
"github.com/nais/api/internal/graph/loader"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rs/cors"
"github.com/sirupsen/logrus"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
semconv "go.opentelemetry.io/otel/semconv/v1.24.0"
"go.opentelemetry.io/otel/trace"
"golang.org/x/sync/errgroup"
)
// runHttpServer will start the HTTP server
func runHttpServer(
ctx context.Context,
listenAddress string,
insecureAuth bool,
db database.Database,
authHandler authn.Handler,
graphHandler *handler.Server,
reg prometheus.Gatherer,
log logrus.FieldLogger,
) error {
router := chi.NewRouter()
router.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{}))
router.Get("/healthz", func(_ http.ResponseWriter, _ *http.Request) {})
router.Method("GET", "/",
otelhttp.WithRouteTag("playground", otelhttp.NewHandler(playground.Handler("GraphQL playground", "/query"), "playground")),
)
middlewares := []func(http.Handler) http.Handler{}
if insecureAuth {
middlewares = append(middlewares, auth.InsecureUserHeader(db))
}
middlewares = append(middlewares,
cors.New(
cors.Options{
AllowedOrigins: []string{"https://*", "http://*"},
AllowedMethods: []string{"GET", "POST", "OPTIONS"},
AllowCredentials: true,
},
).Handler,
middleware.ApiKeyAuthentication(db),
middleware.Oauth2Authentication(db, authHandler),
loader.Middleware(db),
)
router.Route("/query", func(r chi.Router) {
r.Use(middlewares...)
r.Use(otelhttp.NewMiddleware("graphql", otelhttp.WithPublicEndpoint(), otelhttp.WithSpanOptions(trace.WithAttributes(semconv.ServiceName("http")))))
r.Method("POST", "/", otelhttp.WithRouteTag("query", graphHandler))
})
router.Route("/oauth2", func(r chi.Router) {
r.Get("/login", authHandler.Login)
r.Get("/logout", authHandler.Logout)
r.Get("/callback", authHandler.Callback)
})
srv := &http.Server{
Addr: listenAddress,
Handler: router,
}
wg, ctx := errgroup.WithContext(ctx)
wg.Go(func() error {
<-ctx.Done()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
log.Infof("HTTP server shutting down...")
if err := srv.Shutdown(ctx); err != nil && !errors.Is(err, context.Canceled) {
log.WithError(err).Infof("HTTP server shutdown failed")
return err
}
return nil
})
wg.Go(func() error {
log.Infof("HTTP server accepting requests on %q", listenAddress)
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.WithError(err).Infof("unexpected error from HTTP server")
return err
}
log.Infof("HTTP server finished, terminating...")
return nil
})
return wg.Wait()
}