singleFrontend.go 2.7 KB

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