From 48c0e0bf4cfd0600f12534a798a133e3e32bf1b8 Mon Sep 17 00:00:00 2001 From: LordNature Date: Sat, 25 Jan 2020 01:46:51 -0600 Subject: [PATCH] generate.go: Handle compiling for index and vip - Moved compiling to single function in generate.go to improve footprint - Added index handler with a data pass-through to generatePage - Made generatePage take a "global" template variable and data interface - Improved error return on main.go --- generate.go | 44 +++++++++++++++++++++++++++++++++----------- main.go | 15 +++------------ 2 files changed, 36 insertions(+), 23 deletions(-) diff --git a/generate.go b/generate.go index 09f41b6..b71ebb5 100644 --- a/generate.go +++ b/generate.go @@ -1,31 +1,51 @@ package main import ( + "fmt" "html/template" + "io/ioutil" "os" + "os/exec" "strconv" "time" ) +// compile takes no arguments and goes through each page and compiles +// the respective html. +func compile() error { + templates := template.Must(template.ParseGlob("templates/*.html")) + // Template called year to insert year into each page + templates.New("year").Parse(strconv.Itoa(time.Now().Year())) -// generate takes a page in the format `{page}.html` and creates -// a compiled html file under the `build/` directory. If any failures -// happen, it alerts the user via stdout. -func generate(page string) error { - var templates = template.Must(template.ParseFiles( - "templates/head.html", - "templates/footer.html", - "templates/nav.html", - "templates/" + page)) + // Index Generator + uptime, _ := exec.Command("sh", "-c", "uptime -p").Output() + pi, _ := ioutil.ReadFile("assets/pi.txt") + data := struct { + Uptime []byte + Pi string + }{uptime, string(pi)} + if err := generatePage(templates, "index.html", data); err != nil { + return err + } - templates.New("year").Parse(strconv.Itoa(time.Now().Year())) + // VIP Generator + if err := generatePage(templates, "vip.html", nil); err != nil { + return err + } + + return nil +} +// generatePage takes a page in the format `{page}.html` and creates +// a compiled html file under the `build/` directory. If any failures +// happen, it alerts the user via stdout. +func generatePage(templates *template.Template, page string, data interface{}) error { file, err := os.Create("build/" + page) if err != nil { return err } - err = templates.ExecuteTemplate(file, page, nil) + err = templates.ExecuteTemplate(file, page, data) if err != nil { return err } @@ -33,6 +53,8 @@ func generate(page string) error { if err := file.Close(); err != nil { return err } + + fmt.Println("COMPILED: ", page) return nil } diff --git a/main.go b/main.go index 5735447..becc762 100644 --- a/main.go +++ b/main.go @@ -2,24 +2,15 @@ package main import ( "fmt" - "io/ioutil" "net/http" ) func main() { fmt.Println("COMPILING STATIC PAGES") - pages, err := ioutil.ReadDir("templates") - if err != nil { - panic(fmt.Errorf("FAILED TO READ DIR: %v", err)) + if err := compile(); err != nil { + panic(fmt.Errorf("COMPILE FAILED: %v", err)) } - // TO-DO: Implement a better way for generation - for _, p := range pages { - if err := generate(p.Name()); err != nil { - panic(fmt.Errorf("FAILED TO GENERATE: %v", err)) - } - fmt.Println(p.Name()) - } - + fmt.Println("COMPILED SUCCESSFULLY") fmt.Println("STARTING GALAXY SERVER") fileServer := http.FileServer(http.Dir("build")) http.Handle("/", http.StripPrefix("/", fileServer)) -- 2.45.2