4
0

webuiServer.go 3.9 KB

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