singleFrontend.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. "google.golang.org/grpc/metadata"
  18. )
  19. func logDebugRequest(cfg *config.Config, source string, r *http.Request) {
  20. if cfg.LogDebugOptions.SingleFrontendRequests {
  21. log.Debugf("SingleFrontend HTTP Req URL %v: %q", source, r.URL)
  22. if cfg.LogDebugOptions.SingleFrontendRequestHeaders {
  23. for name, values := range r.Header {
  24. log.Debugf("SingleFrontend HTTP Req Hdr: %v = %v", name, values)
  25. }
  26. }
  27. }
  28. }
  29. // StartSingleHTTPFrontend will create a reverse proxy that proxies the API
  30. // and webui internally.
  31. func StartSingleHTTPFrontend(cfg *config.Config, ex *executor.Executor) {
  32. log.WithFields(log.Fields{
  33. "address": cfg.ListenAddressSingleHTTPFrontend,
  34. }).Info("Starting single HTTP frontend")
  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.Debugf("SingleFrontend HTTP API Req URL after rewrite: %v", r.URL.Path)
  44. // Process HTTP headers for authentication and add to context
  45. ctx := r.Context()
  46. md := parseRequestMetadata(cfg, ctx, r)
  47. ctx = metadata.NewIncomingContext(ctx, md)
  48. r = r.WithContext(ctx)
  49. apiHandler.ServeHTTP(w, r)
  50. }))
  51. oauth2handler := NewOAuth2Handler(cfg)
  52. mux.HandleFunc("/oauth/login", oauth2handler.handleOAuthLogin)
  53. mux.HandleFunc("/oauth/callback", oauth2handler.handleOAuthCallback)
  54. mux.HandleFunc("/readyz", handleReadyz)
  55. webuiServer := NewWebUIServer(cfg)
  56. mux.HandleFunc("/theme.css", webuiServer.generateThemeCss)
  57. mux.Handle("/custom-webui/", webuiServer.handleCustomWebui())
  58. mux.HandleFunc("/", webuiServer.handleWebui)
  59. if cfg.Prometheus.Enabled {
  60. promURL, _ := url.Parse("http://" + cfg.ListenAddressPrometheus)
  61. promProxy := httputil.NewSingleHostReverseProxy(promURL)
  62. mux.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) {
  63. logDebugRequest(cfg, "prom", r)
  64. promProxy.ServeHTTP(w, r)
  65. })
  66. }
  67. srv := &http.Server{
  68. Addr: cfg.ListenAddressSingleHTTPFrontend,
  69. Handler: mux,
  70. }
  71. log.Fatal(srv.ListenAndServe())
  72. }
  73. func handleReadyz(w http.ResponseWriter, r *http.Request) {
  74. w.Header().Set("Content-Type", "text/plain; charset=utf-8")
  75. w.WriteHeader(http.StatusOK)
  76. w.Write([]byte("OK. Single HTTP Frontend is ready.\n"))
  77. }