diff --git a/go.mod b/go.mod index 359d6c5..133f7e4 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,8 @@ module chromagies go 1.24.2 require ( - github.com/gorilla/mux v1.8.1 - filippo.io/edwards25519 v1.1.0 github.com/go-sql-driver/mysql v1.9.2 + github.com/julienschmidt/httprouter v1.3.0 ) + +require filippo.io/edwards25519 v1.1.0 // indirect diff --git a/go.sum b/go.sum index 1673b5e..5ab75fd 100644 --- a/go.sum +++ b/go.sum @@ -2,5 +2,5 @@ filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= github.com/go-sql-driver/mysql v1.9.2 h1:4cNKDYQ1I84SXslGddlsrMhc8k4LeDVj6Ad6WRjiHuU= github.com/go-sql-driver/mysql v1.9.2/go.mod h1:qn46aNg1333BRMNU69Lq93t8du/dwxI64Gl8i5p1WMU= -github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= -github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= +github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U= +github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= diff --git a/main.go b/main.go index 48297f1..7932770 100644 --- a/main.go +++ b/main.go @@ -13,10 +13,10 @@ import ( "syscall" "time" - "github.com/gorilla/mux" + "github.com/julienschmidt/httprouter" ) -var router *mux.Router +var router *httprouter.Router var logFile *os.File func main() { @@ -78,14 +78,13 @@ func initConfig() { } func initRouter() { - router = mux.NewRouter() + router = httprouter.New() // routeur.NotFoundHandler = TODO api.Init(router) - router.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./static/")))) - + router.ServeFiles("/static/*filepath", http.Dir("./static/")) } func handleStopSignal() { diff --git a/src/api/api.go b/src/api/api.go index c55c6b0..9e15d3e 100644 --- a/src/api/api.go +++ b/src/api/api.go @@ -7,7 +7,7 @@ import ( "net/http" "time" - "github.com/gorilla/mux" + "github.com/julienschmidt/httprouter" ) const ( @@ -16,129 +16,131 @@ const ( var apiRootPath string = "/api/v1" -func Init(router *mux.Router) { +func Init(router *httprouter.Router) { profiler.Register(profiler_API_PROCESS_REQUEST, 10000, "µs") - router.HandleFunc(apiRootPath, apiRoot).Methods("GET") + router.GET("/", apiHandler(apiRoot)) - routerAuth := router.PathPrefix(apiRootPath + "/auth").Subrouter() - routerPage := router.PathPrefix(apiRootPath + "/page").Subrouter() - routerUser := router.PathPrefix(apiRootPath + "/user").Subrouter() - routerTag := router.PathPrefix(apiRootPath + "/tag").Subrouter() - routerDebug := router.PathPrefix(apiRootPath + "/debug").Subrouter() + router.GET(apiRootPath+"/auth", apiHandler(apiAuth)) + router.GET(apiRootPath+"/auth/login", apiHandler(apiAuthLogin)) + router.GET(apiRootPath+"/auth/logout", apiHandler(apiAuthLogout)) - routerAuth.HandleFunc("", apiAuth) - routerAuth.HandleFunc("/login", apiAuthLogin) - routerAuth.HandleFunc("/logout", apiAuthLogout) + router.GET(apiRootPath+"/page", apiHandler(apiPage)) + router.GET(apiRootPath+"/page/:folder", apiHandler(apiPageFolder)) + router.GET(apiRootPath+"/page/:folder/:page", apiHandler(apiPageFolderPage)) + router.GET(apiRootPath+"/page/:folder/:page/content", apiHandler(apiPageFolderPageContent)) - routerPage.HandleFunc("", apiPage) - routerPage.HandleFunc("/{folder}", apiPageFolder) - routerPage.HandleFunc("/{folder}/{page}", apiPageFolderPage) - routerPage.HandleFunc("/{folder}/{page}/content", apiPageFolderPageContent) + router.GET(apiRootPath+"/user", apiHandler(apiUser)) + router.GET(apiRootPath+"/user/:name", apiHandler(apiUserName)) - routerUser.HandleFunc("", apiUser) - routerUser.HandleFunc("/{name}", apiUserName) + router.GET(apiRootPath+"/tag", apiHandler(apiTag)) + router.GET(apiRootPath+"/tag/:name", apiHandler(apiTagName)) - routerTag.HandleFunc("", apiTag) - routerTag.HandleFunc("/{name}", apiTagName) + router.GET(apiRootPath+"/debug/profiler", apiHandler(apiDebugProfiler)) + router.GET(apiRootPath+"/debug/profiler/:name", apiHandler(apiDebugProfilerName)) - routerDebug.HandleFunc("/profiler", apiDebugProfiler) - routerDebug.HandleFunc("/profiler/{name}", apiDebugProfilerName) + /* + + // router.HandleFunc(apiRootPath, apiRoot).Methods("GET") + + routerAuth := router.PathPrefix(apiRootPath + "/auth").Subrouter() + routerPage := router.PathPrefix(apiRootPath + "/page").Subrouter() + routerUser := router.PathPrefix(apiRootPath + "/user").Subrouter() + routerTag := router.PathPrefix(apiRootPath + "/tag").Subrouter() + routerDebug := router.PathPrefix(apiRootPath + "/debug").Subrouter() + + routerAuth.HandleFunc("", apiAuth) + routerAuth.HandleFunc("/login", apiAuthLogin) + routerAuth.HandleFunc("/logout", apiAuthLogout) + + routerPage.HandleFunc("", apiPage) + routerPage.HandleFunc("/{folder}", apiPageFolder) + routerPage.HandleFunc("/{folder}/{page}", apiPageFolderPage) + routerPage.HandleFunc("/{folder}/{page}/content", apiPageFolderPageContent) + + routerUser.HandleFunc("", apiUser) + routerUser.HandleFunc("/{name}", apiUserName) + + routerTag.HandleFunc("", apiTag) + routerTag.HandleFunc("/{name}", apiTagName) + + routerDebug.HandleFunc("/profiler", apiDebugProfiler) + routerDebug.HandleFunc("/profiler/{name}", apiDebugProfilerName) + + */ +} + +func apiHandler(h httprouter.Handle) httprouter.Handle { + return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { + tStart := time.Now() + h(w, r, ps) + profiler.Add(profiler_API_PROCESS_REQUEST, time.Duration(time.Since(tStart).Microseconds())) + } } // API ROOT -func apiRoot(w http.ResponseWriter, r *http.Request) { - tStart := time.Now() +func apiRoot(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { database.ExecuteStoredRoutine("test") fmt.Fprintf(w, "API ROOT") - profiler.Add(profiler_API_PROCESS_REQUEST, time.Duration(time.Since(tStart).Microseconds())) } // API Auth -func apiAuth(w http.ResponseWriter, r *http.Request) { - tStart := time.Now() +func apiAuth(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { fmt.Fprintf(w, "API Auth") - profiler.Add(profiler_API_PROCESS_REQUEST, time.Duration(time.Since(tStart).Microseconds())) } -func apiAuthLogin(w http.ResponseWriter, r *http.Request) { - tStart := time.Now() +func apiAuthLogin(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { fmt.Fprintf(w, "API Auth Login") - profiler.Add(profiler_API_PROCESS_REQUEST, time.Duration(time.Since(tStart).Microseconds())) } -func apiAuthLogout(w http.ResponseWriter, r *http.Request) { - tStart := time.Now() +func apiAuthLogout(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { fmt.Fprintf(w, "API Auth Logout") - profiler.Add(profiler_API_PROCESS_REQUEST, time.Duration(time.Since(tStart).Microseconds())) } // API Page -func apiPage(w http.ResponseWriter, r *http.Request) { - tStart := time.Now() +func apiPage(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { fmt.Fprintf(w, "API Page") - profiler.Add(profiler_API_PROCESS_REQUEST, time.Duration(time.Since(tStart).Microseconds())) } -func apiPageFolder(w http.ResponseWriter, r *http.Request) { - tStart := time.Now() - vars := mux.Vars(r) - fmt.Fprintf(w, "API Page Folder(%s)", vars["folder"]) - profiler.Add(profiler_API_PROCESS_REQUEST, time.Duration(time.Since(tStart).Microseconds())) +func apiPageFolder(w http.ResponseWriter, r *http.Request, params httprouter.Params) { + fmt.Fprintf(w, "API Page Folder(%s)", params.ByName("page")) } -func apiPageFolderPage(w http.ResponseWriter, r *http.Request) { - tStart := time.Now() - vars := mux.Vars(r) - fmt.Fprintf(w, "API Page Folder(%s) Page(%s)", vars["folder"], vars["page"]) - profiler.Add(profiler_API_PROCESS_REQUEST, time.Duration(time.Since(tStart).Microseconds())) +func apiPageFolderPage(w http.ResponseWriter, r *http.Request, params httprouter.Params) { + fmt.Fprintf(w, "API Page Folder(%s) Page(%s)", params.ByName("folder"), params.ByName("page")) } -func apiPageFolderPageContent(w http.ResponseWriter, r *http.Request) { - tStart := time.Now() - vars := mux.Vars(r) - fmt.Fprintf(w, "API Page Folder(%s) Page(%s) Content", vars["folder"], vars["page"]) - profiler.Add(profiler_API_PROCESS_REQUEST, time.Duration(time.Since(tStart).Microseconds())) +func apiPageFolderPageContent(w http.ResponseWriter, r *http.Request, params httprouter.Params) { + fmt.Fprintf(w, "API Page Folder(%s) Page(%s) Content", params.ByName("folder"), params.ByName("page")) } // API User -func apiUser(w http.ResponseWriter, r *http.Request) { - tStart := time.Now() +func apiUser(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { fmt.Fprintf(w, "API User") - profiler.Add(profiler_API_PROCESS_REQUEST, time.Duration(time.Since(tStart).Microseconds())) } -func apiUserName(w http.ResponseWriter, r *http.Request) { - tStart := time.Now() - vars := mux.Vars(r) - fmt.Fprintf(w, "API User Name(%s)", vars["name"]) - profiler.Add(profiler_API_PROCESS_REQUEST, time.Duration(time.Since(tStart).Microseconds())) +func apiUserName(w http.ResponseWriter, r *http.Request, params httprouter.Params) { + fmt.Fprintf(w, "API User Name(%s)", params.ByName("page")) } // API Tag -func apiTag(w http.ResponseWriter, r *http.Request) { - tStart := time.Now() +func apiTag(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { fmt.Fprintf(w, "API Tag") - profiler.Add(profiler_API_PROCESS_REQUEST, time.Duration(time.Since(tStart).Microseconds())) } -func apiTagName(w http.ResponseWriter, r *http.Request) { - tStart := time.Now() - vars := mux.Vars(r) - fmt.Fprintf(w, "API Tag Name(%s)", vars["name"]) - profiler.Add(profiler_API_PROCESS_REQUEST, time.Duration(time.Since(tStart).Microseconds())) +func apiTagName(w http.ResponseWriter, r *http.Request, params httprouter.Params) { + fmt.Fprintf(w, "API Tag Name(%s)", params.ByName("page")) } // API Tag -func apiDebugProfiler(w http.ResponseWriter, r *http.Request) { - tStart := time.Now() +func apiDebugProfiler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { var entries []string profiler.GetAll(&entries) @@ -148,12 +150,8 @@ func apiDebugProfiler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "API Debug Profiler(%s)\n\n%s\n\n", entry, string(profiler.Get(entry).ToString())) } - profiler.Add(profiler_API_PROCESS_REQUEST, time.Duration(time.Since(tStart).Microseconds())) } -func apiDebugProfilerName(w http.ResponseWriter, r *http.Request) { - tStart := time.Now() - vars := mux.Vars(r) - fmt.Fprintf(w, "API Debug Profiler(%s)\n%s", vars["name"], string(profiler.Get(vars["name"]).ToString())) - profiler.Add(profiler_API_PROCESS_REQUEST, time.Duration(time.Since(tStart).Microseconds())) +func apiDebugProfilerName(w http.ResponseWriter, r *http.Request, params httprouter.Params) { + fmt.Fprintf(w, "API Debug Profiler(%s)\n%s", params.ByName("page"), string(profiler.Get(params.ByName("page")).ToString())) }