4
0

webuiServer.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. }
  28. func findWebuiDir() string {
  29. directoriesToSearch := []string{
  30. cfg.WebUIDir,
  31. "../webui/",
  32. "/usr/share/OliveTin/webui/",
  33. "/var/www/OliveTin/",
  34. "/var/www/olivetin/",
  35. "/etc/OliveTin/webui/",
  36. }
  37. // Use a classic i := 0 style for loop here instead of range, as the
  38. // search order must be deterministic - the order that the slice was defined in.
  39. for i := 0; i < len(directoriesToSearch); i++ {
  40. dir := directoriesToSearch[i]
  41. absdir, _ := filepath.Abs(dir)
  42. if _, err := os.Stat(absdir); !os.IsNotExist(err) {
  43. log.WithFields(log.Fields{
  44. "dir": absdir,
  45. }).Infof("Found the webui directory")
  46. sv.Set("internal.webuidir", absdir+" ("+dir+")")
  47. return dir
  48. }
  49. }
  50. log.Warnf("Did not find the webui directory, you will probably get 404 errors.")
  51. return "./webui" // Should not exist
  52. }
  53. func findCustomWebuiDir() string {
  54. dir := path.Join(cfg.GetDir(), "custom-webui")
  55. return dir
  56. }
  57. func setupCustomWebuiDir() {
  58. dir := findCustomWebuiDir()
  59. err := os.MkdirAll(path.Join(dir, "themes/"), 0775)
  60. if err != nil {
  61. log.Warnf("Could not create themes directory: %v", err)
  62. sv.Set("internal.themesdir", err.Error())
  63. } else {
  64. sv.Set("internal.themesdir", dir)
  65. }
  66. }
  67. func generateThemeCss(w http.ResponseWriter, r *http.Request) {
  68. themeCssFilename := path.Join(findCustomWebuiDir(), "themes", cfg.ThemeName, "theme.css")
  69. if !customThemeCssRead {
  70. customThemeCssRead = true
  71. if _, err := os.Stat(themeCssFilename); err == nil {
  72. customThemeCss, err = os.ReadFile(themeCssFilename)
  73. } else {
  74. log.Debugf("Theme CSS not read: %v", err)
  75. customThemeCss = []byte("/* not found */")
  76. }
  77. }
  78. w.Header().Add("Content-Type", "text/css")
  79. w.Write(customThemeCss)
  80. }
  81. func generateWebUISettings(w http.ResponseWriter, r *http.Request) {
  82. jsonRet, _ := json.Marshal(webUISettings{
  83. Rest: cfg.ExternalRestAddress + "/api/",
  84. ShowFooter: cfg.ShowFooter,
  85. ShowNavigation: cfg.ShowNavigation,
  86. ShowNewVersions: cfg.ShowNewVersions,
  87. AvailableVersion: updatecheck.AvailableVersion,
  88. CurrentVersion: updatecheck.CurrentVersion,
  89. PageTitle: cfg.PageTitle,
  90. SectionNavigationStyle: cfg.SectionNavigationStyle,
  91. })
  92. _, err := w.Write([]byte(jsonRet))
  93. if err != nil {
  94. log.Warnf("Could not write webui settings: %v", err)
  95. }
  96. }
  97. func startWebUIServer(cfg *config.Config) {
  98. log.WithFields(log.Fields{
  99. "address": cfg.ListenAddressWebUI,
  100. }).Info("Starting WebUI server")
  101. setupCustomWebuiDir()
  102. mux := http.NewServeMux()
  103. mux.Handle("/", http.FileServer(http.Dir(findWebuiDir())))
  104. mux.Handle("/custom-webui/", http.StripPrefix("/custom-webui/", http.FileServer(http.Dir(findCustomWebuiDir()))))
  105. mux.HandleFunc("/theme.css", generateThemeCss)
  106. mux.HandleFunc("/webUiSettings.json", generateWebUISettings)
  107. srv := &http.Server{
  108. Addr: cfg.ListenAddressWebUI,
  109. Handler: mux,
  110. }
  111. log.Fatal(srv.ListenAndServe())
  112. }