Refactor handleFunc for chaining middlewares
This commit is contained in:
parent
2894493d34
commit
6c6bde42a1
29
web/main.go
29
web/main.go
@ -19,23 +19,30 @@ func logged(handler http.HandlerFunc) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func handleFunc(pattern string, handler http.HandlerFunc) {
|
||||
http.HandleFunc(pattern, logged(handler))
|
||||
func handleFunc(
|
||||
pattern string,
|
||||
handler http.HandlerFunc,
|
||||
middlewares ...func(http.HandlerFunc) http.HandlerFunc,
|
||||
) {
|
||||
for _, middleware := range middlewares {
|
||||
handler = middleware(handler)
|
||||
}
|
||||
http.HandleFunc(pattern, handler)
|
||||
}
|
||||
|
||||
func RunServer(db core.DB, addr string, port string) {
|
||||
env := &Env{db}
|
||||
bind := net.JoinHostPort(addr, port)
|
||||
|
||||
handleFunc("GET /", env.getRoot)
|
||||
handleFunc("GET /style.css", env.getStyle)
|
||||
handleFunc("GET /htmx.org@2.0.4.js", env.getScript)
|
||||
handleFunc("GET /source/{source}", env.getSource)
|
||||
handleFunc("GET /channel/{channel}", env.getChannel)
|
||||
handleFunc("GET /item/{source}/{id}", env.getItem)
|
||||
handleFunc("DELETE /item/{source}/{id}", env.deleteItem)
|
||||
handleFunc("POST /item/{source}/{id}/action/{action}", env.doAction)
|
||||
handleFunc("POST /mass-deactivate", env.massDeactivate)
|
||||
handleFunc("GET /", env.getRoot, logged)
|
||||
handleFunc("GET /style.css", env.getStyle, logged)
|
||||
handleFunc("GET /htmx.org@2.0.4.js", env.getScript, logged)
|
||||
handleFunc("GET /source/{source}", env.getSource, logged)
|
||||
handleFunc("GET /channel/{channel}", env.getChannel, logged)
|
||||
handleFunc("GET /item/{source}/{id}", env.getItem, logged)
|
||||
handleFunc("DELETE /item/{source}/{id}", env.deleteItem, logged)
|
||||
handleFunc("POST /item/{source}/{id}/action/{action}", env.doAction, logged)
|
||||
handleFunc("POST /mass-deactivate", env.massDeactivate, logged)
|
||||
|
||||
log.Fatal(http.ListenAndServe(bind, nil))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user