mirror of
https://github.com/OliveTin/OliveTin
synced 2026-09-23 02:05:42 +00:00
Single endpoint for web+ui (Issue #4). Migrated to modern protobuf+grpc.
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
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/jamesread/OliveTin/internal/config"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
// 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: %v", r.URL)
|
||||
apiProxy.ServeHTTP(w, r)
|
||||
})
|
||||
|
||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
log.Debugf("ui req: %v", r.URL)
|
||||
webuiProxy.ServeHTTP(w, r)
|
||||
})
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: cfg.ListenAddressSingleHTTPFrontend,
|
||||
Handler: mux,
|
||||
}
|
||||
|
||||
log.Fatal(srv.ListenAndServe())
|
||||
}
|
||||
Reference in New Issue
Block a user