summaryrefslogtreecommitdiff
path: root/pages/local.go
blob: ccd0f41750b4f0a6553c4f733e74b55928bf7da6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package pages

import (
	"fmt"
	"github.com/spf13/pflag"
	"gopkg.in/fsnotify/fsnotify.v1"
	"html/template"
	"os"
)

var Local = pflag.BoolP("local", "l", false, "use local files instead of embedded")

var Templates = map[string]func(content string){
	"pages/create.gohtml": func(content string) {
		template.Must(tmpl.New("create").Parse(content))
	},
	"pages/landing.gohtml": func(content string) {
		template.Must(tmpl.New("landing").Parse(content))
	},
	"pages/style.gohtml": func(content string) {
		template.Must(tmpl.Parse(content))
	},
}

func SetupLocal() {
	if *Local {

		watcher, err := fsnotify.NewWatcher()
		if err != nil {
			panic(err)
		}
		for page := range Templates {
			watcher.Add(page)
		}

		go func() {
			for {
				select {
				case ev := <-watcher.Events:
					newContent, err := os.ReadFile(ev.Name)
					if err != nil {
						fmt.Printf("error: %vn", err)
						continue
					}
					Templates[ev.Name](string(newContent))
					if err != nil {
						fmt.Printf("error: %vn", err)
					} else {
						fmt.Printf("updated: %vn", ev.Name)

					}

				}
			}
		}()

	}
}