webuiServer.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package httpservers
  2. import (
  3. "encoding/json"
  4. // cors "github.com/OliveTin/OliveTin/internal/cors"
  5. log "github.com/sirupsen/logrus"
  6. "net/http"
  7. "os"
  8. "path"
  9. "path/filepath"
  10. config "github.com/OliveTin/OliveTin/internal/config"
  11. sv "github.com/OliveTin/OliveTin/internal/stringvariables"
  12. updatecheck "github.com/OliveTin/OliveTin/internal/updatecheck"
  13. )
  14. var (
  15. customThemeCss []byte
  16. customThemeCssRead = false
  17. )
  18. type webUISettings struct {
  19. Rest string
  20. ShowFooter bool
  21. ShowNavigation bool
  22. ShowNewVersions bool
  23. AvailableVersion string
  24. CurrentVersion string
  25. PageTitle string
  26. SectionNavigationStyle string
  27. DefaultIconForBack string
  28. }
  29. func findWebuiDir() string {
  30. directoriesToSearch := []string{
  31. cfg.WebUIDir,
  32. "../webui/",
  33. "/usr/share/OliveTin/webui/",
  34. "/var/www/OliveTin/",
  35. "/var/www/olivetin/",
  36. "/etc/OliveTin/webui/",
  37. }
  38. // Use a classic i := 0 style for loop here instead of range, as the
  39. // search order must be deterministic - the order that the slice was defined in.
  40. for i := 0; i < len(directoriesToSearch); i++ {
  41. dir := directoriesToSearch[i]
  42. absdir, _ := filepath.Abs(dir)
  43. if _, err := os.Stat(absdir); !os.IsNotExist(err) {
  44. log.WithFields(log.Fields{
  45. "dir": absdir,
  46. }).Infof("Found the webui directory")
  47. sv.Set("internal.webuidir", absdir+" ("+dir+")")
  48. return dir
  49. }
  50. }
  51. log.Warnf("Did not find the webui directory, you will probably get 404 errors.")
  52. return "./webui" // Should not exist
  53. }
  54. func findCustomWebuiDir() string {
  55. dir := path.Join(cfg.GetDir(), "custom-webui")
  56. return dir
  57. }
  58. func setupCustomWebuiDir() {
  59. dir := findCustomWebuiDir()
  60. err := os.MkdirAll(path.Join(dir, "themes/"), 0775)
  61. if err != nil {
  62. log.Warnf("Could not create themes directory: %v", err)
  63. sv.Set("internal.themesdir", err.Error())
  64. } else {
  65. sv.Set("internal.themesdir", dir)
  66. }
  67. }
  68. func generateThemeCss(w http.ResponseWriter, r *http.Request) {
  69. themeCssFilename := path.Join(findCustomWebuiDir(), "themes", cfg.ThemeName, "theme.css")
  70. if !customThemeCssRead {
  71. customThemeCssRead = true
  72. if _, err := os.Stat(themeCssFilename); err == nil {
  73. customThemeCss, err = 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 generateWebUISettings(w http.ResponseWriter, r *http.Request) {
  83. jsonRet, _ := json.Marshal(webUISettings{
  84. Rest: cfg.ExternalRestAddress + "/api/",
  85. ShowFooter: cfg.ShowFooter,
  86. ShowNavigation: cfg.ShowNavigation,
  87. ShowNewVersions: cfg.ShowNewVersions,
  88. AvailableVersion: updatecheck.AvailableVersion,
  89. CurrentVersion: updatecheck.CurrentVersion,
  90. PageTitle: cfg.PageTitle,
  91. SectionNavigationStyle: cfg.SectionNavigationStyle,
  92. DefaultIconForBack: cfg.DefaultIconForBack,
  93. })
  94. _, err := w.Write([]byte(jsonRet))
  95. if err != nil {
  96. log.Warnf("Could not write webui settings: %v", err)
  97. }
  98. }
  99. func startWebUIServer(cfg *config.Config) {
  100. log.WithFields(log.Fields{
  101. "address": cfg.ListenAddressWebUI,
  102. }).Info("Starting WebUI server")
  103. setupCustomWebuiDir()
  104. mux := http.NewServeMux()
  105. mux.Handle("/", http.FileServer(http.Dir(findWebuiDir())))
  106. mux.Handle("/custom-webui/", http.StripPrefix("/custom-webui/", http.FileServer(http.Dir(findCustomWebuiDir()))))
  107. mux.HandleFunc("/theme.css", generateThemeCss)
  108. mux.HandleFunc("/webUiSettings.json", generateWebUISettings)
  109. srv := &http.Server{
  110. Addr: cfg.ListenAddressWebUI,
  111. Handler: mux,
  112. }
  113. log.Fatal(srv.ListenAndServe())
  114. }