-
Notifications
You must be signed in to change notification settings - Fork 10.6k
/
Copy pathmain.go
59 lines (50 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
// Example code for Chapter 4.2 from "Build Web Application with Golang"
// Purpose: Shows how to perform server-side validation of user input from a form.
// Also shows to use multiple template files with predefined template names.
// Run `go run main.go` and then access http://localhost:9090
package main
import (
"apps/ch.4.2/validator"
"html/template"
"log"
"net/http"
)
const (
PORT = "9090"
HOST_URL = "http://localhost:" + PORT
)
var t *template.Template
type Links struct {
BadLinks [][2]string
}
// invalid links to display for testing.
var links Links
func index(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, HOST_URL+"/profile", http.StatusTemporaryRedirect)
}
func profileHandler(w http.ResponseWriter, r *http.Request) {
t.ExecuteTemplate(w, "profile", links)
}
func checkProfile(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
p := validator.ProfilePage{&r.Form}
t.ExecuteTemplate(w, "submission", p.GetErrors())
}
// This function is called before main()
func init() {
// Note: we can reference the loaded templates by their defined name inside the template files.
t = template.Must(template.ParseFiles("profile.gtpl", "submission.gtpl"))
list := make([][2]string, 2)
list[0] = [2]string{HOST_URL + "/checkprofile", "No data"}
list[1] = [2]string{HOST_URL + "/checkprofile?age=1&gender=guy&shirtsize=big", "Invalid options"}
links = Links{list}
}
func main() {
http.HandleFunc("/", index)
http.HandleFunc("/profile", profileHandler)
http.HandleFunc("/checkprofile", checkProfile)
err := http.ListenAndServe(":"+PORT, nil) // setting listening port
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}