Skip to content

Commit

Permalink
get param from env instead
Browse files Browse the repository at this point in the history
  • Loading branch information
syahidfrd committed Jul 18, 2022
1 parent ce10a1e commit c934539
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@ docker pull syahidfrd/asynqmon-handler[:tag]
```
docker run --rm \
--name asynqmon-handler \
-e ASYNQMON_USER=admin \
-e ASYNQMON_PASSWORD=secure123 \
-p 3000:3000 \
syahidfrd/asynqmon-handler --auth-password=secure123
syahidfrd/asynqmon-handler
```

Here's the available flags:
Here's the available env:

| Flag | Default | Description |
|--------------------------|--------------|----------------------------|
| `--auth-username`(string) | `admin` | Basic auth username |
| `--auth-password`(string) | `admin` | Basic auth password |
| `--redis-addr`(string) | `:6379` | Address of redis server |
| Env | Default | Description |
|---------------------|---------|-------------------|
| `ASYNQMON_USER` | `admin` | Asynqmon user |
| `ASYNQMON_PASSWORD` | `admin` | Asynqmon password |
| `REDIS_ADDR` | `:6379` | Redis address |

Next, go to [localhost:3000](http://localhost:3000) and see Asynqmon dashboard
26 changes: 15 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package main
import (
"crypto/sha256"
"crypto/subtle"
"flag"
"log"
"net/http"
"os"

"github.com/hibiken/asynq"
"github.com/hibiken/asynqmon"
Expand All @@ -19,24 +19,20 @@ type application struct {
}

func main() {
authUsername := flag.String("auth-username", "admin", "basic auth username")
authPassword := flag.String("auth-password", "admin", "basic auth password")
redisAddr := flag.String("redis-addr", ":6379", "redis address")

flag.Parse()
asynqmonUser := getenv("ASYNQMON_USER", "admin")
asynqmonPassword := getenv("ASYNQMON_PASSWORD", "admin")
redisAddr := getenv("REDIS_ADDR", ":6379")

app := new(application)
app.auth.username = *authUsername
app.auth.password = *authPassword
app.auth.username = asynqmonUser
app.auth.password = asynqmonPassword

h := asynqmon.New(asynqmon.Options{
RootPath: "/",
RedisConnOpt: asynq.RedisClientOpt{Addr: *redisAddr},
RedisConnOpt: asynq.RedisClientOpt{Addr: redisAddr},
})

http.Handle(h.RootPath()+"/", app.basicAuth(h))

log.Print("Server is up on 3000 port")
log.Fatal(http.ListenAndServe(":3000", nil))
}

Expand All @@ -62,3 +58,11 @@ func (app *application) basicAuth(next *asynqmon.HTTPHandler) http.HandlerFunc {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
})
}

func getenv(key, fallback string) string {
value := os.Getenv(key)
if len(value) == 0 {
return fallback
}
return value
}

0 comments on commit c934539

Please sign in to comment.