Files
ai/main.go
2023-08-13 05:48:21 +08:00

185 lines
7.8 KiB
Go

package main
import (
"log"
"net/http"
"runtime"
"time"
"main/routers"
"main/utils"
"github.com/gorilla/mux"
)
//// 獲取查詢參數(int64 類型)
//func ParamInt(value string, defaultValue int64) int64 {
// if value == "" {
// return defaultValue
// }
// if v, err := strconv.ParseInt(value, 10, 64); err == nil {
// return v
// }
// return defaultValue
//}
//
//func getlist(w http.ResponseWriter, r *http.Request) {
// listview := struct {
// Name string `json:"name"`
// Page int64 `json:"page"`
// PageSize int64 `json:"pageSize"`
// Total int64 `json:"total"`
// Next bool `json:"next"`
// List interface{} `json:"list"`
// }{
// Name: mux.Vars(r)["name"],
// Page: ParamInt(r.URL.Query().Get("page"), 1),
// PageSize: ParamInt(r.URL.Query().Get("pageSize"), 10),
// Total: 0,
// Next: false,
// List: make([]interface{}, 0),
// }
//
// // 选择对应的模型
// switch listview.Name {
// case "sessions":
// listview.List = []models.Session{}
// case "users":
// listview.List = []models.User{}
// case "models":
// listview.List = []models.Model{}
// case "images":
// listview.List = []models.Image{}
// case "tags":
// listview.List = []models.Tag{}
// case "servers":
// listview.List = []models.Server{}
// case "datasets":
// listview.List = []models.Dataset{}
// default:
// fmt.Fprintf(w, "404")
// return
// }
//
// // 从数据库中获取数据
// db := configs.ORMDB()
// //if task := r.URL.Query().Get("task"); task != "" {
// // db = db.Where("task = ?", task)
// //}
// //if status := r.URL.Query().Get("status"); status != "" {
// // db = db.Where("status = ?", status)
// //}
// //if user_id := r.URL.Query().Get("user_id"); user_id != "" {
// // db = db.Where("user_id = ?", user_id)
// //}
// //if model_id := r.URL.Query().Get("model_id"); model_id != "" {
// // db = db.Where("model_id = ?", model_id)
// //}
// //// 获取指定用户喜欢的对象(图像)
// //if like := r.URL.Query().Get("like"); like != "" {
// // list, _ := models.LikeImage.GetA(like)
// // db = db.Where("id in (?)", list)
// //}
// // 分页数据
// db = db.Offset(int((listview.Page - 1) * listview.PageSize)).Limit(int(listview.PageSize))
// db.Find(&listview.List).Count(&listview.Total)
//
// // 轉換爲JSON並返回
// data, _ := json.MarshalIndent(listview, "", " ")
// w.Header().Set("Content-Type", "application/json; charset=utf-8")
// w.Write(data)
//}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
log.SetFlags(log.Lshortfile | log.LstdFlags)
r := mux.NewRouter()
// 設定中間件
r.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer utils.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)
})
})
// 設定路由
r.HandleFunc("/api", routers.GetDocs).Methods("GET")
//r.HandleFunc("/api/{name}", getlist).Methods("GET")
r.HandleFunc("/api/sessions", routers.SessionsGet).Methods("GET")
r.HandleFunc("/api/sessions", routers.SessionsPost).Methods("POST")
r.HandleFunc("/api/sessions/{id}", routers.SessionsItemGet).Methods("GET")
r.HandleFunc("/api/sessions/{id}", routers.SessionsItemPatch).Methods("PATCH")
r.HandleFunc("/api/sessions/{id}", routers.SessionsItemDelete).Methods("DELETE")
r.HandleFunc("/api/users", routers.UsersGet).Methods("GET") // 获取用户列表
r.HandleFunc("/api/users", routers.UsersPost).Methods("POST") // 创建一条用户
r.HandleFunc("/api/users/{id}", routers.UsersItemGet).Methods("GET") // 获取一条用户
r.HandleFunc("/api/users/{id}", routers.UsersItemPatch).Methods("PATCH") // 更新一条用户
r.HandleFunc("/api/users/{id}", routers.UsersItemDelete).Methods("DELETE") // 删除一条用户
r.HandleFunc("/api/users/{id}/like", routers.UsersItemLike).Methods("POST") // 添加一条喜欢
r.HandleFunc("/api/users/{id}/like", routers.UsersItemUnlike).Methods("DELETE") // 移除一条喜欢
r.HandleFunc("/api/models", routers.ModelsGet).Methods("GET") // 获取模型列表
r.HandleFunc("/api/models", routers.ModelsPost).Methods("POST") // 创建一条模型
r.HandleFunc("/api/models/update", routers.ModelsUpdate).Methods("GET") // 更新模型列表
r.HandleFunc("/api/models/{id}", routers.ModelItemGet).Methods("GET") // 获取一条模型
r.HandleFunc("/api/models/{id}", routers.ModelItemPatch).Methods("PATCH") // 更新一条模型
r.HandleFunc("/api/models/{id}", routers.ModelItemDelete).Methods("DELETE") // 删除一条模型
r.HandleFunc("/api/models/{id}/like", routers.ModelsItemLike).Methods("POST") // 添加一条喜欢
r.HandleFunc("/api/models/{id}/like", routers.ModelsItemUnlike).Methods("DELETE") // 移除一条喜欢
r.HandleFunc("/api/models/{id}/preview/{filename}", routers.ModelsItemPreview).Methods("GET") // 获取预览图片
r.HandleFunc("/api/images", routers.ImagesGet).Methods("GET") // 获取图片列表
r.HandleFunc("/api/images", routers.ImagesPost).Methods("POST") // 创建一条图片
r.HandleFunc("/api/images/{id}", routers.ImagesItemGet).Methods("GET") // 获取一条图片
r.HandleFunc("/api/images/{id}", routers.ImagesItemPatch).Methods("PATCH") // 更新一条图片
r.HandleFunc("/api/images/{id}", routers.ImagesItemDelete).Methods("DELETE") // 删除一条图片
r.HandleFunc("/api/images/{id}/like", routers.ImagesItemLike).Methods("POST") // 添加一条喜欢
r.HandleFunc("/api/images/{id}/like", routers.ImagesItemUnlike).Methods("DELETE") // 移除一条喜欢
r.HandleFunc("/api/tags", routers.TagsGet).Methods("GET")
r.HandleFunc("/api/tags", routers.TagsPost).Methods("POST")
r.HandleFunc("/api/tags/{id}", routers.TagsItemGet).Methods("GET")
r.HandleFunc("/api/tags/{id}", routers.TagsItemPatch).Methods("PATCH")
r.HandleFunc("/api/tags/{id}", routers.TagsItemDelete).Methods("DELETE")
r.HandleFunc("/api/servers", routers.ServersGet).Methods("GET")
r.HandleFunc("/api/servers", routers.ServersPost).Methods("POST")
r.HandleFunc("/api/servers/{id}", routers.ServersItemGet).Methods("GET")
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", routers.ParamsListGet).Methods("GET")
r.HandleFunc("/api/params/model", routers.ParamsModelsGet).Methods("GET")
r.HandleFunc("/api/account", routers.AccountGet).Methods("GET")
r.HandleFunc("/api/ws", routers.WebSocket).Methods("GET")
r.HandleFunc("/img/{id}", routers.WebpGet).Methods("GET")
// 設定靜態資源 (前端) 位于dist目录下
r.PathPrefix("/images/").Handler(http.FileServer(http.Dir("./data/")))
r.PathPrefix("/").Handler(http.FileServer(http.Dir("./dist/")))
log.Println("Web Server is running on http://localhost:8080")
if err := http.ListenAndServe(":8080", r); err != nil {
log.Fatal(err)
}
}