This commit is contained in:
2023-04-28 14:23:19 +08:00
parent d911d7eb87
commit bb58879595
8 changed files with 63 additions and 88 deletions

View File

@@ -63,6 +63,7 @@ type Model struct {
ModelPath string `json:"model_path"` // 模型路徑 ModelPath string `json:"model_path"` // 模型路徑
Status string `json:"status"` // (waiting|running|success|error) Status string `json:"status"` // (waiting|running|success|error)
Progress int `json:"progress"` // (0-100) Progress int `json:"progress"` // (0-100)
Image string `json:"image"` // 封面圖片實際地址
Tags string `json:"tags"` Tags string `json:"tags"`
CreatedAt string `json:"created_at"` CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"` UpdatedAt string `json:"updated_at"`
@@ -249,10 +250,12 @@ GET /api/models
Method | URL | Info Method | URL | Info
-------|--------------------------------|------------------ -------|--------------------------------|------------------
GET | /api/models | 獲取所有模型
GET | /api/models?user=1234 | 僅取指定用戶的(按用戶ID過濾) GET | /api/models?user=1234 | 僅取指定用戶的(按用戶ID過濾)
GET | /api/models?tag=xxx | 按標籤分類篩選 GET | /api/models?tag=xxx | 按標籤分類篩選
GET | /api/models?public=true | 僅取公開的 GET | /api/models?public=true | 僅取公開的
GET | /api/models?public=false | 僅取私有的 GET | /api/models?public=false | 僅取私有的
POST | /api/models | 創建一個模型
* 如果未登錄, 返回的結果將過濾掉無權限查看的內容 * 如果未登錄, 返回的結果將過濾掉無權限查看的內容
* 如果已登錄, 返回的結果將包含私有的 * 如果已登錄, 返回的結果將包含私有的

38
configs/mysql.go Normal file
View File

@@ -0,0 +1,38 @@
package configs
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
"encoding/json"
"io/ioutil"
)
// 數據庫配置
type DBConfig struct {
Type string `json:"type"`
Host string `json:"host"`
Port int `json:"port"`
UserName string `json:"username"`
Password string `json:"password"`
Database string `json:"database"`
}
var conf DBConfig
func init() {
data, err := ioutil.ReadFile("./data/config.json")
if err != nil {
fmt.Println(err)
}
err = json.Unmarshal(data, &conf)
if err != nil {
fmt.Println(err)
}
}
func GetMySQL() (*sql.DB, error) {
return sql.Open(conf.Type, conf.UserName+":"+conf.Password+"@/"+conf.Database)
}

View File

@@ -52,6 +52,7 @@ func init() {
model_path TEXT, model_path TEXT,
status TEXT, status TEXT,
progress INTEGER, progress INTEGER,
image TEXT,
tags TEXT, tags TEXT,
created_at TEXT, created_at TEXT,
updated_at TEXT, updated_at TEXT,

1
go.mod
View File

@@ -3,6 +3,7 @@ module main
go 1.18 go 1.18
require ( require (
github.com/go-sql-driver/mysql v1.7.1
github.com/gorilla/mux v1.8.0 github.com/gorilla/mux v1.8.0
github.com/gorilla/websocket v1.5.0 github.com/gorilla/websocket v1.5.0
github.com/mattn/go-sqlite3 v1.14.16 github.com/mattn/go-sqlite3 v1.14.16

2
go.sum
View File

@@ -1,3 +1,5 @@
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=

35
main.go
View File

@@ -21,7 +21,23 @@ func main() {
runtime.GOMAXPROCS(runtime.NumCPU()) runtime.GOMAXPROCS(runtime.NumCPU())
r := mux.NewRouter() r := mux.NewRouter()
r.Use(middleware)
// 設定中間件
r.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer LogComponent(time.Now().UnixNano(), r) // 最后打印日志
w.Header().Set("Access-Control-Allow-Origin", "*") // 處理跨域請求
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization, X-Requested-With")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS")
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK) // 處理OPTIONS請求
return
}
next.ServeHTTP(w, r)
})
})
// 設定路由
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
t, _ := template.ParseFiles("templates/index.html") t, _ := template.ParseFiles("templates/index.html")
t.Execute(w, nil) t.Execute(w, nil)
@@ -308,23 +324,6 @@ func tasks_item_delete(w http.ResponseWriter, r *http.Request) {
w.Write(ToJSON(task)) w.Write(ToJSON(task))
} }
// 中間件, 通用
func middleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer LogComponent(time.Now().UnixNano(), r) // 最后打印日志
// 處理跨域請求
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization, X-Requested-With")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS")
// 處理OPTIONS請求
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}
next.ServeHTTP(w, r)
})
}
func ToJSON(object interface{}) []byte { func ToJSON(object interface{}) []byte {
json, err := json.MarshalIndent(object, "", " ") json, err := json.MarshalIndent(object, "", " ")
if err != nil { if err != nil {

View File

@@ -29,73 +29,3 @@ func (listview *ListView) ToJSON() []byte {
} }
return b return b
} }
//// 查詢數據(分頁, 每頁數量, 排序, 條件, 關聯, 獲取總數)
//func (listview *ListView) Read(name string, page int64, page_size int64) error {
//
// // 獲取數據庫連接
// db, err := configs.GetDB()
// if err != nil {
// log.Println(err)
// return err
// }
//
// // 獲取總數
// var total int64
// err = db.QueryRow("SELECT COUNT(*) FROM " + name).Scan(&total)
// if err != nil {
// log.Println(err)
// return err
// }
//
// // 獲取數據
// rows, err := db.Query("SELECT * FROM "+name+" LIMIT ?, ?", (page-1)*page_size, page_size)
// if err != nil {
// log.Println(err)
// return err
// }
// defer rows.Close()
//
// // 將數據轉換成對象
// switch name {
// case "images":
// for rows.Next() {
// var image Image
// err = rows.Scan(&image.ID, &image.Name, &image.CreatedAt, &image.UpdatedAt)
// if err != nil {
// log.Println(err)
// return err
// }
// listview.List = append(listview.List, image)
// }
// case "models":
// for rows.Next() {
// var model Model
// err = rows.Scan(&model.ID, &model.Name, &model.CreatedAt, &model.UpdatedAt)
// if err != nil {
// log.Println(err)
// return err
// }
// listview.List = append(listview.List, model)
// }
// default:
// for rows.Next() {
// var object Object
// err = rows.Scan(&object.ID, &object.Name, &object.CreatedAt, &object.UpdatedAt)
// if err != nil {
// log.Println(err)
// return err
// }
// listview.List = append(listview.List, object)
// }
// }
//
// // 設置返回值
// listview.Code = 0
// listview.Page = page
// listview.PageSize = page_size
// listview.Total = total
// listview.Next = total > page*page_size
//
// return nil
//}

View File

@@ -14,6 +14,7 @@ type Model struct {
ModelPath string `json:"model_path"` // 模型路徑 ModelPath string `json:"model_path"` // 模型路徑
Status string `json:"status"` // (waiting|running|success|error) Status string `json:"status"` // (waiting|running|success|error)
Progress int `json:"progress"` // (0-100) Progress int `json:"progress"` // (0-100)
Image string `json:"image"` // 封面圖片實際地址
Tags string `json:"tags"` Tags string `json:"tags"`
CreatedAt string `json:"created_at"` CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"` UpdatedAt string `json:"updated_at"`