Switching from Gorilla to httprouter
parent
af1f6352ce
commit
e1baee2fd3
5
go.mod
5
go.mod
|
@ -3,7 +3,8 @@ module chromagies
|
||||||
go 1.24.2
|
go 1.24.2
|
||||||
|
|
||||||
require (
|
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/go-sql-driver/mysql v1.9.2
|
||||||
|
github.com/julienschmidt/httprouter v1.3.0
|
||||||
)
|
)
|
||||||
|
|
||||||
|
require filippo.io/edwards25519 v1.1.0 // indirect
|
||||||
|
|
4
go.sum
4
go.sum
|
@ -2,5 +2,5 @@ filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
|
||||||
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
|
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 h1:4cNKDYQ1I84SXslGddlsrMhc8k4LeDVj6Ad6WRjiHuU=
|
||||||
github.com/go-sql-driver/mysql v1.9.2/go.mod h1:qn46aNg1333BRMNU69Lq93t8du/dwxI64Gl8i5p1WMU=
|
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/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
|
||||||
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
|
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
|
||||||
|
|
9
main.go
9
main.go
|
@ -13,10 +13,10 @@ import (
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/julienschmidt/httprouter"
|
||||||
)
|
)
|
||||||
|
|
||||||
var router *mux.Router
|
var router *httprouter.Router
|
||||||
var logFile *os.File
|
var logFile *os.File
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -78,14 +78,13 @@ func initConfig() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func initRouter() {
|
func initRouter() {
|
||||||
router = mux.NewRouter()
|
router = httprouter.New()
|
||||||
|
|
||||||
// routeur.NotFoundHandler = TODO
|
// routeur.NotFoundHandler = TODO
|
||||||
|
|
||||||
api.Init(router)
|
api.Init(router)
|
||||||
|
|
||||||
router.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))))
|
router.ServeFiles("/static/*filepath", http.Dir("./static/"))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleStopSignal() {
|
func handleStopSignal() {
|
||||||
|
|
148
src/api/api.go
148
src/api/api.go
|
@ -7,7 +7,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/julienschmidt/httprouter"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -16,129 +16,131 @@ const (
|
||||||
|
|
||||||
var apiRootPath string = "/api/v1"
|
var apiRootPath string = "/api/v1"
|
||||||
|
|
||||||
func Init(router *mux.Router) {
|
func Init(router *httprouter.Router) {
|
||||||
|
|
||||||
profiler.Register(profiler_API_PROCESS_REQUEST, 10000, "µs")
|
profiler.Register(profiler_API_PROCESS_REQUEST, 10000, "µs")
|
||||||
|
|
||||||
router.HandleFunc(apiRootPath, apiRoot).Methods("GET")
|
router.GET("/", apiHandler(apiRoot))
|
||||||
|
|
||||||
routerAuth := router.PathPrefix(apiRootPath + "/auth").Subrouter()
|
router.GET(apiRootPath+"/auth", apiHandler(apiAuth))
|
||||||
routerPage := router.PathPrefix(apiRootPath + "/page").Subrouter()
|
router.GET(apiRootPath+"/auth/login", apiHandler(apiAuthLogin))
|
||||||
routerUser := router.PathPrefix(apiRootPath + "/user").Subrouter()
|
router.GET(apiRootPath+"/auth/logout", apiHandler(apiAuthLogout))
|
||||||
routerTag := router.PathPrefix(apiRootPath + "/tag").Subrouter()
|
|
||||||
routerDebug := router.PathPrefix(apiRootPath + "/debug").Subrouter()
|
|
||||||
|
|
||||||
routerAuth.HandleFunc("", apiAuth)
|
router.GET(apiRootPath+"/page", apiHandler(apiPage))
|
||||||
routerAuth.HandleFunc("/login", apiAuthLogin)
|
router.GET(apiRootPath+"/page/:folder", apiHandler(apiPageFolder))
|
||||||
routerAuth.HandleFunc("/logout", apiAuthLogout)
|
router.GET(apiRootPath+"/page/:folder/:page", apiHandler(apiPageFolderPage))
|
||||||
|
router.GET(apiRootPath+"/page/:folder/:page/content", apiHandler(apiPageFolderPageContent))
|
||||||
|
|
||||||
routerPage.HandleFunc("", apiPage)
|
router.GET(apiRootPath+"/user", apiHandler(apiUser))
|
||||||
routerPage.HandleFunc("/{folder}", apiPageFolder)
|
router.GET(apiRootPath+"/user/:name", apiHandler(apiUserName))
|
||||||
routerPage.HandleFunc("/{folder}/{page}", apiPageFolderPage)
|
|
||||||
routerPage.HandleFunc("/{folder}/{page}/content", apiPageFolderPageContent)
|
|
||||||
|
|
||||||
routerUser.HandleFunc("", apiUser)
|
router.GET(apiRootPath+"/tag", apiHandler(apiTag))
|
||||||
routerUser.HandleFunc("/{name}", apiUserName)
|
router.GET(apiRootPath+"/tag/:name", apiHandler(apiTagName))
|
||||||
|
|
||||||
routerTag.HandleFunc("", apiTag)
|
router.GET(apiRootPath+"/debug/profiler", apiHandler(apiDebugProfiler))
|
||||||
routerTag.HandleFunc("/{name}", apiTagName)
|
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
|
// API ROOT
|
||||||
|
|
||||||
func apiRoot(w http.ResponseWriter, r *http.Request) {
|
func apiRoot(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||||
tStart := time.Now()
|
|
||||||
database.ExecuteStoredRoutine("test")
|
database.ExecuteStoredRoutine("test")
|
||||||
fmt.Fprintf(w, "API ROOT")
|
fmt.Fprintf(w, "API ROOT")
|
||||||
profiler.Add(profiler_API_PROCESS_REQUEST, time.Duration(time.Since(tStart).Microseconds()))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// API Auth
|
// API Auth
|
||||||
|
|
||||||
func apiAuth(w http.ResponseWriter, r *http.Request) {
|
func apiAuth(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||||
tStart := time.Now()
|
|
||||||
fmt.Fprintf(w, "API Auth")
|
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) {
|
func apiAuthLogin(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||||
tStart := time.Now()
|
|
||||||
fmt.Fprintf(w, "API Auth Login")
|
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) {
|
func apiAuthLogout(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||||
tStart := time.Now()
|
|
||||||
fmt.Fprintf(w, "API Auth Logout")
|
fmt.Fprintf(w, "API Auth Logout")
|
||||||
profiler.Add(profiler_API_PROCESS_REQUEST, time.Duration(time.Since(tStart).Microseconds()))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// API Page
|
// API Page
|
||||||
|
|
||||||
func apiPage(w http.ResponseWriter, r *http.Request) {
|
func apiPage(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||||
tStart := time.Now()
|
|
||||||
fmt.Fprintf(w, "API Page")
|
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) {
|
func apiPageFolder(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
|
||||||
tStart := time.Now()
|
fmt.Fprintf(w, "API Page Folder(%s)", params.ByName("page"))
|
||||||
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 apiPageFolderPage(w http.ResponseWriter, r *http.Request) {
|
func apiPageFolderPage(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
|
||||||
tStart := time.Now()
|
fmt.Fprintf(w, "API Page Folder(%s) Page(%s)", params.ByName("folder"), params.ByName("page"))
|
||||||
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 apiPageFolderPageContent(w http.ResponseWriter, r *http.Request) {
|
func apiPageFolderPageContent(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
|
||||||
tStart := time.Now()
|
fmt.Fprintf(w, "API Page Folder(%s) Page(%s) Content", params.ByName("folder"), params.ByName("page"))
|
||||||
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()))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// API User
|
// API User
|
||||||
|
|
||||||
func apiUser(w http.ResponseWriter, r *http.Request) {
|
func apiUser(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||||
tStart := time.Now()
|
|
||||||
fmt.Fprintf(w, "API User")
|
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) {
|
func apiUserName(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
|
||||||
tStart := time.Now()
|
fmt.Fprintf(w, "API User Name(%s)", params.ByName("page"))
|
||||||
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()))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// API Tag
|
// API Tag
|
||||||
|
|
||||||
func apiTag(w http.ResponseWriter, r *http.Request) {
|
func apiTag(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||||
tStart := time.Now()
|
|
||||||
fmt.Fprintf(w, "API Tag")
|
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) {
|
func apiTagName(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
|
||||||
tStart := time.Now()
|
fmt.Fprintf(w, "API Tag Name(%s)", params.ByName("page"))
|
||||||
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()))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// API Tag
|
// API Tag
|
||||||
|
|
||||||
func apiDebugProfiler(w http.ResponseWriter, r *http.Request) {
|
func apiDebugProfiler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||||
tStart := time.Now()
|
|
||||||
|
|
||||||
var entries []string
|
var entries []string
|
||||||
profiler.GetAll(&entries)
|
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()))
|
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) {
|
func apiDebugProfilerName(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
|
||||||
tStart := time.Now()
|
fmt.Fprintf(w, "API Debug Profiler(%s)\n%s", params.ByName("page"), string(profiler.Get(params.ByName("page")).ToString()))
|
||||||
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()))
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue