/
main.go
89 lines (73 loc) · 2 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"bytes"
"net/http"
"encoding/gob"
"github.com/flosch/pongo2"
"github.com/gorilla/sessions"
"github.com/k0kubun/pp"
"github.com/sheercat/fillinform"
"github.com/zenazn/goji"
"github.com/zenazn/goji/web"
"github.com/zenazn/goji/web/middleware"
)
var store = sessions.NewCookieStore([]byte("hogehogehogehogehoge"))
func inquiry(c web.C, w http.ResponseWriter, r *http.Request) {
if tpl, err := pongo2.DefaultSet.FromFile("inquiry.tpl"); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
} else {
session, _ := store.Get(r, "inqury")
message := ""
msg := session.Values["message"]
_, ok := msg.(string)
if ok {
message = msg.(string)
session.Values["message"] = nil
session.Save(r, w)
}
bytes, err := tpl.ExecuteBytes(pongo2.Context{"title": "inquiry", "message": message})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
formData := session.Values["formData"]
if formData != nil {
pp.Println(formData)
bytes = fillinform.Fill(bytes, formData.(map[string][]string), nil)
session.Values["formData"] = nil
if err := session.Save(r, w); err != nil {
pp.Println(err)
}
}
w.Write(bytes)
}
}
func sendInquiry(c web.C, w http.ResponseWriter, r *http.Request) {
formData := make(map[string][]string)
if err := r.ParseForm(); err != nil {
panic("parse error")
} else {
formData = r.PostForm
}
pp.Println(formData)
session, _ := store.Get(r, "inqury")
session.Values["message"] = "send inquery"
buf := new(bytes.Buffer)
gob.Register(map[string][]string{})
enc := gob.NewEncoder(buf)
if err := enc.Encode(&formData); err != nil {
pp.Println(err)
}
session.Values["formData"] = formData
if err := session.Save(r, w); err != nil {
pp.Println(err)
}
http.Redirect(w, r, "/", http.StatusFound)
}
func main() {
pongo2.DefaultLoader.SetBaseDir("view")
goji.Use(middleware.Recoverer)
goji.Use(middleware.NoCache)
goji.Get("/", inquiry)
goji.Post("/", sendInquiry)
goji.Serve()
}