-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathtodo.go
50 lines (41 loc) · 847 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
package main
import (
"database/sql"
"github.com/labstack/echo"
_ "github.com/mattn/go-sqlite3"
"github.com/skadimoolam/go-vue-todos/handlers"
)
func main() {
db := initDb("storage.db")
migrate(db)
e := echo.New()
e.Static("/", "public")
e.GET("/tasks", handlers.GetTasks(db))
e.POST("/task", handlers.PostTask(db))
e.PUT("/task", handlers.PutTask(db))
e.DELETE("/task/:id", handlers.DeleteTask(db))
e.Start(":8080")
}
func initDb(filepath string) *sql.DB {
db, err := sql.Open("sqlite3", filepath)
if err != nil {
panic(err)
}
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,
done INTEGER NOT NULL
);
`
_, err := db.Exec(sql)
if err != nil {
panic(err)
}
}