-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
61 lines (50 loc) · 1.4 KB
/
main.go
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
59
60
61
package main
import (
"log"
"net/http"
"os"
"text/template"
"time"
"github.com/William-Vandebelt/godemo"
"github.com/go-chi/chi"
"github.com/joho/godotenv"
)
func getTemplate(fm template.FuncMap, name string) *template.Template {
tpl := template.Must(template.New("").Funcs(fm).ParseFiles("templates/"+name+".tmpl.html",
"templates/header.tmpl.html", "templates/footer.tmpl.html"))
log.Printf("%s template returned\n", name)
return tpl
}
// HandleError : Func that sends back a 500 error code if there is an error
func handleError(w http.ResponseWriter, err error) {
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Println(err)
}
}
func routes() *chi.Mux {
router := chi.NewRouter()
router.Get("/", func(w http.ResponseWriter, r *http.Request) {
tpl := getTemplate(template.FuncMap{}, "index")
err := tpl.ExecuteTemplate(w, "index.tmpl.html", "Hello Message From Index.go")
handleError(w, err)
})
return router
}
func init() {
err := godotenv.Load()
if err != nil {
log.Printf("Error loading .env file: %q\n", err)
}
// Using new golang package from personal github
t := time.Now()
t = godemo.ConvertDate("UTC", t)
log.Printf("Current Time in UTC is: %v\n", t)
}
// Build with >> go build -o bin/goheroku -v .
func main() {
port := os.Getenv("PORT")
r := routes()
log.Printf("Golang App running...\n")
log.Fatal(http.ListenAndServe(":"+port, r))
}