4
0

webuiServer.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. updatecheck "github.com/jamesread/OliveTin/internal/updatecheck"
  10. )
  11. type webUISettings struct {
  12. Rest string
  13. ThemeName string
  14. HideNavigation bool
  15. AvailableVersion string
  16. CurrentVersion string
  17. ShowNewVersions bool
  18. }
  19. func findWebuiDir() string {
  20. directoriesToSearch := []string{
  21. "./webui",
  22. "/var/www/olivetin/",
  23. }
  24. for _, dir := range directoriesToSearch {
  25. if _, err := os.Stat(dir); !os.IsNotExist(err) {
  26. log.Infof("Found the webui directory here: %v", dir)
  27. return dir
  28. }
  29. }
  30. log.Warnf("Did not find the webui directory, you will probably get 404 errors.")
  31. return "./webui" // Should not exist
  32. }
  33. func generateWebUISettings(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. ThemeName: cfg.ThemeName,
  41. HideNavigation: cfg.HideNavigation,
  42. AvailableVersion: updatecheck.AvailableVersion,
  43. CurrentVersion: updatecheck.CurrentVersion,
  44. ShowNewVersions: cfg.ShowNewVersions,
  45. })
  46. w.Write([]byte(jsonRet))
  47. }
  48. func startWebUIServer(cfg *config.Config) {
  49. log.WithFields(log.Fields{
  50. "address": cfg.ListenAddressWebUI,
  51. }).Info("Starting WebUI server")
  52. mux := http.NewServeMux()
  53. mux.Handle("/", http.FileServer(http.Dir(findWebuiDir())))
  54. mux.HandleFunc("/webUiSettings.json", generateWebUISettings)
  55. srv := &http.Server{
  56. Addr: cfg.ListenAddressWebUI,
  57. Handler: mux,
  58. }
  59. log.Fatal(srv.ListenAndServe())
  60. }