webuiServer.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. ThemeName string
  13. }
  14. func findWebuiDir() string {
  15. directoriesToSearch := []string{
  16. "./webui",
  17. "/var/www/olivetin/",
  18. }
  19. for _, dir := range directoriesToSearch {
  20. if _, err := os.Stat(dir); !os.IsNotExist(err) {
  21. log.Infof("Found the webui directory here: %v", dir)
  22. return dir
  23. }
  24. }
  25. log.Warnf("Did not find the webui directory, you will probably get 404 errors.")
  26. return "./webui" // Should not exist
  27. }
  28. func generateWebUISettings(w http.ResponseWriter, r *http.Request) {
  29. restAddress := ""
  30. if !cfg.UseSingleHTTPFrontend {
  31. restAddress = cfg.ExternalRestAddress
  32. }
  33. jsonRet, _ := json.Marshal(webUISettings{
  34. Rest: restAddress + "/api/",
  35. ThemeName: cfg.ThemeName,
  36. })
  37. w.Write([]byte(jsonRet))
  38. }
  39. func startWebUIServer(cfg *config.Config) {
  40. log.WithFields(log.Fields{
  41. "address": cfg.ListenAddressWebUI,
  42. }).Info("Starting WebUI server")
  43. mux := http.NewServeMux()
  44. mux.Handle("/", http.FileServer(http.Dir(findWebuiDir())))
  45. mux.HandleFunc("/webUiSettings.json", generateWebUISettings)
  46. srv := &http.Server{
  47. Addr: cfg.ListenAddressWebUI,
  48. Handler: mux,
  49. }
  50. log.Fatal(srv.ListenAndServe())
  51. }