-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
48 lines (38 loc) · 1.11 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
package main
import (
"html/template"
"net/http"
"path"
)
var ROOT_DIR = path.Dir(".")
var TEMPLATES_DIR = path.Join(ROOT_DIR, "templates")
type Context struct {
Title string
}
func init() {
http.HandleFunc("/", homeHandler)
// Mandatory root-based resources
serveSingle("/favicon.ico", "images/favicon.ico")
// Normal resources
http.Handle("/static", http.FileServer(http.Dir("./static/")))
}
func main() {
http.ListenAndServe(":8080", nil)
}
func homeHandler(w http.ResponseWriter, r *http.Request) {
fp := path.Join(TEMPLATES_DIR, "home.html")
tmpl, err := template.ParseFiles(fp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
context := Context{Title:"Krystian Hanek website"}
if err := tmpl.Execute(w, context); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func serveSingle(pattern string, filename string) {
http.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, path.Join(TEMPLATES_DIR, filename))
})
}