4
0

webuiServer.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package httpservers
  2. import (
  3. "encoding/json"
  4. // cors "github.com/jamesread/OliveTin/internal/cors"
  5. log "github.com/sirupsen/logrus"
  6. "net/http"
  7. "os"
  8. config "github.com/jamesread/OliveTin/internal/config"
  9. )
  10. type webUISettings struct {
  11. Rest string
  12. }
  13. func findWebuiDir() string {
  14. directoriesToSearch := []string{
  15. "./webui",
  16. "/var/www/olivetin/",
  17. }
  18. for _, dir := range directoriesToSearch {
  19. if _, err := os.Stat(dir); !os.IsNotExist(err) {
  20. log.Infof("Found the webui directory here: %v", dir)
  21. return dir
  22. }
  23. }
  24. log.Warnf("Did not find the webui directory, you will probably get 404 errors.")
  25. return "./webui" // Should not exist
  26. }
  27. func startWebUIServer(cfg *config.Config) {
  28. log.WithFields(log.Fields{
  29. "address": cfg.ListenAddressWebUI,
  30. }).Info("Starting WebUI server")
  31. mux := http.NewServeMux()
  32. mux.Handle("/", http.FileServer(http.Dir(findWebuiDir())))
  33. mux.HandleFunc("/webUiSettings.json", func(w http.ResponseWriter, r *http.Request) {
  34. restAddress := ""
  35. if !cfg.UseSingleHTTPFrontend {
  36. restAddress = cfg.ExternalRestAddress
  37. }
  38. jsonRet, _ := json.Marshal(webUISettings{
  39. Rest: restAddress + "/api/",
  40. })
  41. w.Write([]byte(jsonRet))
  42. })
  43. srv := &http.Server{
  44. Addr: cfg.ListenAddressWebUI,
  45. Handler: mux,
  46. }
  47. log.Fatal(srv.ListenAndServe())
  48. }