frontend.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. log "github.com/sirupsen/logrus"
  19. )
  20. func logDebugRequest(cfg *config.Config, source string, r *http.Request) {
  21. if cfg.LogDebugOptions.SingleFrontendRequests {
  22. log.Debugf("SingleFrontend HTTP Req URL %v: %q", source, r.URL)
  23. if cfg.LogDebugOptions.SingleFrontendRequestHeaders {
  24. for name, values := range r.Header {
  25. log.Debugf("SingleFrontend HTTP Req Hdr: %v = %v", name, values)
  26. }
  27. }
  28. }
  29. }
  30. func StartFrontendMux(cfg *config.Config, ex *executor.Executor) {
  31. log.WithFields(log.Fields{
  32. "address": cfg.ListenAddressSingleHTTPFrontend,
  33. }).Info("Starting single HTTP frontend")
  34. go StartPrometheus(cfg)
  35. mux := http.NewServeMux()
  36. apiPath, apiHandler := api.GetNewHandler(ex)
  37. log.Infof("API path is %s", apiPath)
  38. mux.Handle("/api/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  39. fn := path.Base(r.URL.Path)
  40. // Translate /api/foo/bar to /api/bar - this preserves compatibility
  41. // with OliveTin 2k.
  42. r.URL.Path = apiPath + fn
  43. log.WithFields(log.Fields{
  44. "path": r.URL.Path,
  45. }).Tracef("SingleFrontend HTTP API Req URL after rewrite")
  46. logDebugRequest(cfg, "api", r)
  47. apiHandler.ServeHTTP(w, r)
  48. }))
  49. oauth2handler := otoauth2.NewOAuth2Handler(cfg)
  50. auth.AddAuthChainFunction(oauth2handler.CheckUserFromOAuth2Cookie)
  51. mux.HandleFunc("/oauth/login", oauth2handler.HandleOAuthLogin)
  52. mux.HandleFunc("/oauth/callback", oauth2handler.HandleOAuthCallback)
  53. mux.HandleFunc("/readyz", handleReadyz)
  54. webuiServer := NewWebUIServer(cfg)
  55. mux.HandleFunc("/theme.css", webuiServer.generateThemeCss)
  56. mux.Handle("/custom-webui/", webuiServer.handleCustomWebui())
  57. mux.HandleFunc("/", webuiServer.handleWebui)
  58. if cfg.Prometheus.Enabled {
  59. promURL, _ := url.Parse("http://" + cfg.ListenAddressPrometheus)
  60. promProxy := httputil.NewSingleHostReverseProxy(promURL)
  61. mux.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) {
  62. logDebugRequest(cfg, "prom", r)
  63. promProxy.ServeHTTP(w, r)
  64. })
  65. }
  66. srv := &http.Server{
  67. Addr: cfg.ListenAddressSingleHTTPFrontend,
  68. Handler: mux,
  69. }
  70. log.Fatal(srv.ListenAndServe())
  71. }
  72. func handleReadyz(w http.ResponseWriter, r *http.Request) {
  73. w.Header().Set("Content-Type", "text/plain; charset=utf-8")
  74. w.WriteHeader(http.StatusOK)
  75. _, err := w.Write([]byte("OK. Single HTTP Frontend is ready.\n"))
  76. if err != nil {
  77. log.WithFields(log.Fields{
  78. "error": err,
  79. }).Warnf("Failed to write readyz response")
  80. }
  81. }