hibiken / asynqmon

Web UI for Asynq task queue
MIT License
644 stars 142 forks source link

feature: Basic auth for web console #313

Open zekiahmetbayar opened 11 months ago

zekiahmetbayar commented 11 months ago

Hi,

I did not want everyone to read the data I wrote to the tasks, and encrypting and decrypting this data slowed down my application. For this reason, I researched whether there was a basic auth option and came across this. #78

When I saw that it was not yet available, I wanted to make an improvement for this application and I added basic auth.

Features:

zekiahmetbayar commented 10 months ago

Hey @hibiken, are you interested in this pr?

I think there are a lot of people who need it :) #78

sh0umik commented 10 months ago

Hey @hibiken, are you interested in this pr?

I think there are a lot of people who need it :) #78

thx for the PR. I think this should be merged but its the author's wish, however, I will be using this in my code. thx a lot

thanhfphan commented 5 months ago

any update???

emilhakobian commented 2 months ago

I've found another solution, without modifying the base code. Given the example with gorilla mux.

First - create asynqmon handler

var h = asynqmon.New(asynqmon.Options{
    RootPath: "/monitoring",
    RedisConnOpt: asynq.RedisClientOpt{
        Addr:     "localhost:6379",
        DB:       0,
        Password: "",
})

Second - create gorilla mux sub-router with your middleware and attach handler for that route.

monitoring := r.PathPrefix(h.RootPath()).Subrouter()
monitoring.Use(isAuthenticated, isAuthorised, exampleMiddleware)
monitoring.NewRoute().Handler(h)

[!IMPORTANT] It is important to use NewRoute(), in order to be able to attach he handler.

Bellow is the exampleMiddleware where you can add your logic.

var exampleMiddleware = func(next http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        allowed := true

                 if !allowed {
                    w.WriteHeader(http.StatusForbidden)
                    return
                 }

        next.ServeHTTP(w, r.WithContext(ctx))

    })
}