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. "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. r.URL.Path = apiPath + fn
  40. log.Infof("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("/theme.css", webuiServer.generateThemeCss)
  49. mux.Handle("/custom-webui/", webuiServer.handleCustomWebui())
  50. mux.HandleFunc("/", webuiServer.handleWebui)
  51. if cfg.Prometheus.Enabled {
  52. promURL, _ := url.Parse("http://" + cfg.ListenAddressPrometheus)
  53. promProxy := httputil.NewSingleHostReverseProxy(promURL)
  54. mux.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) {
  55. logDebugRequest(cfg, "prom", r)
  56. promProxy.ServeHTTP(w, r)
  57. })
  58. }
  59. srv := &http.Server{
  60. Addr: cfg.ListenAddressSingleHTTPFrontend,
  61. Handler: mux,
  62. }
  63. log.Fatal(srv.ListenAndServe())
  64. }
  65. func handleReadyz(w http.ResponseWriter, r *http.Request) {
  66. w.Header().Set("Content-Type", "text/plain; charset=utf-8")
  67. w.WriteHeader(http.StatusOK)
  68. w.Write([]byte("OK. Single HTTP Frontend is ready.\n"))
  69. }