forked from apex/up-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
44 lines (34 loc) · 766 Bytes
/
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
package main
import (
"html/template"
"log"
"net/http"
"os"
"github.com/gorilla/pat"
)
var views = template.Must(template.ParseGlob("templates/*.html"))
func main() {
addr := ":" + os.Getenv("PORT")
s := pat.New()
s.Get("/", index)
s.Post("/", submit)
log.Fatal(http.ListenAndServe(addr, s))
}
func index(w http.ResponseWriter, r *http.Request) {
name, _ := r.Cookie("name")
w.Header().Set("Content-Type", "text/html")
views.ExecuteTemplate(w, "index.html", struct {
Name *http.Cookie
}{
Name: name,
})
}
func submit(w http.ResponseWriter, r *http.Request) {
name := r.FormValue("name")
http.SetCookie(w, &http.Cookie{
Name: "name",
Value: name,
})
back := r.Header.Get("Referer")
http.Redirect(w, r, back, http.StatusFound)
}