mirror of
https://github.com/OliveTin/OliveTin
synced 2026-08-27 21:16:29 +00:00
Single endpoint for web+ui (Issue #4). Migrated to modern protobuf+grpc.
This commit is contained in:
@@ -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())
|
||||
}
|
||||
Reference in New Issue
Block a user