frontend.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package httpservers
  2. /*
  3. This file implements a very simple, lightweight reverse proxy so that REST and
  4. the webui can be accessed from a single endpoint.
  5. This makes external reverse proxies (treafik, haproxy, etc) easier, CORS goes
  6. away, and several other issues.
  7. */
  8. import (
  9. "net/http"
  10. "net/http/httputil"
  11. "net/url"
  12. "path"
  13. "github.com/OliveTin/OliveTin/internal/api"
  14. "github.com/OliveTin/OliveTin/internal/auth"
  15. "github.com/OliveTin/OliveTin/internal/auth/otoauth2"
  16. config "github.com/OliveTin/OliveTin/internal/config"
  17. "github.com/OliveTin/OliveTin/internal/executor"
  18. "github.com/OliveTin/OliveTin/internal/webhooks"
  19. log "github.com/sirupsen/logrus"
  20. )
  21. func logDebugRequest(cfg *config.Config, source string, r *http.Request) {
  22. if cfg.LogDebugOptions.SingleFrontendRequests {
  23. log.Debugf("SingleFrontend HTTP Req URL %v: %q", source, r.URL)
  24. if cfg.LogDebugOptions.SingleFrontendRequestHeaders {
  25. for name, values := range r.Header {
  26. log.Debugf("SingleFrontend HTTP Req Hdr: %v = %v", name, values)
  27. }
  28. }
  29. }
  30. }
  31. func StartFrontendMux(cfg *config.Config, ex *executor.Executor) {
  32. log.WithFields(log.Fields{
  33. "address": cfg.ListenAddressSingleHTTPFrontend,
  34. }).Info("Starting single HTTP frontend")
  35. go StartPrometheus(cfg)
  36. mux := http.NewServeMux()
  37. apiPath, apiHandler := api.GetNewHandler(ex)
  38. log.Infof("API path is %s", apiPath)
  39. mux.Handle("/api/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  40. fn := path.Base(r.URL.Path)
  41. // Translate /api/foo/bar to /api/bar - this preserves compatibility
  42. // with OliveTin 2k.
  43. r.URL.Path = apiPath + fn
  44. log.WithFields(log.Fields{
  45. "path": r.URL.Path,
  46. }).Tracef("SingleFrontend HTTP API Req URL after rewrite")
  47. logDebugRequest(cfg, "api", r)
  48. apiHandler.ServeHTTP(w, r)
  49. }))
  50. oauth2handler := otoauth2.NewOAuth2Handler(cfg)
  51. auth.AddAuthChainFunction(oauth2handler.CheckUserFromOAuth2Cookie)
  52. mux.HandleFunc("/oauth/login", oauth2handler.HandleOAuthLogin)
  53. mux.HandleFunc("/oauth/callback", oauth2handler.HandleOAuthCallback)
  54. mux.HandleFunc("/readyz", handleReadyz)
  55. webhookHandler := webhooks.NewWebhookHandler(cfg, ex)
  56. mux.HandleFunc("/webhooks", webhookHandler.HandleWebhook)
  57. mux.HandleFunc("/webhooks/", webhookHandler.HandleWebhook)
  58. webuiServer := NewWebUIServer(cfg)
  59. mux.HandleFunc("/theme.css", webuiServer.generateThemeCss)
  60. mux.Handle("/custom-webui/", webuiServer.handleCustomWebui())
  61. mux.HandleFunc("/", webuiServer.handleWebui)
  62. if cfg.Prometheus.Enabled {
  63. promURL, _ := url.Parse("http://" + cfg.ListenAddressPrometheus)
  64. promProxy := httputil.NewSingleHostReverseProxy(promURL)
  65. mux.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) {
  66. logDebugRequest(cfg, "prom", r)
  67. promProxy.ServeHTTP(w, r)
  68. })
  69. }
  70. srv := &http.Server{
  71. Addr: cfg.ListenAddressSingleHTTPFrontend,
  72. Handler: mux,
  73. }
  74. log.Fatal(srv.ListenAndServe())
  75. }
  76. func handleReadyz(w http.ResponseWriter, r *http.Request) {
  77. w.Header().Set("Content-Type", "text/plain; charset=utf-8")
  78. w.WriteHeader(http.StatusOK)
  79. _, err := w.Write([]byte("OK. Single HTTP Frontend is ready.\n"))
  80. if err != nil {
  81. log.WithFields(log.Fields{
  82. "error": err,
  83. }).Warnf("Failed to write readyz response")
  84. }
  85. }