Last active
October 13, 2023 22:21
-
-
Save inhies/f5b0592b3dc8195aa5a3d551db96496d to your computer and use it in GitHub Desktop.
Revisions
-
inhies revised this gist
Nov 4, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -12,7 +12,7 @@ import ( ) var ( templateDir = "./webui/templates" staticDir = "./webui/static" reloadTemplates bool routes = map[string]http.HandlerFunc{ -
inhies revised this gist
Nov 4, 2016 . 1 changed file with 29 additions and 23 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,16 +2,17 @@ package main import ( "fmt" "html/template" "io/ioutil" "log" "net/http" "strings" "gopkg.in/fsnotify.v1" ) var ( templateDir = ".//templates" staticDir = "./webui/static" reloadTemplates bool routes = map[string]http.HandlerFunc{ @@ -41,39 +42,44 @@ func init() { go func() { for { select { case ev := <-watcher.Events: log.Println("event:", ev) reloadTemplates = true case err := <-watcher.Errors: log.Println("error:", err) } } }() err = watcher.Add(templateDir) if err != nil { fmt.Println(err) } } func parseTemplates() { t = template.New("templates") var allFiles []string files, err := ioutil.ReadDir(templateDir) if err != nil { fmt.Println("Error reading template dir") } for _, file := range files { filename := file.Name() if strings.HasSuffix(filename, ".html") { allFiles = append(allFiles, templateDir+"/"+filename) fmt.Println("Parsing", templateDir+"/"+filename) } } if err != nil { log.Fatal(err) } t = template.Must(template.ParseFiles(allFiles...)) } func main() { @@ -85,7 +91,7 @@ func main() { http.HandleFunc(route, function) } log.Fatal(http.ListenAndServe(":8080", nil)) } // AssetsHandler is a static file server that serves everything in -
inhies created this gist
Oct 18, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,107 @@ package main import ( "fmt" "github.com/howeyc/fsnotify" "html/template" "net/http" "os" "path/filepath" "strings" ) var ( templateDir = "./webui/templates" staticDir = "./webui/static" reloadTemplates bool routes = map[string]http.HandlerFunc{ "/": rootHandler, } t *template.Template cwd string pages = map[string]Page{ "index": indexPage{ Title: "Index", }, } ) type Page interface{} type indexPage struct { Title string } func init() { watcher, err := fsnotify.NewWatcher() if err != nil { fmt.Println(err) } // Process events go func() { for { select { case ev := <-watcher.Event: fmt.Println("event:", ev) reloadTemplates = true case err := <-watcher.Error: fmt.Println("error:", err) } } }() err = watcher.Watch(templateDir) if err != nil { fmt.Println(err) } } func parseTemplates() { t = template.New("templates") err := filepath.Walk(templateDir, func(path string, info os.FileInfo, err error) error { if err != nil { return err } if info.IsDir() || !strings.HasSuffix(path, ".html") { return nil // Ignore directories and other files } _, err = t.ParseFiles(path) // Parse the template return err }) if err != nil { fmt.Println(err) os.Exit(1) } } func main() { parseTemplates() cwd = "./" http.HandleFunc("/static/", assetsHandler) for route, function := range routes { http.HandleFunc(route, function) } http.ListenAndServe(":8080", nil) } // AssetsHandler is a static file server that serves everything in // static directory func assetsHandler(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, staticDir+r.URL.Path) } // RootHandler handles the "/" connections func rootHandler(w http.ResponseWriter, r *http.Request) { if reloadTemplates { parseTemplates() reloadTemplates = false } err := t.ExecuteTemplate(w, "index", pages["index"]) if err != nil { fmt.Println(err) } }