mirror of
https://github.com/OliveTin/OliveTin
synced 2026-08-29 05:56:41 +00:00
56 lines
1.4 KiB
Go
56 lines
1.4 KiB
Go
package httpservers
|
|
|
|
/*
|
|
This file implements a very simple, lightweight reverse proxy so that REST and
|
|
the webui can be accessed from a single endpoint.
|
|
|
|
This makes external reverse proxies (treafik, haproxy, etc) easier, CORS goes
|
|
away, and several other issues.
|
|
*/
|
|
|
|
import (
|
|
config "github.com/OliveTin/OliveTin/internal/config"
|
|
log "github.com/sirupsen/logrus"
|
|
"net/http"
|
|
"net/http/httputil"
|
|
"net/url"
|
|
"strings"
|
|
)
|
|
|
|
// StartSingleHTTPFrontend will create a reverse proxy that proxies the API
|
|
// and webui internally.
|
|
func StartSingleHTTPFrontend(cfg *config.Config) {
|
|
log.WithFields(log.Fields{
|
|
"address": cfg.ListenAddressSingleHTTPFrontend,
|
|
}).Info("Starting single HTTP frontend")
|
|
|
|
apiURL, _ := url.Parse("http://" + cfg.ListenAddressRestActions)
|
|
apiProxy := httputil.NewSingleHostReverseProxy(apiURL)
|
|
|
|
webuiURL, _ := url.Parse("http://" + cfg.ListenAddressWebUI)
|
|
webuiProxy := httputil.NewSingleHostReverseProxy(webuiURL)
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/api/", func(w http.ResponseWriter, r *http.Request) {
|
|
log.Debugf("api req: %q", r.URL)
|
|
apiProxy.ServeHTTP(w, r)
|
|
})
|
|
|
|
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
if strings.Contains(r.Header.Get("Connection"), "Upgrade") {
|
|
handleWebsocket(w, r)
|
|
} else {
|
|
log.Debugf("ui req: %q", r.URL)
|
|
webuiProxy.ServeHTTP(w, r)
|
|
}
|
|
})
|
|
|
|
srv := &http.Server{
|
|
Addr: cfg.ListenAddressSingleHTTPFrontend,
|
|
Handler: mux,
|
|
}
|
|
|
|
log.Fatal(srv.ListenAndServe())
|
|
}
|