4
0

webuiServer.go 2.0 KB

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