package main
import (
"html/template"
"net/http"
"os"
)
func main() {
// this code below makes a file server and serves everything as a file
// fs := http.FileServer(http.Dir(""))
// http.Handle("/", fs)
// serve everything in the css folder, the img folder and mp3 folder as a file
http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("css"))))
// when navigating to /home it should serve the home page
http.HandleFunc("/", Home)
http.HandleFunc("/rewards", Rewards)
http.HandleFunc("/table", Rewards)
http.ListenAndServe(getPort(), nil)
}
// Detect $PORT and if present uses it for listen and serve else defaults to :8080
// This is so that app can run on Heroku
func getPort() string {
p := os.Getenv("PORT")
if p != "" {
return ":" + p
}
return ":8080"
}
func render(w http.ResponseWriter, tmpl string, data interface{}) {
//parse the template file held in the templates folder
t, err := template.ParseFiles("templates/"+tmpl, "templates/includes.html")
if err != nil {
panic(err)
}
// render the website and pass data to the templating engine
err = t.Execute(w, data)
if err != nil {
panic(err)
}
}