-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
47 lines (37 loc) · 850 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
45
46
47
package main
import (
"github.com/go-martini/martini"
"github.com/martini-contrib/render"
_ "github.com/lib/pq"
"github.com/go-xorm/xorm"
)
func panicIf(err error) {
if err != nil {
panic(err)
}
}
func establishDbConnection() *xorm.Engine {
engine, err := xorm.NewEngine("postgres", "dbname=example_app_dev sslmode=disable")
panicIf(err)
return engine
}
type Products struct {
Id int32 `json:"id"`
Code string `json:"code"`
Name string `json:"name"`
}
func main() {
app := martini.Classic()
engine := establishDbConnection()
engine.Sync(new(Products))
app.Map(engine)
app.Use(render.Renderer())
// Routes
app.Get("/products", func(db *xorm.Engine, r render.Render) {
var products []Products
err := db.Find(&products)
panicIf(err)
r.JSON(200, products)
})
app.Run()
}