-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
67 lines (55 loc) · 1.64 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
62
63
64
65
66
67
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"github.com/gorilla/mux"
)
var homeTemplate *template.Template
var contactsTemplate *template.Template
var faqTemplate *template.Template
func homeHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
if err := homeTemplate.Execute(w, nil); err != nil {
panic(err)
}
}
func contactsHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
if err := contactsTemplate.Execute(w, nil); err != nil {
panic(err)
}
}
func faqHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
if err := faqTemplate.Execute(w, nil); err != nil {
panic(err)
}
}
func notFoundHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(http.StatusNotFound)
fmt.Fprint(w, "<h1>We are sorry! No such page found :(</h1>")
}
func main() {
var router = mux.NewRouter()
var err error
homeTemplate, err = template.ParseFiles("views/home.gohtml")
if homeTemplate, err = template.ParseFiles("views/home.gohtml"); err != nil {
panic(err)
}
if contactsTemplate, err = template.ParseFiles("views/contacts.gohtml"); err != nil {
panic(err)
}
if faqTemplate, err = template.ParseFiles("views/faq.gohtml"); err != nil {
panic(err)
}
//handler for NotFound request
router.NotFoundHandler = http.HandlerFunc(notFoundHandler)
//URL paths and corresponding handlers
router.HandleFunc("/", homeHandler)
router.HandleFunc("/contacts", contactsHandler)
router.HandleFunc("/faq", faqHandler)
log.Fatal(http.ListenAndServe(":3000", router))
}