package main
import (
"io/ioutil"
"log"
"net/http"
"runtime"
"time"
"main/routers"
"main/utils"
"github.com/gorilla/mux"
"github.com/russross/blackfriday"
)
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("/", func(w http.ResponseWriter, r *http.Request) {
input, err := ioutil.ReadFile("./README.md")
if err != nil {
log.Println(err)
return
}
output := blackfriday.Markdown(input, blackfriday.HtmlRenderer(0, "", ""), blackfriday.EXTENSION_TABLES|blackfriday.EXTENSION_FENCED_CODE|blackfriday.EXTENSION_AUTOLINK)
css := ``
html := "
API Document" + css + "" + string(output) + ""
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write([]byte(html))
})
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/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/models", routers.ModelsGet).Methods("GET")
r.HandleFunc("/api/models", routers.ModelsPost).Methods("POST")
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/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/tasks", routers.TasksGet).Methods("GET")
r.HandleFunc("/api/tasks", routers.TasksPost).Methods("POST")
r.HandleFunc("/api/tasks/{id}", routers.TasksItemGet).Methods("GET")
r.HandleFunc("/api/tasks/{id}", routers.TasksItemPatch).Methods("PATCH")
r.HandleFunc("/api/tasks/{id}", routers.TasksItemDelete).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")
log.Println("Web Server is running on http://localhost:8080")
http.ListenAndServe(":8080", r)
}