singleFrontend.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. config "github.com/OliveTin/OliveTin/internal/config"
  10. "github.com/OliveTin/OliveTin/internal/api"
  11. "github.com/OliveTin/OliveTin/internal/executor"
  12. log "github.com/sirupsen/logrus"
  13. "net/http"
  14. "net/http/httputil"
  15. "net/url"
  16. "path"
  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. r.URL.Path = apiPath + fn
  40. log.Debugf("SingleFrontend HTTP API Req URL after rewrite: %v", r.URL.Path)
  41. apiHandler.ServeHTTP(w, r)
  42. }))
  43. oauth2handler := NewOAuth2Handler(cfg)
  44. mux.HandleFunc("/oauth/login", oauth2handler.handleOAuthLogin)
  45. mux.HandleFunc("/oauth/callback", oauth2handler.handleOAuthCallback)
  46. mux.HandleFunc("/readyz", handleReadyz)
  47. webuiServer := NewWebUIServer(cfg)
  48. mux.HandleFunc("/webUiSettings.json", webuiServer.generateWebUISettings)
  49. mux.HandleFunc("/theme.css", webuiServer.generateThemeCss)
  50. mux.Handle("/custom-webui/", webuiServer.handleCustomWebui())
  51. mux.HandleFunc("/", webuiServer.handleWebui)
  52. if cfg.Prometheus.Enabled {
  53. promURL, _ := url.Parse("http://" + cfg.ListenAddressPrometheus)
  54. promProxy := httputil.NewSingleHostReverseProxy(promURL)
  55. mux.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) {
  56. logDebugRequest(cfg, "prom", r)
  57. promProxy.ServeHTTP(w, r)
  58. })
  59. }
  60. srv := &http.Server{
  61. Addr: cfg.ListenAddressSingleHTTPFrontend,
  62. Handler: mux,
  63. }
  64. log.Fatal(srv.ListenAndServe())
  65. }
  66. func handleReadyz(w http.ResponseWriter, r *http.Request) {
  67. w.Header().Set("Content-Type", "text/plain; charset=utf-8")
  68. w.WriteHeader(http.StatusOK)
  69. w.Write([]byte("OK. Single HTTP Frontend is ready.\n"))
  70. }