diff --git a/README.md b/README.md index fdf7af9..a00ce7f 100644 --- a/README.md +++ b/README.md @@ -17,13 +17,17 @@ TEST: - [x] 刪除數據集 - [x] 修改數據集 - [x] 查詢數據集 -- [x] GET [/api/servers](/api/servers) 服務器列表 - [x] GET [/api/models](/api/models) 模型列表 + - [x] 創建模型(訓練模型) + - [x] 刪除模型 + - [x] 修改模型 + - [x] 查詢模型 - [x] GET [/api/images](/api/images) 圖片列表 - [x] GET [/api/tasks](/api/tasks) 任務列表 - [x] GET [/api/tags](/api/tags) 標籤列表 - [x] GET [/api/params](/api/params) 參數列表 - [x] GET [/api/account](/api/account) 賬戶信息 +- [x] GET [/api/servers](/api/servers) 服務器列表 TEST: diff --git a/models/Server.go b/models/Server.go index 47549c6..1f3c787 100644 --- a/models/Server.go +++ b/models/Server.go @@ -11,6 +11,7 @@ type Server struct { Type string `json:"type"` // (訓練|推理) IP string `json:"ip"` Port int `json:"port"` + Status string `json:"status"` // (異常|初始化|就緒|工作中|關閉中) Username string `json:"username"` Password string `json:"password"` CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"` diff --git a/routers/datasets.go b/routers/datasets.go index ccd15ba..ac91efc 100644 --- a/routers/datasets.go +++ b/routers/datasets.go @@ -22,7 +22,7 @@ func (list ImageList) Value() (driver.Value, error) { return json.Marshal(list) } -// 數據集 +// 數據集模型 type Dataset struct { ID int `json:"id" gorm:"primary_key"` Name string `json:"name"` @@ -129,6 +129,7 @@ func DatasetsItemPatch(w http.ResponseWriter, r *http.Request) { }) } +// 刪除數據集 func DatasetsItemDelete(w http.ResponseWriter, r *http.Request) { models.AccountRead(w, r, func(account *models.Account) { // 獲取數據集 diff --git a/routers/images.go b/routers/images.go index de802c6..594dcc2 100644 --- a/routers/images.go +++ b/routers/images.go @@ -77,9 +77,6 @@ func ImagesItemPatch(w http.ResponseWriter, r *http.Request) { log.Println(err) return } - - //image.ID = utils.ParamInt(mux.Vars(r)["id"], 0) - //image.Update() w.Header().Set("Content-Type", "application/json; charset=utf-8") w.Write(utils.ToJSON(image)) } diff --git a/routers/models.go b/routers/models.go index 0702655..8d89f9f 100644 --- a/routers/models.go +++ b/routers/models.go @@ -54,6 +54,9 @@ func ModelsPost(w http.ResponseWriter, r *http.Request) { log.Println(err) return } + // 直接提交訓練任務 + go model.Train() + // 返回創建的模型 w.Header().Set("Content-Type", "application/json; charset=utf-8") w.Write(utils.ToJSON(model)) }) diff --git a/routers/servers.go b/routers/servers.go index ed15c1e..4b19044 100644 --- a/routers/servers.go +++ b/routers/servers.go @@ -1,11 +1,15 @@ package routers import ( + "encoding/json" + "fmt" + "io/ioutil" "main/configs" "main/models" "main/utils" "net/http" + "github.com/google/uuid" "github.com/gorilla/mux" ) @@ -17,6 +21,13 @@ func ServersGet(w http.ResponseWriter, r *http.Request) { db := configs.ORMDB() db.Offset((listview.Page - 1) * listview.PageSize).Limit(listview.PageSize).Find(&server_list) for _, server := range server_list { + // 驗證服務器狀態 + resp, err := http.Get(fmt.Sprintf("http://%s:%d/docs", server.IP, server.Port)) + if err != nil || resp.StatusCode != http.StatusOK { + server.Status = "異常" + } else { + server.Status = "正常" + } listview.List = append(listview.List, server) } listview.Next = listview.Page*listview.PageSize < int(listview.Total) @@ -25,6 +36,41 @@ func ServersGet(w http.ResponseWriter, r *http.Request) { func ServersPost(w http.ResponseWriter, r *http.Request) { var server models.Server + + // 獲取參數 + body, _ := ioutil.ReadAll(r.Body) + defer r.Body.Close() + if err := json.Unmarshal(body, &server); err != nil { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte(err.Error())) + return + } + + // 如果不指定類型,禁止創建服務器, 必須指定類型:訓練|推理 + if server.Type != "訓練" && server.Type != "推理" { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte("必須指定類型:訓練|推理")) + return + } + + // 如果不指定名稱,則使用uuid生成隨機名稱 + if server.Name == "" { + server.Name = uuid.New().String() + } + + // 如果不指定 port,則使用默認 port + if server.Port == 0 { + server.Port = 7860 + } + + // 如果不指定IP,則自動創建新服務器 + if server.IP == "" { + // TODO: 創建新服務器 + w.Header().Set("Content-Type", "application/json; charset=utf-8") + w.Write(utils.ToJSON(server)) + return + } + configs.ORMDB().Create(&server) w.Header().Set("Content-Type", "application/json; charset=utf-8") w.Write(utils.ToJSON(server))