webuiServer.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. config "github.com/OliveTin/OliveTin/internal/config"
  9. updatecheck "github.com/OliveTin/OliveTin/internal/updatecheck"
  10. )
  11. type webUISettings struct {
  12. Rest string
  13. ThemeName string
  14. ShowFooter bool
  15. ShowNavigation bool
  16. ShowNewVersions bool
  17. AvailableVersion string
  18. CurrentVersion string
  19. PageTitle string
  20. SectionNavigationStyle string
  21. }
  22. func findWebuiDir() string {
  23. directoriesToSearch := []string{
  24. cfg.WebUIDir,
  25. "../webui/",
  26. "/usr/share/OliveTin/webui/",
  27. "/var/www/OliveTin/",
  28. "/var/www/olivetin/",
  29. "/etc/OliveTin/webui/",
  30. }
  31. for _, dir := range directoriesToSearch {
  32. if _, err := os.Stat(dir); !os.IsNotExist(err) {
  33. log.WithFields(log.Fields{
  34. "dir": dir,
  35. }).Infof("Found the webui directory")
  36. return dir
  37. }
  38. }
  39. log.Warnf("Did not find the webui directory, you will probably get 404 errors.")
  40. return "./webui" // Should not exist
  41. }
  42. func generateWebUISettings(w http.ResponseWriter, r *http.Request) {
  43. jsonRet, _ := json.Marshal(webUISettings{
  44. Rest: cfg.ExternalRestAddress + "/api/",
  45. ThemeName: cfg.ThemeName,
  46. ShowFooter: cfg.ShowFooter,
  47. ShowNavigation: cfg.ShowNavigation,
  48. ShowNewVersions: cfg.ShowNewVersions,
  49. AvailableVersion: updatecheck.AvailableVersion,
  50. CurrentVersion: updatecheck.CurrentVersion,
  51. PageTitle: cfg.PageTitle,
  52. SectionNavigationStyle: cfg.SectionNavigationStyle,
  53. })
  54. _, err := w.Write([]byte(jsonRet))
  55. if err != nil {
  56. log.Warnf("Could not write webui settings: %v", err)
  57. }
  58. }
  59. func startWebUIServer(cfg *config.Config) {
  60. log.WithFields(log.Fields{
  61. "address": cfg.ListenAddressWebUI,
  62. }).Info("Starting WebUI server")
  63. mux := http.NewServeMux()
  64. mux.Handle("/", http.FileServer(http.Dir(findWebuiDir())))
  65. mux.HandleFunc("/webUiSettings.json", generateWebUISettings)
  66. srv := &http.Server{
  67. Addr: cfg.ListenAddressWebUI,
  68. Handler: mux,
  69. }
  70. log.Fatal(srv.ListenAndServe())
  71. }