singleFrontend.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. log "github.com/sirupsen/logrus"
  11. "net/http"
  12. "net/http/httputil"
  13. "net/url"
  14. )
  15. // StartSingleHTTPFrontend will create a reverse proxy that proxies the API
  16. // and webui internally.
  17. func StartSingleHTTPFrontend(cfg *config.Config) {
  18. log.WithFields(log.Fields{
  19. "address": cfg.ListenAddressSingleHTTPFrontend,
  20. }).Info("Starting single HTTP frontend")
  21. apiURL, _ := url.Parse("http://" + cfg.ListenAddressRestActions)
  22. apiProxy := httputil.NewSingleHostReverseProxy(apiURL)
  23. webuiURL, _ := url.Parse("http://" + cfg.ListenAddressWebUI)
  24. webuiProxy := httputil.NewSingleHostReverseProxy(webuiURL)
  25. mux := http.NewServeMux()
  26. mux.HandleFunc("/api/", func(w http.ResponseWriter, r *http.Request) {
  27. log.Debugf("api req: %q", r.URL)
  28. apiProxy.ServeHTTP(w, r)
  29. })
  30. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  31. log.Debugf("ui req: %q", r.URL)
  32. webuiProxy.ServeHTTP(w, r)
  33. })
  34. srv := &http.Server{
  35. Addr: cfg.ListenAddressSingleHTTPFrontend,
  36. Handler: mux,
  37. }
  38. log.Fatal(srv.ListenAndServe())
  39. }