Browse Source

watching file modification and reload data

Help-14 3 years ago
parent
commit
a4cbd26044
4 changed files with 59 additions and 9 deletions
  1. 5 0
      src/go.mod
  2. 4 0
      src/go.sum
  3. 49 9
      src/main.go
  4. 1 0
      src/themes/flame/index.html

+ 5 - 0
src/go.mod

@@ -3,3 +3,8 @@ module github.com/help-14/magma
 go 1.18
 
 require gopkg.in/yaml.v2 v2.4.0
+
+require (
+	github.com/fsnotify/fsnotify v1.5.1 // indirect
+	golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
+)

+ 4 - 0
src/go.sum

@@ -1,3 +1,7 @@
+github.com/fsnotify/fsnotify v1.5.1 h1:mZcQUHVQUQWoPXXtuf9yuEXKudkV2sx1E06UadKWpgI=
+github.com/fsnotify/fsnotify v1.5.1/go.mod h1:T3375wBYaZdLLcVNkcVbzGHY7f1l/uK5T5Ai1i3InKU=
+golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
+golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=

+ 49 - 9
src/main.go

@@ -9,6 +9,7 @@ import (
 	"path/filepath"
 	"time"
 
+	"github.com/fsnotify/fsnotify"
 	"github.com/help-14/magma/modules"
 )
 
@@ -22,17 +23,11 @@ var websiteData = struct {
 var webTemplate *template.Template
 
 func main() {
-	pwd, _ = os.Getwd()
-
-	dataPath := filepath.Join(pwd, "data")
-	if _, err := os.Stat(filepath.Join(dataPath, "data.yaml")); os.IsNotExist(err) {
-		log.Println("Copy files to data folder")
-		os.MkdirAll(dataPath, os.ModePerm)
-		modules.CopyDir(filepath.Join(pwd, "common"), dataPath)
-	}
+	prepare()
 	loadData()
+	go watchChanges()
 
-	commonfs := http.FileServer(http.Dir(dataPath))
+	commonfs := http.FileServer(http.Dir(filepath.Join(pwd, "data")))
 	http.Handle("/common/", http.StripPrefix("/common/", commonfs))
 
 	languagefs := http.FileServer(http.Dir(filepath.Join(pwd, "languages")))
@@ -51,6 +46,17 @@ func main() {
 	}
 }
 
+func prepare() {
+	pwd, _ = os.Getwd()
+
+	dataPath := filepath.Join(pwd, "data")
+	if _, err := os.Stat(filepath.Join(dataPath, "data.yaml")); os.IsNotExist(err) {
+		log.Println("Copy files to data folder")
+		os.MkdirAll(dataPath, os.ModePerm)
+		modules.CopyDir(filepath.Join(pwd, "common"), dataPath)
+	}
+}
+
 func loadData() {
 	appConfig = modules.LoadConfig()
 	websiteData.Config = appConfig.Website
@@ -61,6 +67,40 @@ func loadData() {
 	webTemplate = tmpl
 }
 
+func watchChanges() {
+	watcher, err := fsnotify.NewWatcher()
+	if err != nil {
+		log.Fatal(err)
+	}
+	defer watcher.Close()
+
+	done := make(chan bool)
+	go func() {
+		for {
+			select {
+			case event, ok := <-watcher.Events:
+				if !ok {
+					return
+				}
+				log.Println("Modified file:", event.Name)
+				loadData()
+			case err, ok := <-watcher.Errors:
+				if !ok {
+					return
+				}
+				log.Println("error:", err)
+			}
+		}
+	}()
+
+	err = watcher.Add(filepath.Join(pwd, "data", "data.yaml"))
+	err = watcher.Add(filepath.Join(pwd, "data", "config.yaml"))
+	if err != nil {
+		log.Fatal(err)
+	}
+	<-done
+}
+
 func serveTemplate(w http.ResponseWriter, r *http.Request) {
 	webTemplate.Execute(w, websiteData)
 }

+ 1 - 0
src/themes/flame/index.html

@@ -153,6 +153,7 @@
             // Set weather icon to canvas
             var skycons = new Skycons({ "color": window.cssRoot["--accentColor"] });
             skycons.add("weather-icon", icon);
+            skycons.play();
             // Set weather info
             if (window.config.useMetric) {
                 document.querySelector("#temp").innerText = Math.floor(weather.main.temp - 273.15) + "°C";