@@ -12,7 +12,6 @@ import (
1212 "strings"
1313 "time"
1414
15- "github.com/go-chi/chi/v5"
1615 "github.com/go-oauth2/oauth2/v4/errors"
1716 "github.com/go-oauth2/oauth2/v4/generates"
1817 "github.com/go-oauth2/oauth2/v4/manage"
@@ -22,6 +21,7 @@ import (
2221 log "github.com/go-pkgz/lgr"
2322 "github.com/go-pkgz/rest"
2423 "github.com/go-pkgz/rest/logger"
24+ "github.com/go-pkgz/routegroup"
2525 "github.com/golang-jwt/jwt/v5"
2626 "golang.org/x/oauth2"
2727
@@ -49,7 +49,7 @@ func main() {
4949 AvatarStore : avatar .NewLocalFS ("/tmp/demo-auth-service" ), // stores avatars locally
5050 AvatarResizeLimit : 200 , // resizes avatars to 200x200
5151 ClaimsUpd : token .ClaimsUpdFunc (func (claims token.Claims ) token.Claims { // modify issued token
52- if claims .User != nil && claims .User .Name == "dev_admin" { // set attributes for dev_admin
52+ if claims .User != nil && claims .User .Name == "dev_admin" { // set attributes for dev_admin
5353 claims .User .SetAdmin (true )
5454 claims .User .SetStrAttr ("custom-key" , "some value" )
5555 }
@@ -185,18 +185,18 @@ func main() {
185185 m := service .Middleware ()
186186
187187 // setup http server
188- router := chi . NewRouter ( )
188+ router := routegroup . New ( http . NewServeMux () )
189189 // add some external middlewares from go-pkgz/rest
190190 router .Use (rest .AppInfo ("auth-example" , "umputun" , "1.0.0" ), rest .Ping )
191191 router .Use (logger .New (logger .Log (log .Default ()), logger .WithBody , logger .Prefix ("[INFO]" )).Handler ) // log all http requests
192- router .Get ( " /open" , openRouteHandler ) // open page
193- router .Group (func (r chi. Router ) {
192+ router .HandleFunc ( "GET /open" , openRouteHandler ) // open page
193+ router .Group (). Route ( func (r * routegroup. Bundle ) {
194194 r .Use (m .Auth )
195195 r .Use (m .UpdateUser (middleware .UserUpdFunc (func (user token.User ) token.User {
196196 user .SetStrAttr ("some_attribute" , "attribute value" )
197197 return user
198198 })))
199- r .Get ( " /private_data" , protectedDataHandler ) // protected api
199+ r .HandleFunc ( "GET /private_data" , protectedDataHandler ) // protected api
200200 })
201201
202202 // static files under ~/web
@@ -206,8 +206,8 @@ func main() {
206206
207207 // setup auth routes
208208 authRoutes , avaRoutes := service .Handlers ()
209- router .Mount ("/auth" , authRoutes ) // add auth handlers
210- router .Mount ("/avatar" , avaRoutes ) // add avatar handler
209+ router .Handle ("/auth/ " , http . StripPrefix ( "/auth" , authRoutes )) // add auth handlers
210+ router .Handle ("/avatar/ " , http . StripPrefix ( "/avatar" , avaRoutes ) ) // add avatar handler
211211
212212 httpServer := & http.Server {
213213 Addr : ":8080" ,
@@ -240,9 +240,8 @@ func anonymousAuthProvider() provider.CredCheckerFunc {
240240 }
241241}
242242
243- // FileServer conveniently sets up a http.FileServer handler to serve static files from a http.FileSystem.
244- // Borrowed from https://github.com/go-chi/chi/blob/master/_examples/fileserver/main.go
245- func fileServer (r chi.Router , path string , root http.FileSystem ) {
243+ // fileServer conveniently sets up a http.FileServer handler to serve static files from a http.FileSystem.
244+ func fileServer (r * routegroup.Bundle , path string , root http.FileSystem ) {
246245 if strings .ContainsAny (path , "{}*" ) {
247246 panic ("FileServer does not permit URL parameters." )
248247 }
@@ -251,14 +250,11 @@ func fileServer(r chi.Router, path string, root http.FileSystem) {
251250 fs := http .StripPrefix (path , http .FileServer (root ))
252251
253252 if path != "/" && path [len (path )- 1 ] != '/' {
254- r .Get ( path , http .RedirectHandler (path + "/" , http .StatusMovedPermanently ). ServeHTTP )
253+ r .Handle ( "GET " + path , http .RedirectHandler (path + "/" , http .StatusMovedPermanently ))
255254 path += "/"
256255 }
257- path += "*"
258256
259- r .Get (path , func (w http.ResponseWriter , r * http.Request ) {
260- fs .ServeHTTP (w , r )
261- })
257+ r .Handle ("GET " + path + "{file...}" , fs )
262258}
263259
264260// GET /open returns a page available without authorization
0 commit comments