webuiServer.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. "../webui/",
  44. "../frontend/",
  45. "/usr/share/OliveTin/frontend/",
  46. "/usr/share/OliveTin/webui/",
  47. "/var/www/OliveTin/",
  48. "/var/www/olivetin/",
  49. "/etc/OliveTin/frontend/",
  50. }
  51. dir, err := dirs.GetFirstExistingDirectory("webui", directoriesToSearch)
  52. if err != nil {
  53. log.Warnf("Did not find the webui directory, you will probably get 404 errors.")
  54. return "./webui" // Should not exist
  55. }
  56. log.Infof("Using webui directory: %s", dir)
  57. return dir
  58. }
  59. func (s *webUIServer) findCustomWebuiDir() string {
  60. dir := path.Join(s.cfg.GetDir(), "custom-webui")
  61. return dir
  62. }
  63. func (s *webUIServer) setupCustomWebuiDir() {
  64. dir := s.findCustomWebuiDir()
  65. err := os.MkdirAll(path.Join(dir, "themes/"), 0775)
  66. if err != nil {
  67. log.Warnf("Could not create themes directory: %v", err)
  68. }
  69. }
  70. func (s *webUIServer) generateThemeCss(w http.ResponseWriter, r *http.Request) {
  71. themeCssFilename := path.Join(s.findCustomWebuiDir(), "themes", s.cfg.ThemeName, "theme.css")
  72. if !customThemeCssRead || s.cfg.ThemeCacheDisabled {
  73. customThemeCssRead = true
  74. if _, err := os.Stat(themeCssFilename); err == nil {
  75. customThemeCss, _ = os.ReadFile(themeCssFilename)
  76. } else {
  77. log.Debugf("Theme CSS not read: %v", err)
  78. customThemeCss = []byte("/* not found */")
  79. }
  80. }
  81. w.Header().Add("Content-Type", "text/css")
  82. w.Write(customThemeCss)
  83. }
  84. func (s *webUIServer) handleCustomWebui() http.Handler {
  85. return http.StripPrefix("/custom-webui/", http.FileServer(http.Dir(s.findCustomWebuiDir())))
  86. }