mirror of
https://github.com/OliveTin/OliveTin
synced 2026-09-03 08:25:37 +00:00
Single endpoint for web+ui (Issue #4). Migrated to modern protobuf+grpc.
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
package httpservers
|
||||
|
||||
import (
|
||||
config "github.com/jamesread/OliveTin/internal/config"
|
||||
)
|
||||
|
||||
// StartServers will start 3 HTTP servers. The WebUI, the Rest API, and a proxy
|
||||
// for both of them.
|
||||
func StartServers(cfg *config.Config) {
|
||||
go startWebUIServer(cfg)
|
||||
go startRestAPIServer(cfg)
|
||||
|
||||
if cfg.UseSingleHTTPFrontend {
|
||||
StartSingleHTTPFrontend(cfg)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package httpservers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/protobuf/encoding/protojson"
|
||||
"net/http"
|
||||
|
||||
gw "github.com/jamesread/OliveTin/gen/grpc"
|
||||
|
||||
cors "github.com/jamesread/OliveTin/internal/cors"
|
||||
|
||||
config "github.com/jamesread/OliveTin/internal/config"
|
||||
)
|
||||
|
||||
var (
|
||||
cfg *config.Config
|
||||
)
|
||||
|
||||
func startRestAPIServer(globalConfig *config.Config) error {
|
||||
cfg = globalConfig
|
||||
|
||||
log.WithFields(log.Fields{
|
||||
"address": cfg.ListenAddressGrpcActions,
|
||||
}).Info("Starting REST API")
|
||||
|
||||
ctx := context.Background()
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
|
||||
// The JSONPb.EmitDefaults is necssary, so "empty" fields are returned in JSON.
|
||||
//mux := runtime.NewServeMux(runtime.WithMarshalerOption(runtime.MIMEWildcard, &runtime.JSONPb{OrigName: true, EmitDefaults: true}))
|
||||
mux := runtime.NewServeMux(
|
||||
runtime.WithMarshalerOption(runtime.MIMEWildcard, &runtime.HTTPBodyMarshaler{
|
||||
Marshaler: &runtime.JSONPb{
|
||||
MarshalOptions: protojson.MarshalOptions{
|
||||
UseProtoNames: true,
|
||||
EmitUnpopulated: true,
|
||||
},
|
||||
},
|
||||
}),
|
||||
)
|
||||
opts := []grpc.DialOption{grpc.WithInsecure()}
|
||||
|
||||
err := gw.RegisterOliveTinApiHandlerFromEndpoint(ctx, mux, cfg.ListenAddressGrpcActions, opts)
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("gw error %v", err)
|
||||
}
|
||||
|
||||
return http.ListenAndServe(cfg.ListenAddressRestActions, cors.AllowCors(mux))
|
||||
}
|
||||
@@ -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())
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package httpservers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
// cors "github.com/jamesread/OliveTin/internal/cors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
config "github.com/jamesread/OliveTin/internal/config"
|
||||
)
|
||||
|
||||
type webUISettings struct {
|
||||
Rest string
|
||||
}
|
||||
|
||||
func findWebuiDir() string {
|
||||
directoriesToSearch := []string{
|
||||
"./webui",
|
||||
"/var/www/olivetin/",
|
||||
}
|
||||
|
||||
for _, dir := range directoriesToSearch {
|
||||
if _, err := os.Stat(dir); !os.IsNotExist(err) {
|
||||
log.Infof("Found the webui directory here: %v", dir)
|
||||
|
||||
return dir
|
||||
}
|
||||
}
|
||||
|
||||
log.Warnf("Did not find the webui directory, you will probably get 404 errors.")
|
||||
|
||||
return "./webui" // Should not exist
|
||||
}
|
||||
|
||||
func startWebUIServer(cfg *config.Config) {
|
||||
log.WithFields(log.Fields{
|
||||
"address": cfg.ListenAddressWebUI,
|
||||
}).Info("Starting WebUI server")
|
||||
|
||||
mux := http.NewServeMux()
|
||||
mux.Handle("/", http.FileServer(http.Dir(findWebuiDir())))
|
||||
mux.HandleFunc("/webUiSettings.json", func(w http.ResponseWriter, r *http.Request) {
|
||||
restAddress := ""
|
||||
|
||||
if !cfg.UseSingleHTTPFrontend {
|
||||
restAddress = cfg.ExternalRestAddress
|
||||
}
|
||||
|
||||
jsonRet, _ := json.Marshal(webUISettings{
|
||||
Rest: restAddress + "/api/",
|
||||
})
|
||||
|
||||
w.Write([]byte(jsonRet))
|
||||
})
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: cfg.ListenAddressWebUI,
|
||||
Handler: mux,
|
||||
}
|
||||
|
||||
log.Fatal(srv.ListenAndServe())
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package httpservers
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetWebuiDir(t *testing.T) {
|
||||
dir := findWebuiDir()
|
||||
|
||||
assert.Equal(t, "./webui", dir, "Finding the webui dir")
|
||||
}
|
||||
Reference in New Issue
Block a user