fix documentation
This commit is contained in:
parent
e4abb99df9
commit
abf169abeb
23
EXAMPLES.md
23
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 {
|
||||
|
15
README.md
15
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) {
|
||||
return router.Handler(func(w router.Res, r router.Req, params []string) {
|
||||
fmt.Println("Request started")
|
||||
next.Serve(params)
|
||||
next(w, r, params)
|
||||
fmt.Println("Request completed")
|
||||
},
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Apply middleware globally
|
||||
|
Loading…
x
Reference in New Issue
Block a user