mirror of
https://github.com/hibiken/asynqmon.git
synced 2025-04-19 23:30:12 +08:00
Merge 32e440bbff39422a15ae25627311a0efdf8b7a1c into d1b889456de3c98c22de24642b6f836a29da3140
This commit is contained in:
commit
e1a2aec7df
55
README.md
55
README.md
@ -253,6 +253,61 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
Example with Basic Auth Middleware in [gorilla/mux](https://pkg.go.dev/github.com/gorilla/mux):
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/hibiken/asynq"
|
||||
"github.com/hibiken/asynqmon"
|
||||
)
|
||||
|
||||
func main() {
|
||||
h := asynqmon.New(asynqmon.Options{
|
||||
RootPath: "/monitoring", // RootPath specifies the root for asynqmon app
|
||||
RedisConnOpt: asynq.RedisClientOpt{Addr: ":6379"},
|
||||
})
|
||||
|
||||
r := mux.NewRouter()
|
||||
r.PathPrefix(h.RootPath()).Handler(basicAuthMiddleware(u, p, h))
|
||||
|
||||
srv := &http.Server{
|
||||
Handler: r,
|
||||
Addr: ":8080",
|
||||
}
|
||||
|
||||
// Go to http://localhost:8080/monitoring to see asynqmon homepage.
|
||||
log.Fatal(srv.ListenAndServe())
|
||||
}
|
||||
|
||||
func basicAuthMiddleware(u, p string, next http.Handler) http.Handler {
|
||||
var unauthorized = func(w http.ResponseWriter) {
|
||||
w.Header().Set("WWW-Authenticate", `Basic realm="restricted", charset="UTF-8"`)
|
||||
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
||||
}
|
||||
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
username, password, ok := r.BasicAuth()
|
||||
if !ok {
|
||||
unauthorized(w)
|
||||
return
|
||||
}
|
||||
|
||||
if !(u == username && p == password) {
|
||||
unauthorized(w)
|
||||
return
|
||||
}
|
||||
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
Example with [labstack/echo](https://github.com/labstack/echo)):
|
||||
|
||||
|
||||
|
@ -17,3 +17,36 @@ func ExampleHTTPHandler() {
|
||||
http.Handle(h.RootPath(), h)
|
||||
log.Fatal(http.ListenAndServe(":8000", nil)) // visit localhost:8000/monitoring to see asynqmon homepage
|
||||
}
|
||||
|
||||
func ExampleHTTPHandlerWithBasicAuthMidlleware() {
|
||||
|
||||
h := asynqmon.New(asynqmon.Options{
|
||||
RootPath: "/monitoring",
|
||||
RedisConnOpt: asynq.RedisClientOpt{Addr: ":6379"},
|
||||
})
|
||||
|
||||
http.Handle(h.RootPath(), basicAuthHandler("username", "password", h))
|
||||
log.Fatal(http.ListenAndServe(":8000", nil)) // visit localhost:8000/monitoring to see asynqmon homepage
|
||||
}
|
||||
|
||||
func basicAuthHandler(u, p string, next http.Handler) http.Handler {
|
||||
var unauthorized = func(w http.ResponseWriter) {
|
||||
w.Header().Set("WWW-Authenticate", `Basic realm="restricted", charset="UTF-8"`)
|
||||
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
||||
}
|
||||
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
username, password, ok := r.BasicAuth()
|
||||
if !ok {
|
||||
unauthorized(w)
|
||||
return
|
||||
}
|
||||
|
||||
if !(u == username && p == password) {
|
||||
unauthorized(w)
|
||||
return
|
||||
}
|
||||
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
@ -31,7 +31,7 @@
|
||||
manifest.json provides metadata used when your web app is installed on a
|
||||
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
|
||||
-->
|
||||
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
|
||||
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" crossorigin="use-credentials" />
|
||||
<!--
|
||||
Notice the use of %PUBLIC_URL% in the tags above.
|
||||
It will be replaced with the URL of the `public` folder during the build.
|
||||
|
Loading…
x
Reference in New Issue
Block a user