fix documentation

This commit is contained in:
Sky Johnson 2025-05-07 12:30:52 -05:00
parent e4abb99df9
commit abf169abeb
2 changed files with 18 additions and 24 deletions

View File

@ -1,4 +1,4 @@
# Router Usage Examples # Examples
## Basic Usage ## Basic Usage
@ -56,22 +56,22 @@ r.Get("/files/*path", func(w http.ResponseWriter, r *http.Request, params []stri
```go ```go
// Logging middleware // Logging middleware
func LoggingMiddleware(next router.Handler) router.Handler { 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) fmt.Printf("[%s] %s\n", r.Method, r.URL.Path)
next(w, r, params) next(w, r, params)
} })
} }
// Auth middleware // Auth middleware
func AuthMiddleware(next router.Handler) router.Handler { 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") token := r.Header.Get("Authorization")
if token == "" { if token == "" {
http.Error(w, "Unauthorized", http.StatusUnauthorized) http.Error(w, "Unauthorized", http.StatusUnauthorized)
return return
} }
next(w, r, params) next(w, r, params)
} })
} }
// Global middleware // Global middleware
@ -139,6 +139,7 @@ if err != nil {
} }
// Custom NotFound handler // Custom NotFound handler
oldServeHTTP := r.ServeHTTP
r.ServeHTTP = func(w http.ResponseWriter, req *http.Request) { r.ServeHTTP = func(w http.ResponseWriter, req *http.Request) {
h, params, ok := r.Lookup(req.Method, req.URL.Path) h, params, ok := r.Lookup(req.Method, req.URL.Path)
if !ok { if !ok {
@ -239,25 +240,25 @@ func adminUsersHandler(w http.ResponseWriter, r *http.Request, _ []string) {
// Middleware // Middleware
func LoggingMiddleware(next router.Handler) router.Handler { 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) log.Printf("[%s] %s", r.Method, r.URL.Path)
next(w, r, params) next(w, r, params)
} })
} }
func ApiKeyMiddleware(next router.Handler) router.Handler { 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") apiKey := r.Header.Get("X-API-Key")
if apiKey == "" { if apiKey == "" {
http.Error(w, "API key required", http.StatusUnauthorized) http.Error(w, "API key required", http.StatusUnauthorized)
return return
} }
next(w, r, params) next(w, r, params)
} })
} }
func AuthMiddleware(next router.Handler) router.Handler { 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 // Check session or JWT
authorized := checkUserAuth(r) authorized := checkUserAuth(r)
if !authorized { if !authorized {
@ -265,7 +266,7 @@ func AuthMiddleware(next router.Handler) router.Handler {
return return
} }
next(w, r, params) next(w, r, params)
} })
} }
func checkUserAuth(r *http.Request) bool { func checkUserAuth(r *http.Request) bool {

View File

@ -43,14 +43,9 @@ r.Get("/files/*path", func(w router.Res, r router.Req, params []string) {
fmt.Fprintf(w, "File path: %s", filePath) 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 // Lookup routes manually
if handler, params, ok := r.Lookup("GET", "/users/123"); ok { if handler, params, ok := r.Lookup("GET", "/users/123"); ok {
handler.Serve(params) handler(nil, nil, params)
} }
// Or simply serve them // Or simply serve them
@ -62,13 +57,11 @@ http.ListenAndServe(":8080", r)
```go ```go
// Create logging middleware // Create logging middleware
func LoggingMiddleware(next router.Handler) router.Handler { func LoggingMiddleware(next router.Handler) router.Handler {
return &router.simpleHandler{ return router.Handler(func(w router.Res, r router.Req, params []string) {
fn: func(params []string) {
fmt.Println("Request started") fmt.Println("Request started")
next.Serve(params) next(w, r, params)
fmt.Println("Request completed") fmt.Println("Request completed")
}, })
}
} }
// Apply middleware globally // Apply middleware globally