webuiServer.go 1.9 KB

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