4
0

webuiServer.go 1.8 KB

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