-
Notifications
You must be signed in to change notification settings - Fork 48
/
todo.go
56 lines (43 loc) · 970 Bytes
/
todo.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
package main
import (
"database/sql"
"go-echo-vue/handlers"
"github.com/labstack/echo"
_ "github.com/mattn/go-sqlite3"
)
func main() {
db := initDB("storage.db")
migrate(db)
e := echo.New()
e.File("/", "public/index.html")
e.GET("/tasks", handlers.GetTasks(db))
e.PUT("/tasks", handlers.PutTask(db))
e.DELETE("/tasks/:id", handlers.DeleteTask(db))
e.Start(":8000")
}
func initDB(filepath string) *sql.DB {
db, err := sql.Open("sqlite3", filepath)
// Here we check for any db errors then exit
if err != nil {
panic(err)
}
// If we don't get any errors but somehow still don't get a db connection
// we exit as well
if db == nil {
panic("db nil")
}
return db
}
func migrate(db *sql.DB) {
sql := `
CREATE TABLE IF NOT EXISTS tasks(
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
name VARCHAR NOT NULL
);
`
_, err := db.Exec(sql)
// Exit if something goes wrong with our SQL statement above
if err != nil {
panic(err)
}
}