webuiServer.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package httpservers
  2. import (
  3. // cors "github.com/OliveTin/OliveTin/internal/cors"
  4. "net/http"
  5. "os"
  6. "path"
  7. log "github.com/sirupsen/logrus"
  8. "github.com/jamesread/golure/pkg/dirs"
  9. config "github.com/OliveTin/OliveTin/internal/config"
  10. )
  11. type webUIServer struct {
  12. cfg *config.Config
  13. webuiDir string
  14. }
  15. var (
  16. customThemeCss []byte
  17. customThemeCssRead = false
  18. )
  19. func NewWebUIServer(cfg *config.Config) *webUIServer {
  20. s := &webUIServer{
  21. cfg: cfg,
  22. }
  23. s.webuiDir = s.findWebuiDir()
  24. s.setupCustomWebuiDir()
  25. return s
  26. }
  27. func (s *webUIServer) handleWebui(w http.ResponseWriter, r *http.Request) {
  28. // dirName := path.Dir(r.URL.Path)
  29. // Mangle requests for any path like /logs or /config to load the webui index.html
  30. if path.Ext(r.URL.Path) == "" && r.URL.Path != "/" {
  31. log.Debugf("Mangling request for %s to /index.html", r.URL.Path)
  32. http.ServeFile(w, r, path.Join(s.webuiDir, "index.html"))
  33. } else {
  34. log.Infof("Serving webui from %s for %s", s.webuiDir, r.URL.Path)
  35. http.ServeFile(w, r, path.Join(s.webuiDir, r.URL.Path))
  36. // http.StripPrefix(dirName, http.FileServer(http.Dir(s.webuiDir))).ServeHTTP(w, r)
  37. }
  38. }
  39. func (s *webUIServer) findWebuiDir() string {
  40. directoriesToSearch := []string{
  41. s.cfg.WebUIDir,
  42. "../frontend/dist/",
  43. "../frontend/",
  44. "/usr/share/OliveTin/frontend/",
  45. "/var/www/OliveTin/",
  46. "/var/www/olivetin/",
  47. "/etc/OliveTin/frontend/",
  48. }
  49. dir, err := dirs.GetFirstExistingDirectory("webui", directoriesToSearch)
  50. if err != nil {
  51. log.Warnf("Did not find the webui directory, you will probably get 404 errors.")
  52. return "./webui" // Should not exist
  53. }
  54. log.Infof("Using webui directory: %s", dir)
  55. return dir
  56. }
  57. func (s *webUIServer) findCustomWebuiDir() string {
  58. dir := path.Join(s.cfg.GetDir(), "custom-webui")
  59. return dir
  60. }
  61. func (s *webUIServer) setupCustomWebuiDir() {
  62. dir := s.findCustomWebuiDir()
  63. err := os.MkdirAll(path.Join(dir, "themes/"), 0775)
  64. if err != nil {
  65. log.Warnf("Could not create themes directory: %v", err)
  66. }
  67. }
  68. func (s *webUIServer) generateThemeCss(w http.ResponseWriter, r *http.Request) {
  69. themeCssFilename := path.Join(s.findCustomWebuiDir(), "themes", s.cfg.ThemeName, "theme.css")
  70. if !customThemeCssRead || s.cfg.ThemeCacheDisabled {
  71. customThemeCssRead = true
  72. if _, err := os.Stat(themeCssFilename); err == nil {
  73. customThemeCss, _ = os.ReadFile(themeCssFilename)
  74. } else {
  75. log.Debugf("Theme CSS not read: %v", err)
  76. customThemeCss = []byte("/* not found */")
  77. }
  78. }
  79. w.Header().Add("Content-Type", "text/css")
  80. w.Write(customThemeCss)
  81. }
  82. func (s *webUIServer) handleCustomWebui() http.Handler {
  83. return http.StripPrefix("/custom-webui/", http.FileServer(http.Dir(s.findCustomWebuiDir())))
  84. }