From 248b90021b08bd91955c7c35ef808d3498285ba0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A1=9C=E8=8F=AF?= Date: Tue, 16 May 2023 01:51:41 +0800 Subject: [PATCH] user UpdatedAt --- main.go | 6 +++ models/Image.go | 29 +++++++------ models/Model.go | 27 ++++++------ models/Server.go | 19 ++++---- models/Tag.go | 9 ++-- models/Task.go | 18 ++++---- models/User.go | 15 ++++--- models/account.go | 13 +++--- models/session.go | 13 +++--- routers/account.go | 13 +++--- routers/dataset.go | 106 +++++++++++++++++++++++++++++++++++++++++++++ routers/images.go | 2 - routers/models.go | 4 -- routers/users.go | 36 ++++++++++----- 14 files changed, 219 insertions(+), 91 deletions(-) create mode 100644 routers/dataset.go diff --git a/main.go b/main.go index 8904760..3f18ccc 100644 --- a/main.go +++ b/main.go @@ -97,6 +97,12 @@ func main() { r.HandleFunc("/api/servers/{id}", routers.ServersItemPatch).Methods("PATCH") r.HandleFunc("/api/servers/{id}", routers.ServersItemDelete).Methods("DELETE") + r.HandleFunc("/api/datasets", routers.DatasetsGet).Methods("GET") + r.HandleFunc("/api/datasets", routers.DatasetsPost).Methods("POST") + r.HandleFunc("/api/datasets/{id}", routers.DatasetsItemGet).Methods("GET") + r.HandleFunc("/api/datasets/{id}", routers.DatasetsItemPatch).Methods("PATCH") + r.HandleFunc("/api/datasets/{id}", routers.DatasetsItemDelete).Methods("DELETE") + r.HandleFunc("/api/params/model", routers.ParamsModelsGet).Methods("GET") r.HandleFunc("/api/account", routers.AccountGet).Methods("GET") diff --git a/models/Image.go b/models/Image.go index 14c3b14..629daf0 100644 --- a/models/Image.go +++ b/models/Image.go @@ -2,23 +2,24 @@ package models import ( "main/configs" + "time" ) type Image struct { - ID int `json:"id" gorm:"primary_key"` - Name string `json:"name"` - Width int `json:"width"` - Height int `json:"height"` - Prompt string `json:"prompt"` - NegativePrompt string `json:"negative_prompt"` - NumInferenceSteps int `json:"num_inference_steps"` // Number of inference steps (minimum: 1; maximum: 500) - GuidanceScale float32 `json:"guidance_scale"` // Scale for classifier-free guidance (minimum: 1; maximum: 20) - Scheduler string `json:"scheduler"` // (DDIM|K_EULER|DPMSolverMultistep|K_EULER_ANCESTRAL|PNDM|KLMS) - Seed int `json:"seed"` // Random seed (minimum: 0; maximum: 2147483647) - FromImage string `json:"from_image"` // Image to start from - CreatedAt string `json:"created_at"` - UpdatedAt string `json:"updated_at"` - UserID int `json:"user_id"` + ID int `json:"id" gorm:"primary_key"` + Name string `json:"name"` + Width int `json:"width"` + Height int `json:"height"` + Prompt string `json:"prompt"` + NegativePrompt string `json:"negative_prompt"` + NumInferenceSteps int `json:"num_inference_steps"` // Number of inference steps (minimum: 1; maximum: 500) + GuidanceScale float32 `json:"guidance_scale"` // Scale for classifier-free guidance (minimum: 1; maximum: 20) + Scheduler string `json:"scheduler"` // (DDIM|K_EULER|DPMSolverMultistep|K_EULER_ANCESTRAL|PNDM|KLMS) + Seed int `json:"seed"` // Random seed (minimum: 0; maximum: 2147483647) + FromImage string `json:"from_image"` // Image to start from + UserID int `json:"user_id"` + CreatedAt time.Time `json:"created_at" gorm:"default:CURRENT_TIMESTAMP;autoCreateTime"` + UpdatedAt time.Time `json:"updated_at" gorm:"default:CURRENT_TIMESTAMP;autoUpdateTime"` } func init() { diff --git a/models/Model.go b/models/Model.go index fe6d106..01593cc 100644 --- a/models/Model.go +++ b/models/Model.go @@ -3,22 +3,23 @@ package models import ( "fmt" "main/configs" + "time" ) type Model struct { - ID int `json:"id" gorm:"primary_key"` - Name string `json:"name"` - Type string `json:"type"` // (lora|ckp|hyper|ti) - TriggerWords string `json:"trigger_words"` // 觸發詞 - BaseModel string `json:"base_model"` // (SD1.5|SD2) - ModelPath string `json:"model_path"` // 模型路徑 - Status string `json:"status" default:"initial"` // (initial|ready|waiting|running|success|error) - Progress int `json:"progress"` // (0-100) - Image string `json:"image"` // 封面圖片實際地址 - Tags string `json:"tags"` - CreatedAt string `json:"created_at"` - UpdatedAt string `json:"updated_at"` - UserID int `json:"user_id"` + ID int `json:"id" gorm:"primary_key"` + Name string `json:"name"` + Type string `json:"type"` // (lora|ckp|hyper|ti) + TriggerWords string `json:"trigger_words"` // 觸發詞 + BaseModel string `json:"base_model"` // (SD1.5|SD2) + ModelPath string `json:"model_path"` // 模型路徑 + Status string `json:"status" default:"initial"` // (initial|ready|waiting|running|success|error) + Progress int `json:"progress"` // (0-100) + Image string `json:"image"` // 封面圖片實際地址 + Tags string `json:"tags"` + UserID int `json:"user_id"` + CreatedAt time.Time `json:"created_at" gorm:"default:CURRENT_TIMESTAMP;autoCreateTime"` + UpdatedAt time.Time `json:"updated_at" gorm:"default:CURRENT_TIMESTAMP;autoUpdateTime"` } func init() { diff --git a/models/Server.go b/models/Server.go index 2ec8e56..43d0ce3 100644 --- a/models/Server.go +++ b/models/Server.go @@ -2,18 +2,19 @@ package models import ( "main/configs" + "time" ) type Server struct { - ID int `json:"id" gorm:"primary_key"` - Name string `json:"name"` - Type string `json:"type"` // (訓練|推理) - IP string `json:"ip"` - Port int `json:"port"` - Username string `json:"username"` - Password string `json:"password"` - CreatedAt string `json:"created_at"` - UpdatedAt string `json:"updated_at"` + ID int `json:"id" gorm:"primary_key"` + Name string `json:"name"` + Type string `json:"type"` // (訓練|推理) + IP string `json:"ip"` + Port int `json:"port"` + Username string `json:"username"` + Password string `json:"password"` + CreatedAt time.Time `json:"created_at" gorm:"default:CURRENT_TIMESTAMP;autoCreateTime"` + UpdatedAt time.Time `json:"updated_at" gorm:"default:CURRENT_TIMESTAMP;autoUpdateTime"` } func init() { diff --git a/models/Tag.go b/models/Tag.go index f3c6ffa..0e01c62 100644 --- a/models/Tag.go +++ b/models/Tag.go @@ -2,13 +2,14 @@ package models import ( "main/configs" + "time" ) type Tag struct { - ID int `json:"id" gorm:"primary_key"` - Name string `json:"name"` - CreatedAt string `json:"created_at"` - UpdatedAt string `json:"updated_at"` + ID int `json:"id" gorm:"primary_key"` + Name string `json:"name"` + CreatedAt time.Time `json:"created_at" gorm:"default:CURRENT_TIMESTAMP;autoCreateTime"` + UpdatedAt time.Time `json:"updated_at" gorm:"default:CURRENT_TIMESTAMP;autoUpdateTime"` } func init() { diff --git a/models/Task.go b/models/Task.go index ecc1848..d36c8bc 100644 --- a/models/Task.go +++ b/models/Task.go @@ -1,14 +1,16 @@ package models +import "time" + type Task struct { - ID int `json:"id"` - Name string `json:"name"` - Type string `json:"type"` // 任務類型(訓練|推理) - Status string `json:"status"` // (initial|ready|waiting|running|success|error) - Progress int `json:"progress"` // (0-100) - CreatedAt string `json:"created_at"` - UpdatedAt string `json:"updated_at"` - UserID int `json:"user_id"` + ID int `json:"id" gorm:"primary_key"` + Name string `json:"name"` + Type string `json:"type"` // 任務類型(訓練|推理) + Status string `json:"status"` // (initial|ready|waiting|running|success|error) + Progress int `json:"progress"` // (0-100) + UserID int `json:"user_id"` + CreatedAt time.Time `json:"created_at" gorm:"default:CURRENT_TIMESTAMP;autoCreateTime"` + UpdatedAt time.Time `json:"updated_at" gorm:"default:CURRENT_TIMESTAMP;autoUpdateTime"` } //// 推理任務 diff --git a/models/User.go b/models/User.go index 180a88b..cd40644 100644 --- a/models/User.go +++ b/models/User.go @@ -4,16 +4,17 @@ import ( "crypto/md5" "fmt" "main/configs" + "time" ) type User struct { - ID int `json:"id" gorm:"primary_key"` - Name string `json:"name"` - Email string `json:"email"` - CreatedAt string `json:"created_at"` - UpdatedAt string `json:"updated_at"` - Password string `json:"-"` - Slat string `json:"-"` + ID int `json:"id"` + Name string `json:"name"` + Email string `json:"email"` + Password string `json:"-"` + Slat string `json:"-"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` } func init() { diff --git a/models/account.go b/models/account.go index 40b04d0..8f6d63a 100644 --- a/models/account.go +++ b/models/account.go @@ -3,15 +3,16 @@ package models import ( "main/configs" "net/http" + "time" ) type Account struct { - ID int `json:"id"` - Name string `json:"name"` - Email string `json:"email"` - SessionID string `json:"session_id"` - CreatedAt string `json:"created_at"` - UpdatedAt string `json:"updated_at"` + ID int `json:"id"` + Name string `json:"name"` + Email string `json:"email"` + SessionID string `json:"session_id"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` } func AccountRead(w http.ResponseWriter, r *http.Request, cb func(account *Account)) { diff --git a/models/session.go b/models/session.go index e986e84..cd53ef0 100644 --- a/models/session.go +++ b/models/session.go @@ -2,15 +2,16 @@ package models import ( "main/configs" + "time" ) type Session struct { - ID string `json:"id" gorm:"primary_key"` - IP string `json:"ip"` - UserID int `json:"user_id"` - UserAgent string `json:"user_agent"` - CreatedAt string `json:"created_at"` - UpdatedAt string `json:"updated_at"` + ID string `json:"id" gorm:"primary_key"` + IP string `json:"ip"` + UserID int `json:"user_id"` + UserAgent string `json:"user_agent"` + CreatedAt time.Time `json:"created_at" gorm:"default:CURRENT_TIMESTAMP;autoCreateTime"` + UpdatedAt time.Time `json:"updated_at" gorm:"default:CURRENT_TIMESTAMP;autoUpdateTime"` } func init() { diff --git a/routers/account.go b/routers/account.go index 0ba5b20..02a45c3 100644 --- a/routers/account.go +++ b/routers/account.go @@ -6,17 +6,18 @@ import ( "main/models" "main/utils" "net/http" + "time" ) // 獲取當前賬戶信息(重寫, 爲輸出增加sid字段) func AccountGet(w http.ResponseWriter, r *http.Request) { var account struct { - ID int `json:"id"` - Name string `json:"name"` - Email string `json:"email"` - SessionID string `json:"session_id"` - CreatedAt string `json:"created_at"` - UpdatedAt string `json:"updated_at"` + ID int `json:"id"` + Name string `json:"name"` + Email string `json:"email"` + SessionID string `json:"session_id"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` } // 獲取Cookie diff --git a/routers/dataset.go b/routers/dataset.go new file mode 100644 index 0000000..5974fed --- /dev/null +++ b/routers/dataset.go @@ -0,0 +1,106 @@ +package routers + +import ( + "encoding/json" + "io/ioutil" + "main/configs" + "main/models" + "main/utils" + "net/http" + "time" + + "github.com/gorilla/mux" +) + +type Dataset struct { + ID int `json:"id" gorm:"primary_key"` + Name string `json:"name"` + Type string `json:"type"` + Info string `json:"info"` + CreatedAt time.Time `json:"created_at" gorm:"default:CURRENT_TIMESTAMP;autoCreateTime"` + UpdatedAt time.Time `json:"updated_at" gorm:"default:CURRENT_TIMESTAMP;autoUpdateTime"` +} + +func init() { + configs.ORMDB().AutoMigrate(&Dataset{}) +} + +func DatasetsGet(w http.ResponseWriter, r *http.Request) { + var listview models.ListView + listview.Page = utils.ParamInt(r.URL.Query().Get("page"), 1) + listview.PageSize = utils.ParamInt(r.URL.Query().Get("pageSize"), 10) + var dataset_list []Dataset + db := configs.ORMDB() + db.Offset((listview.Page - 1) * listview.PageSize).Limit(listview.PageSize).Find(&dataset_list) + for _, dataset := range dataset_list { + listview.List = append(listview.List, dataset) + } + db.Model(&Dataset{}).Count(&listview.Total) + listview.Next = listview.Page*listview.PageSize < int(listview.Total) + listview.WriteJSON(w) +} + +func DatasetsPost(w http.ResponseWriter, r *http.Request) { + dataset := Dataset{} + if err := configs.ORMDB().Create(&dataset).Error; err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte("500 - Internal Server Error")) + return + } + w.Header().Set("Content-Type", "application/json; charset=utf-8") + w.Write(utils.ToJSON(dataset)) +} + +// 獲取數據集 +func DatasetsItemGet(w http.ResponseWriter, r *http.Request) { + dataset := Dataset{ID: utils.ParamInt(mux.Vars(r)["dataset_id"], 0)} + if err := configs.ORMDB().Find(&dataset).Error; err != nil { + w.WriteHeader(http.StatusNotFound) + w.Write([]byte("404 - Not Found")) + return + } + w.Header().Set("Content-Type", "application/json; charset=utf-8") + w.Write(utils.ToJSON(dataset)) +} + +func DatasetsItemPatch(w http.ResponseWriter, r *http.Request) { + dataset := Dataset{ID: utils.ParamInt(mux.Vars(r)["dataset_id"], 0)} + // 取出更新数据 + var dataset_new Dataset + body, err := ioutil.ReadAll(r.Body) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte("500 - Internal Server Error")) + return + } + defer r.Body.Close() + if err = json.Unmarshal(body, &dataset_new); err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte("500 - Internal Server Error")) + return + } + + // 合併字段 + if dataset_new.Name != "" { + dataset.Name = dataset_new.Name + } + + // 執行更新 + if err := configs.ORMDB().Model(&dataset).Updates(dataset_new).Error; err != nil { + w.WriteHeader(http.StatusNotFound) + w.Write([]byte("404 - Not Found")) + return + } + w.Header().Set("Content-Type", "application/json; charset=utf-8") + w.Write(utils.ToJSON(dataset)) +} + +func DatasetsItemDelete(w http.ResponseWriter, r *http.Request) { + dataset := Dataset{ID: utils.ParamInt(mux.Vars(r)["dataset_id"], 0)} + if err := configs.ORMDB().Delete(&dataset).Error; err != nil { + w.WriteHeader(http.StatusNotFound) + w.Write([]byte("404 - Not Found")) + return + } + w.WriteHeader(http.StatusNoContent) +} diff --git a/routers/images.go b/routers/images.go index 7d24b47..de802c6 100644 --- a/routers/images.go +++ b/routers/images.go @@ -8,7 +8,6 @@ import ( "main/models" "main/utils" "net/http" - "time" "github.com/gorilla/mux" ) @@ -74,7 +73,6 @@ func ImagesItemPatch(w http.ResponseWriter, r *http.Request) { return } image.ID = utils.ParamInt(mux.Vars(r)["id"], 0) - image.UpdatedAt = time.Now().Format("2006-01-02 15:04:05") if err := configs.ORMDB().Model(&image).Updates(image).Error; err != nil { log.Println(err) return diff --git a/routers/models.go b/routers/models.go index e294823..0702655 100644 --- a/routers/models.go +++ b/routers/models.go @@ -10,7 +10,6 @@ import ( "main/utils" "net/http" "strconv" - "time" "github.com/gorilla/mux" "github.com/gorilla/websocket" @@ -144,9 +143,6 @@ func ModelItemPatch(w http.ResponseWriter, r *http.Request) { model.Image = model_new.Image } - // 更新時間 - model.UpdatedAt = time.Now().Format("2006-01-02 15:04:05") - // 執行更新 if err := configs.ORMDB().Save(&model).Error; err != nil { log.Println(err) diff --git a/routers/users.go b/routers/users.go index 70f936d..477fb4f 100644 --- a/routers/users.go +++ b/routers/users.go @@ -9,7 +9,6 @@ import ( "main/models" "main/utils" "net/http" - "time" "github.com/google/uuid" "github.com/gorilla/mux" @@ -57,13 +56,13 @@ func UsersPost(w http.ResponseWriter, r *http.Request) { // 創建用戶 var slat string = uuid.New().String() var user models.User = models.User{ - Name: form.Name, - Email: form.Email, - Password: fmt.Sprintf("%x", md5.Sum([]byte(form.Password+slat))), - Slat: slat, - CreatedAt: time.Now().Format("2006-01-02 15:04:05"), - UpdatedAt: time.Now().Format("2006-01-02 15:04:05"), + Name: form.Name, + Email: form.Email, + Password: fmt.Sprintf("%x", md5.Sum([]byte(form.Password+slat))), + Slat: slat, } + + // 寫入數據庫 if err := configs.ORMDB().Create(&user).Error; err != nil { fmt.Println(err) return @@ -84,21 +83,34 @@ func UsersItemGet(w http.ResponseWriter, r *http.Request) { // 更新用戶 func UsersItemPatch(w http.ResponseWriter, r *http.Request) { - user := models.User{ID: utils.ParamInt(mux.Vars(r)["id"], 0)} + var form struct { + Name string `json:"name"` + Email string `json:"email"` + Password string `json:"password"` + } body, err := ioutil.ReadAll(r.Body) if err != nil { fmt.Println(err) return } defer r.Body.Close() - if err = json.Unmarshal(body, &user); err != nil { + if err = json.Unmarshal(body, &form); err != nil { fmt.Println(err) return } - if err := configs.ORMDB().Save(&user).Error; err != nil { - fmt.Println(err) - return + user := models.User{ID: utils.ParamInt(mux.Vars(r)["id"], 0)} + configs.ORMDB().First(&user) + if form.Name != "" { + user.Name = form.Name } + if form.Email != "" { + user.Email = form.Email + } + if form.Password != "" { + user.Slat = uuid.New().String() + user.Password = fmt.Sprintf("%x", md5.Sum([]byte(form.Password+user.Slat))) + } + configs.ORMDB().Save(&user) w.Header().Set("Content-Type", "application/json; charset=utf-8") w.Write(utils.ToJSON(user)) }