forked from yuheiomori/martini-guestbook
/
main.go
62 lines (54 loc) · 1.41 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
package main
import (
"html/template"
"net/http"
"time"
"github.com/go-martini/martini"
_ "github.com/go-sql-driver/mysql"
"github.com/martini-contrib/render"
)
type Greeting struct {
Id int64
Name string `sql:"size:100"`
Comment string `sql:"size:200"`
CreateAt int64
}
func main() {
m := martini.Classic()
// 静的ファイルの利用
m.Use(martini.Static("static"))
// テンプレート関数の登録
m.Use(render.Renderer(render.Options{
Directory: "templates", // Specify what path to load the templates from.
Extensions: []string{".tmpl", ".html"}, // Specify extensions to load for templates.
Funcs: []template.FuncMap{{
"nl2br": nl2br,
"htmlquote": htmlquote,
"str2html": str2html,
"dateformat": dateformat,
}},
}))
// データベース初期化
dbmap := initDb()
defer dbmap.Db.Close()
// トップページ
m.Get("/", func(render render.Render) {
var greetings []Greeting
_, err := dbmap.Select(&greetings, "select * from greetings order by CreateAt desc")
if err != nil {
panic(err)
}
render.HTML(200, "index", greetings)
})
// 投稿
m.Post("/post", func(w http.ResponseWriter, r *http.Request, render render.Render) {
greeting := Greeting{
Name: r.FormValue("name"),
Comment: r.FormValue("comment"),
CreateAt: time.Now().UnixNano(),
}
dbmap.Insert(&greeting)
render.Redirect("/", 302)
})
m.Run()
}