4
0

webuiServer.go 3.3 KB

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