@@ 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
}
@@ 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))