-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.go
71 lines (53 loc) · 1.21 KB
/
server.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
package main
import (
"github.com/go-martini/martini"
_ "github.com/go-sql-driver/mysql"
"github.com/jinzhu/gorm"
"github.com/martini-contrib/binding"
"github.com/martini-contrib/render"
)
var (
db gorm.DB
sqlConnection string
)
func main() {
var err error
sqlConnection = "doug:doug@tcp(127.0.0.1:3306)/martini-gorm?parseTime=True"
db, err = gorm.Open("mysql", sqlConnection)
if err != nil {
panic(err)
return
}
m := martini.Classic()
m.Use(render.Renderer())
m.Get("/", func(r render.Render) {
var retData struct {
Items []Item
}
db.Find(&retData.Items)
r.HTML(200, "index", retData)
})
m.Get("/item/add", func(r render.Render) {
var retData struct {
Item Item
}
r.HTML(200, "item_edit", retData)
})
m.Post("/item/save", binding.Bind(Item{}), func(r render.Render, i Item) {
db.Save(&i)
r.Redirect("/")
})
m.Get("/item/edit/:id", func(r render.Render, p martini.Params) {
var retData struct {
Item Item
}
db.Where("id = ?", p["id"]).Find(&retData.Item)
r.HTML(200, "item_edit", retData)
})
m.Get("/item/remove/:id", func(r render.Render, p martini.Params) {
var item Item
db.Where("id = ?", p["id"]).Delete(&item)
r.Redirect("/")
})
m.Run()
}