2021-10-03 07:34:21 +05:30
|
|
|
package asynqmon_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/hibiken/asynq"
|
|
|
|
"github.com/hibiken/asynqmon"
|
|
|
|
)
|
|
|
|
|
2021-10-10 06:33:38 -07:00
|
|
|
func ExampleHTTPHandler() {
|
2021-10-04 20:48:00 +05:30
|
|
|
h := asynqmon.New(asynqmon.Options{
|
2021-10-10 06:33:38 -07:00
|
|
|
RootPath: "/monitoring",
|
2021-10-03 07:34:21 +05:30
|
|
|
RedisConnOpt: asynq.RedisClientOpt{Addr: ":6379"},
|
|
|
|
})
|
|
|
|
|
2021-10-10 06:33:38 -07:00
|
|
|
http.Handle(h.RootPath(), h)
|
|
|
|
log.Fatal(http.ListenAndServe(":8000", nil)) // visit localhost:8000/monitoring to see asynqmon homepage
|
2021-10-03 07:34:21 +05:30
|
|
|
}
|
2022-12-29 02:05:45 +07:00
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|