| package app |
|
|
| import ( |
| "context" |
| "net/http" |
| "net/url" |
| "strings" |
| "time" |
|
|
| "github.com/prometheus/client_golang/prometheus/promhttp" |
| "github.com/target/goalert/config" |
| "github.com/target/goalert/expflag" |
| "github.com/target/goalert/genericapi" |
| "github.com/target/goalert/grafana" |
| "github.com/target/goalert/mailgun" |
| "github.com/target/goalert/notification/twilio" |
| "github.com/target/goalert/permission" |
| prometheus "github.com/target/goalert/prometheusalertmanager" |
| "github.com/target/goalert/site24x7" |
| "github.com/target/goalert/util/errutil" |
| "github.com/target/goalert/util/log" |
| "github.com/target/goalert/web" |
| ) |
|
|
| func (app *App) initHTTP(ctx context.Context) error { |
| middleware := []func(http.Handler) http.Handler{ |
| func(next http.Handler) http.Handler { |
| return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| next.ServeHTTP(w, req.WithContext(app.Context(req.Context()))) |
| }) |
| }, |
|
|
| withSecureHeaders(app.cfg.EnableSecureHeaders, strings.HasPrefix(app.cfg.PublicURL, "https://")), |
|
|
| config.ShortURLMiddleware, |
|
|
| |
| func(next http.Handler) http.Handler { |
| return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| fwdProto := req.Header.Get("x-forwarded-proto") |
| if fwdProto != "" { |
| req.URL.Scheme = fwdProto |
| } else if req.URL.Scheme == "" { |
| if req.TLS == nil { |
| req.URL.Scheme = "http" |
| } else { |
| req.URL.Scheme = "https" |
| } |
| } |
|
|
| req.URL.Host = req.Host |
| cfg := config.FromContext(req.Context()) |
|
|
| if app.cfg.DisableHTTPSRedirect || cfg.ValidReferer(req.URL.String(), req.URL.String()) { |
| next.ServeHTTP(w, req) |
| return |
| } |
|
|
| u, err := url.ParseRequestURI(req.RequestURI) |
| if errutil.HTTPError(req.Context(), w, err) { |
| return |
| } |
| u.Scheme = "https" |
| u.Host = req.Host |
| if cfg.ValidReferer(req.URL.String(), u.String()) { |
| http.Redirect(w, req, u.String(), http.StatusTemporaryRedirect) |
| return |
| } |
|
|
| next.ServeHTTP(w, req) |
| }) |
| }, |
|
|
| |
| authCheckLimit(100), |
|
|
| |
| logRequest(app.cfg.LogRequests), |
|
|
| |
| timeout(2 * time.Minute), |
|
|
| func(next http.Handler) http.Handler { |
| return http.StripPrefix(app.cfg.HTTPPrefix, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| if req.URL.Path == "" { |
| req.URL.Path = "/" |
| } |
|
|
| next.ServeHTTP(w, req) |
| })) |
| }, |
|
|
| |
| maxBodySizeMiddleware(app.cfg.MaxReqBodyBytes), |
|
|
| |
| app.AuthHandler.WrapHandler, |
|
|
| |
| logRequestAuth, |
|
|
| LimitConcurrencyByAuthSource, |
|
|
| wrapGzip, |
| } |
|
|
| if app.cfg.Verbose { |
| middleware = append(middleware, func(next http.Handler) http.Handler { |
| return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| next.ServeHTTP(w, req.WithContext(log.WithDebug(req.Context()))) |
| }) |
| }) |
| } |
|
|
| mux := http.NewServeMux() |
|
|
| generic := genericapi.NewHandler(genericapi.Config{ |
| AlertStore: app.AlertStore, |
| IntegrationKeyStore: app.IntegrationKeyStore, |
| HeartbeatStore: app.HeartbeatStore, |
| UserStore: app.UserStore, |
| }) |
|
|
| mux.Handle("POST /api/graphql", app.graphql2.Handler()) |
|
|
| mux.HandleFunc("GET /api/v2/config", app.ConfigStore.ServeConfig) |
| mux.HandleFunc("PUT /api/v2/config", app.ConfigStore.ServeConfig) |
|
|
| mux.HandleFunc("GET /api/v2/identity/providers", app.AuthHandler.ServeProviders) |
| mux.HandleFunc("POST /api/v2/identity/logout", app.AuthHandler.ServeLogout) |
|
|
| basicAuth := app.AuthHandler.IdentityProviderHandler("basic") |
| mux.HandleFunc("POST /api/v2/identity/providers/basic", basicAuth) |
|
|
| githubAuth := app.AuthHandler.IdentityProviderHandler("github") |
| mux.HandleFunc("POST /api/v2/identity/providers/github", githubAuth) |
| mux.HandleFunc("GET /api/v2/identity/providers/github/callback", githubAuth) |
|
|
| oidcAuth := app.AuthHandler.IdentityProviderHandler("oidc") |
| mux.HandleFunc("POST /api/v2/identity/providers/oidc", oidcAuth) |
| mux.HandleFunc("GET /api/v2/identity/providers/oidc/callback", oidcAuth) |
|
|
| if expflag.ContextHas(ctx, expflag.UnivKeys) { |
| mux.HandleFunc("POST /api/v2/uik", app.UIKHandler.ServeHTTP) |
| } |
| mux.HandleFunc("POST /api/v2/mailgun/incoming", mailgun.IngressWebhooks(app.AlertStore, app.IntegrationKeyStore)) |
| mux.HandleFunc("POST /api/v2/grafana/incoming", grafana.GrafanaToEventsAPI(app.AlertStore, app.IntegrationKeyStore)) |
| mux.HandleFunc("POST /api/v2/site24x7/incoming", site24x7.Site24x7ToEventsAPI(app.AlertStore, app.IntegrationKeyStore)) |
| mux.HandleFunc("POST /api/v2/prometheusalertmanager/incoming", prometheus.PrometheusAlertmanagerEventsAPI(app.AlertStore, app.IntegrationKeyStore)) |
|
|
| mux.HandleFunc("POST /api/v2/generic/incoming", generic.ServeCreateAlert) |
| mux.HandleFunc("POST /api/v2/heartbeat/{heartbeatID}", generic.ServeHeartbeatCheck) |
| mux.HandleFunc("GET /api/v2/user-avatar/{userID}", generic.ServeUserAvatar) |
| mux.HandleFunc("GET /api/v2/calendar", app.CalSubStore.ServeICalData) |
|
|
| mux.HandleFunc("POST /api/v2/twilio/message", app.twilioSMS.ServeMessage) |
| mux.HandleFunc("POST /api/v2/twilio/message/status", app.twilioSMS.ServeStatusCallback) |
| mux.HandleFunc("POST /api/v2/twilio/call", app.twilioVoice.ServeCall) |
| mux.HandleFunc("POST /api/v2/twilio/call/status", app.twilioVoice.ServeStatusCallback) |
|
|
| mux.HandleFunc("POST /api/v2/slack/message-action", app.slackChan.ServeMessageAction) |
|
|
| middleware = append(middleware, |
| httpRewrite(app.cfg.HTTPPrefix, "/v1/graphql2", "/api/graphql"), |
| httpRedirect(app.cfg.HTTPPrefix, "/v1/graphql2/explore", "/api/graphql/explore"), |
|
|
| httpRewrite(app.cfg.HTTPPrefix, "/v1/config", "/api/v2/config"), |
| httpRewrite(app.cfg.HTTPPrefix, "/v1/identity/providers", "/api/v2/identity/providers"), |
| httpRewrite(app.cfg.HTTPPrefix, "/v1/identity/providers/", "/api/v2/identity/providers/"), |
| httpRewrite(app.cfg.HTTPPrefix, "/v1/identity/logout", "/api/v2/identity/logout"), |
|
|
| httpRewrite(app.cfg.HTTPPrefix, "/v1/webhooks/mailgun", "/api/v2/mailgun/incoming"), |
| httpRewrite(app.cfg.HTTPPrefix, "/v1/webhooks/grafana", "/api/v2/grafana/incoming"), |
| httpRewrite(app.cfg.HTTPPrefix, "/v1/api/alerts", "/api/v2/generic/incoming"), |
| httpRewrite(app.cfg.HTTPPrefix, "/v1/api/heartbeat/", "/api/v2/heartbeat/"), |
| httpRewriteWith(app.cfg.HTTPPrefix, "/v1/api/users/", func(req *http.Request) *http.Request { |
| parts := strings.Split(strings.TrimSuffix(req.URL.Path, "/avatar"), "/") |
| req.URL.Path = "/api/v2/user-avatar/" + parts[len(parts)-1] |
| return req |
| }), |
|
|
| httpRewrite(app.cfg.HTTPPrefix, "/v1/twilio/sms/messages", "/api/v2/twilio/message"), |
| httpRewrite(app.cfg.HTTPPrefix, "/v1/twilio/sms/status", "/api/v2/twilio/message/status"), |
| httpRewrite(app.cfg.HTTPPrefix, "/v1/twilio/voice/call", "/api/v2/twilio/call?type=alert"), |
| httpRewrite(app.cfg.HTTPPrefix, "/v1/twilio/voice/alert-status", "/api/v2/twilio/call?type=alert-status"), |
| httpRewrite(app.cfg.HTTPPrefix, "/v1/twilio/voice/test", "/api/v2/twilio/call?type=test"), |
| httpRewrite(app.cfg.HTTPPrefix, "/v1/twilio/voice/stop", "/api/v2/twilio/call?type=stop"), |
| httpRewrite(app.cfg.HTTPPrefix, "/v1/twilio/voice/verify", "/api/v2/twilio/call?type=verify"), |
| httpRewrite(app.cfg.HTTPPrefix, "/v1/twilio/voice/status", "/api/v2/twilio/call/status"), |
|
|
| func(next http.Handler) http.Handler { |
| twilioHandler := twilio.WrapValidation( |
| |
| twilio.WrapHeaderHack(next), |
| *app.twilioConfig, |
| ) |
| return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| if strings.HasPrefix(req.URL.Path, "/api/v2/twilio/") { |
| twilioHandler.ServeHTTP(w, req) |
| return |
| } |
|
|
| next.ServeHTTP(w, req) |
| }) |
| }, |
| ) |
|
|
| mux.HandleFunc("GET /health", app.healthCheck) |
| mux.HandleFunc("GET /health/engine", app.engineStatus) |
| mux.HandleFunc("GET /health/engine/cycle", app.engineCycle) |
| mux.Handle("GET /health/", http.NotFoundHandler()) |
|
|
| webH, err := web.NewHandler(app.cfg.UIDir, app.cfg.HTTPPrefix) |
| if err != nil { |
| return err |
| } |
|
|
| |
| mux.Handle("GET /api/", http.NotFoundHandler()) |
| mux.Handle("POST /api/", http.NotFoundHandler()) |
| mux.Handle("GET /v1/", http.NotFoundHandler()) |
| mux.Handle("POST /v1/", http.NotFoundHandler()) |
|
|
| |
| mux.Handle("GET /", webH) |
|
|
| mux.Handle("GET /api/graphql/explore", webH) |
| mux.Handle("GET /api/graphql/explore/", webH) |
|
|
| mux.HandleFunc("GET /admin/riverui/", func(w http.ResponseWriter, r *http.Request) { |
| err := permission.LimitCheckAny(r.Context(), permission.Admin) |
| if permission.IsUnauthorized(err) { |
| |
| webH.ServeHTTP(w, r) |
| return |
| } |
| if errutil.HTTPError(r.Context(), w, err) { |
| return |
| } |
|
|
| app.RiverUI.ServeHTTP(w, r) |
| }) |
| mux.HandleFunc("POST /admin/riverui/api/", func(w http.ResponseWriter, r *http.Request) { |
| err := permission.LimitCheckAny(r.Context(), permission.Admin) |
| if errutil.HTTPError(r.Context(), w, err) { |
| return |
| } |
|
|
| app.RiverUI.ServeHTTP(w, r) |
| }) |
|
|
| app.srv = &http.Server{ |
| Handler: applyMiddleware(mux, middleware...), |
|
|
| ReadHeaderTimeout: time.Second * 30, |
| ReadTimeout: time.Minute, |
| WriteTimeout: time.Minute, |
| IdleTimeout: time.Minute * 2, |
| MaxHeaderBytes: app.cfg.MaxReqHeaderBytes, |
| } |
| app.srv.Handler = promhttp.InstrumentHandlerInFlight(metricReqInFlight, app.srv.Handler) |
| app.srv.Handler = promhttp.InstrumentHandlerCounter(metricReqTotal, app.srv.Handler) |
|
|
| |
| |
| |
| app.srv.SetKeepAlivesEnabled(false) |
|
|
| return nil |
| } |
|
|