webuiServer.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package staticWebserverForUi
  2. import (
  3. "net/http"
  4. "encoding/json"
  5. log "github.com/sirupsen/logrus"
  6. cors "github.com/jamesread/OliveTin/pkg/cors"
  7. "os"
  8. )
  9. type WebUiSettings struct {
  10. Rest string
  11. }
  12. func findWebuiDir() string {
  13. directoriesToSearch := []string{
  14. "./webui",
  15. "/var/www/olivetin/",
  16. }
  17. for _, dir := range directoriesToSearch {
  18. if _, err := os.Stat(dir); !os.IsNotExist(err) {
  19. log.Infof("Found the webui directory here: %v", dir)
  20. return dir
  21. }
  22. }
  23. log.Warnf("Did not find the webui directory, you will probably get 404 errors.")
  24. return "./webui" // Should not exist
  25. }
  26. func Start(listenAddress string, listenAddressRest string) {
  27. http.Handle("/", cors.AllowCors(http.FileServer(http.Dir(findWebuiDir()))))
  28. http.HandleFunc("/webUiSettings.json", func(w http.ResponseWriter, r *http.Request) {
  29. ret := WebUiSettings {
  30. Rest: "http://" + listenAddressRest + "/",
  31. }
  32. jsonRet, _ := json.Marshal(ret)
  33. w.Write([]byte(jsonRet));
  34. })
  35. log.Fatal(http.ListenAndServe(listenAddress, nil));
  36. }