From abf169abeb5b0c5ba6a3dd3f6290554e8e41111d Mon Sep 17 00:00:00 2001 From: Sky Johnson Date: Wed, 7 May 2025 12:30:52 -0500 Subject: [PATCH] fix documentation --- EXAMPLES.md | 23 ++++++++++++----------- README.md | 19 ++++++------------- 2 files changed, 18 insertions(+), 24 deletions(-) diff --git a/EXAMPLES.md b/EXAMPLES.md index 26935c2..28caa63 100644 --- a/EXAMPLES.md +++ b/EXAMPLES.md @@ -1,4 +1,4 @@ -# Router Usage Examples +# Examples ## Basic Usage @@ -56,22 +56,22 @@ r.Get("/files/*path", func(w http.ResponseWriter, r *http.Request, params []stri ```go // Logging middleware func LoggingMiddleware(next router.Handler) router.Handler { - return func(w http.ResponseWriter, r *http.Request, params []string) { + return router.Handler(func(w http.ResponseWriter, r *http.Request, params []string) { fmt.Printf("[%s] %s\n", r.Method, r.URL.Path) next(w, r, params) - } + }) } // Auth middleware func AuthMiddleware(next router.Handler) router.Handler { - return func(w http.ResponseWriter, r *http.Request, params []string) { + return router.Handler(func(w http.ResponseWriter, r *http.Request, params []string) { token := r.Header.Get("Authorization") if token == "" { http.Error(w, "Unauthorized", http.StatusUnauthorized) return } next(w, r, params) - } + }) } // Global middleware @@ -139,6 +139,7 @@ if err != nil { } // Custom NotFound handler +oldServeHTTP := r.ServeHTTP r.ServeHTTP = func(w http.ResponseWriter, req *http.Request) { h, params, ok := r.Lookup(req.Method, req.URL.Path) if !ok { @@ -239,25 +240,25 @@ func adminUsersHandler(w http.ResponseWriter, r *http.Request, _ []string) { // Middleware func LoggingMiddleware(next router.Handler) router.Handler { - return func(w http.ResponseWriter, r *http.Request, params []string) { + return router.Handler(func(w http.ResponseWriter, r *http.Request, params []string) { log.Printf("[%s] %s", r.Method, r.URL.Path) next(w, r, params) - } + }) } func ApiKeyMiddleware(next router.Handler) router.Handler { - return func(w http.ResponseWriter, r *http.Request, params []string) { + return router.Handler(func(w http.ResponseWriter, r *http.Request, params []string) { apiKey := r.Header.Get("X-API-Key") if apiKey == "" { http.Error(w, "API key required", http.StatusUnauthorized) return } next(w, r, params) - } + }) } func AuthMiddleware(next router.Handler) router.Handler { - return func(w http.ResponseWriter, r *http.Request, params []string) { + return router.Handler(func(w http.ResponseWriter, r *http.Request, params []string) { // Check session or JWT authorized := checkUserAuth(r) if !authorized { @@ -265,7 +266,7 @@ func AuthMiddleware(next router.Handler) router.Handler { return } next(w, r, params) - } + }) } func checkUserAuth(r *http.Request) bool { diff --git a/README.md b/README.md index 7a09123..925d0eb 100644 --- a/README.md +++ b/README.md @@ -43,14 +43,9 @@ r.Get("/files/*path", func(w router.Res, r router.Req, params []string) { fmt.Fprintf(w, "File path: %s", filePath) }) -// Standard http.HandlerFunc adapter -r.Get("/simple", router.StandardHandler(func(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, "Simple handler without params") -})) - // Lookup routes manually if handler, params, ok := r.Lookup("GET", "/users/123"); ok { - handler.Serve(params) + handler(nil, nil, params) } // Or simply serve them @@ -62,13 +57,11 @@ http.ListenAndServe(":8080", r) ```go // Create logging middleware func LoggingMiddleware(next router.Handler) router.Handler { - return &router.simpleHandler{ - fn: func(params []string) { - fmt.Println("Request started") - next.Serve(params) - fmt.Println("Request completed") - }, - } + return router.Handler(func(w router.Res, r router.Req, params []string) { + fmt.Println("Request started") + next(w, r, params) + fmt.Println("Request completed") + }) } // Apply middleware globally