-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
70 lines (59 loc) · 1.52 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
68
69
70
package main
import (
"html/template"
"log"
"net/http"
)
var tpl *template.Template
func init() {
tpl = template.Must(template.ParseGlob("./templates/*.gohtml"))
}
func main() {
http.HandleFunc("/", index)
http.HandleFunc("/contact", contact)
http.HandleFunc("/apply", apply)
//http.HandleFunc("/applyProcess", applyProcess)
http.HandleFunc("/about", about)
http.ListenAndServe(":8080", nil)
}
func errorHttp(w http.ResponseWriter, err error) {
if err != nil {
http.Error(w, "error occured", 500) //internal server error
log.Fatal(err) //or os.exit(2)
}
}
func index(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
err := tpl.ExecuteTemplate(w, "index.gohtml", nil)
errorHttp(w, err)
}
}
func contact(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
err := tpl.ExecuteTemplate(w, "contact.gohtml", nil)
errorHttp(w, err)
}
}
//func apply(w http.ResponseWriter, r *http.Request) {
// if r.Method == "GET" {
// err := tpl.ExecuteTemplate(w, "apply.gohtml", nil)
// errorHttp(w, err)
// }
//}
func apply(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost { // or "POST"
err := tpl.ExecuteTemplate(w, "apply-process.gohtml", nil)
errorHttp(w, err)
return
}
// if r.Method == http.MethodGet { //"GET"
err := tpl.ExecuteTemplate(w, "apply.gohtml", nil)
errorHttp(w, err)
// }
}
func about(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
err := tpl.ExecuteTemplate(w, "about.gohtml", nil)
errorHttp(w, err)
}
}