4
0

webuiServer.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. installationinfo "github.com/OliveTin/OliveTin/internal/installationinfo"
  12. sv "github.com/OliveTin/OliveTin/internal/stringvariables"
  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. SshFoundKey string
  29. SshFoundConfig string
  30. EnableCustomJs bool
  31. }
  32. func findWebuiDir() string {
  33. directoriesToSearch := []string{
  34. cfg.WebUIDir,
  35. "../webui/",
  36. "/usr/share/OliveTin/webui/",
  37. "/var/www/OliveTin/",
  38. "/var/www/olivetin/",
  39. "/etc/OliveTin/webui/",
  40. }
  41. // Use a classic i := 0 style for loop here instead of range, as the
  42. // search order must be deterministic - the order that the slice was defined in.
  43. for i := 0; i < len(directoriesToSearch); i++ {
  44. dir := directoriesToSearch[i]
  45. absdir, _ := filepath.Abs(dir)
  46. if _, err := os.Stat(absdir); !os.IsNotExist(err) {
  47. log.WithFields(log.Fields{
  48. "dir": absdir,
  49. }).Infof("Found the webui directory")
  50. sv.Set("internal.webuidir", absdir+" ("+dir+")")
  51. return dir
  52. }
  53. }
  54. log.Warnf("Did not find the webui directory, you will probably get 404 errors.")
  55. return "./webui" // Should not exist
  56. }
  57. func findCustomWebuiDir() string {
  58. dir := path.Join(cfg.GetDir(), "custom-webui")
  59. return dir
  60. }
  61. func setupCustomWebuiDir() {
  62. dir := 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. sv.Set("internal.themesdir", err.Error())
  67. } else {
  68. sv.Set("internal.themesdir", dir)
  69. }
  70. }
  71. func generateThemeCss(w http.ResponseWriter, r *http.Request) {
  72. themeCssFilename := path.Join(findCustomWebuiDir(), "themes", cfg.ThemeName, "theme.css")
  73. if !customThemeCssRead || cfg.ThemeCacheDisabled {
  74. customThemeCssRead = true
  75. if _, err := os.Stat(themeCssFilename); err == nil {
  76. customThemeCss, err = os.ReadFile(themeCssFilename)
  77. } else {
  78. log.Debugf("Theme CSS not read: %v", err)
  79. customThemeCss = []byte("/* not found */")
  80. }
  81. }
  82. w.Header().Add("Content-Type", "text/css")
  83. w.Write(customThemeCss)
  84. }
  85. func generateWebUISettings(w http.ResponseWriter, r *http.Request) {
  86. jsonRet, _ := json.Marshal(webUISettings{
  87. Rest: cfg.ExternalRestAddress + "/api/",
  88. ShowFooter: cfg.ShowFooter,
  89. ShowNavigation: cfg.ShowNavigation,
  90. ShowNewVersions: cfg.ShowNewVersions,
  91. AvailableVersion: installationinfo.Runtime.AvailableVersion,
  92. CurrentVersion: installationinfo.Build.Version,
  93. PageTitle: cfg.PageTitle,
  94. SectionNavigationStyle: cfg.SectionNavigationStyle,
  95. DefaultIconForBack: cfg.DefaultIconForBack,
  96. SshFoundKey: installationinfo.Runtime.SshFoundKey,
  97. SshFoundConfig: installationinfo.Runtime.SshFoundConfig,
  98. EnableCustomJs: cfg.EnableCustomJs,
  99. })
  100. w.Header().Add("Content-Type", "application/json")
  101. _, err := w.Write([]byte(jsonRet))
  102. if err != nil {
  103. log.Warnf("Could not write webui settings: %v", err)
  104. }
  105. }
  106. func startWebUIServer(cfg *config.Config) {
  107. log.WithFields(log.Fields{
  108. "address": cfg.ListenAddressWebUI,
  109. }).Info("Starting WebUI server")
  110. setupCustomWebuiDir()
  111. mux := http.NewServeMux()
  112. mux.Handle("/custom-webui/", http.StripPrefix("/custom-webui/", http.FileServer(http.Dir(findCustomWebuiDir()))))
  113. mux.HandleFunc("/theme.css", generateThemeCss)
  114. mux.HandleFunc("/webUiSettings.json", generateWebUISettings)
  115. webuiDir := findWebuiDir()
  116. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  117. dirName := path.Dir(r.URL.Path)
  118. // Mangle requests for any path like /logs or /config to load the webui index.html
  119. if path.Ext(r.URL.Path) == "" && r.URL.Path != "/" {
  120. log.Debugf("Mangling request for %s to /index.html", r.URL.Path)
  121. http.ServeFile(w, r, path.Join(webuiDir, "index.html"))
  122. } else {
  123. http.StripPrefix(dirName, http.FileServer(http.Dir(webuiDir))).ServeHTTP(w, r)
  124. }
  125. })
  126. srv := &http.Server{
  127. Addr: cfg.ListenAddressWebUI,
  128. Handler: mux,
  129. }
  130. log.Fatal(srv.ListenAndServe())
  131. }