forked from kataras/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
113 lines (94 loc) · 2.67 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"github.com/kataras/iris/v12"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
)
/*
go get -u github.com/mattn/go-sqlite3
go get -u github.com/jmoiron/sqlx
If you're on win64 and you can't install go-sqlite3:
1. Download: https://sourceforge.net/projects/mingw-w64/files/latest/download
2. Select "x86_x64" and "posix"
3. Add C:\Program Files\mingw-w64\x86_64-7.1.0-posix-seh-rt_v5-rev1\mingw64\bin
to your PATH env variable.
Docs: https://github.com/jmoiron/sqlx
*/
// Person is our person table structure.
type Person struct {
ID int64 `db:"person_id"`
FirstName string `db:"first_name"`
LastName string `db:"last_name"`
Email string
}
const schema = `
CREATE TABLE IF NOT EXISTS person (
person_id INTEGER PRIMARY KEY,
first_name text,
last_name text,
email text
);`
func main() {
app := iris.New()
db, err := sqlx.Connect("sqlite3", "./test.db")
if err != nil {
app.Logger().Fatalf("db failed to initialized: %v", err)
}
iris.RegisterOnInterrupt(func() {
db.Close()
})
db.MustExec(schema)
app.Get("/insert", func(ctx iris.Context) {
res, err := db.NamedExec(`INSERT INTO person (first_name,last_name,email) VALUES (:first,:last,:email)`,
map[string]interface{}{
"first": "John",
"last": "Doe",
"email": "[email protected]",
})
if err != nil {
// Note: on production, don't give the error back to the user.
// However for the sake of the example we do:
ctx.StopWithError(iris.StatusInternalServerError, err)
return
}
id, err := res.LastInsertId()
if err != nil {
ctx.StopWithError(iris.StatusInternalServerError, err)
return
}
ctx.Writef("person inserted: id: %d", id)
})
app.Get("/get", func(ctx iris.Context) {
// Select all persons.
people := []Person{}
db.Select(&people, "SELECT * FROM person ORDER BY first_name ASC")
if err != nil {
ctx.StopWithError(iris.StatusInternalServerError, err)
return
}
if len(people) == 0 {
ctx.Writef("no persons found, use /insert first.")
return
}
ctx.Writef("persons found: %#v", people)
/* Select a single or more with a first name of John from the database:
person := Person{FirstName: "John"}
rows, err := db.NamedQuery(`SELECT * FROM person WHERE first_name=:first_name`, person)
if err != nil { ... }
defer rows.Close()
for rows.Next() {
if err := rows.StructScan(&person); err != nil {
if err == sql.ErrNoRows {
ctx.StopWithText(iris.StatusNotFound, "Person: %s not found", person.FirstName)
} else {
ctx.StopWithError(iris.StatusInternalServerError, err)
}
return
}
}
*/
})
// http://localhost:8080/insert
// http://localhost:8080/get
app.Listen(":8080")
}