package main import ( "fmt" "log" "net/http" "runtime" "time" "main/routers" "main/utils" "github.com/gorilla/mux" ) 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/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/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("/img/{id}", routers.WebpGet).Methods("GET") // 設定靜態資源 (前端) 位于dist目录下, 并且为 图片和 js/css 设置缓存时间为7天 cacheTime := 7 * 24 * time.Hour r.PathPrefix("/images/").Handler(http.StripPrefix("/images/", http.FileServer(http.Dir("./data/")))).Headers("Cache-Control", fmt.Sprintf("max-age=%d", int(cacheTime.Seconds()))).Methods("GET") r.PathPrefix("/static/").Handler(http.StripPrefix("/", http.FileServer(http.Dir("./dist/static/")))).Headers("Cache-Control", fmt.Sprintf("max-age=%d", int(cacheTime.Seconds()))).Methods("GET") r.PathPrefix("/favicon.ico").Handler(http.StripPrefix("/", http.FileServer(http.Dir("./dist/favicon.ico")))).Headers("Cache-Control", fmt.Sprintf("max-age=%d", int(cacheTime.Seconds()))).Methods("GET") r.PathPrefix("/").Handler(http.StripPrefix("/", http.FileServer(http.Dir("./dist/")))).Methods("GET") log.Println("Web Server is running on http://localhost:8080") if err := http.ListenAndServe(":8080", r); err != nil { log.Fatal(err) } }