This commit is contained in:
2023-04-18 15:22:01 +08:00
parent 2187b7fdbb
commit 62f2e55ffa
8 changed files with 143 additions and 0 deletions

5
go.mod Normal file
View File

@@ -0,0 +1,5 @@
module main
go 1.18
require github.com/mattn/go-sqlite3 v1.14.16

2
go.sum Normal file
View File

@@ -0,0 +1,2 @@
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=

15
main.go
View File

@@ -25,6 +25,21 @@ func main() {
}) })
http.HandleFunc("/tasks", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/tasks", func(w http.ResponseWriter, r *http.Request) {
defer LogComponent(time.Now().UnixNano(), r) // 最后打印日志 defer LogComponent(time.Now().UnixNano(), r) // 最后打印日志
// 查詢一組任務
// tasks, err := models.GetTasks()
// if err != nil {
// log.Println(err)
// return
// }
// fmt.Println(tasks)
// 將取得的任務裝載到ListView中
// listView := ListView{}
// for _, task := range tasks {
// listView.Tasks = append(listView.Tasks, task)
// }
fmt.Fprintf(w, "Hello, World!") fmt.Fprintf(w, "Hello, World!")
}) })
http.HandleFunc("/users", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/users", func(w http.ResponseWriter, r *http.Request) {

24
models/Image.go Normal file
View File

@@ -0,0 +1,24 @@
package models
import (
"database/sql"
"log"
_ "github.com/mattn/go-sqlite3"
)
type Image struct {
ID string `json:"id"`
Name string `json:"name"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
// 使用SQLite3初始化數據庫
func init() {
db, err := sql.Open("sqlite3", "image.db")
if err != nil {
log.Fatal(err)
}
defer db.Close()
}

24
models/Model.go Normal file
View File

@@ -0,0 +1,24 @@
package models
import (
"database/sql"
"log"
_ "github.com/mattn/go-sqlite3"
)
type Model struct {
ID string `json:"id"`
Name string `json:"name"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
// 使用SQLite3初始化數據庫
func init() {
db, err := sql.Open("sqlite3", "model.db")
if err != nil {
log.Fatal(err)
}
defer db.Close()
}

24
models/Tag.go Normal file
View File

@@ -0,0 +1,24 @@
package models
import (
"database/sql"
"log"
_ "github.com/mattn/go-sqlite3"
)
type Tag struct {
ID string `json:"id"`
Name string `json:"name"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
// 使用SQLite3初始化數據庫
func init() {
db, err := sql.Open("sqlite3", "tag.db")
if err != nil {
log.Fatal(err)
}
defer db.Close()
}

24
models/Task.go Normal file
View File

@@ -0,0 +1,24 @@
package models
import (
"database/sql"
"log"
_ "github.com/mattn/go-sqlite3"
)
type Task struct {
ID string `json:"id"`
Type string `json:"type"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
// 使用SQLite3初始化數據庫
func init() {
db, err := sql.Open("sqlite3", "task.db")
if err != nil {
log.Fatal(err)
}
defer db.Close()
}

25
models/User.go Normal file
View File

@@ -0,0 +1,25 @@
package models
import (
"database/sql"
"log"
_ "github.com/mattn/go-sqlite3"
)
type User struct {
ID string `json:"id"`
UserName string `json:"username"`
Password string `json:"password"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
// 使用SQLite3初始化數據庫
func init() {
db, err := sql.Open("sqlite3", "user.db")
if err != nil {
log.Fatal(err)
}
defer db.Close()
}