webuiServer.go 2.0 KB

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