From 933127cc0e175a86bc50d871647f20aa1e129222 Mon Sep 17 00:00:00 2001 From: Ken Hibino Date: Fri, 8 Jan 2021 07:16:48 -0800 Subject: [PATCH] Support command-line flags to configure redis connection and port to use --- Makefile | 4 ++-- main.go | 45 +++++++++++++++++++++++++++++++++--------- redis_info_handlers.go | 2 +- ui/src/api.ts | 6 +++++- 4 files changed, 44 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index 53ddbeb..9998416 100644 --- a/Makefile +++ b/Makefile @@ -2,10 +2,10 @@ assets: cd ./ui && yarn build # TODO: Update this once go1.16 is released. -go_bin: +go_binary: go1.16beta1 build -o asynqmon . # Target to build a release binary. -build: assets go_bin +build: assets go_binary diff --git a/main.go b/main.go index f17a9d1..186dfb3 100644 --- a/main.go +++ b/main.go @@ -1,8 +1,10 @@ package main import ( + "crypto/tls" "embed" "errors" + "flag" "fmt" "io/fs" "log" @@ -16,6 +18,23 @@ import ( "github.com/rs/cors" ) +// Command-line flags +var ( + flagPort int + flagRedisAddr string + flagRedisDB int + flagRedisPassword string + flagRedisTLS string +) + +func init() { + flag.IntVar(&flagPort, "port", 8080, "port number to use for web ui server") + flag.StringVar(&flagRedisAddr, "redis_addr", "localhost:6379", "address of redis server to connect to") + flag.IntVar(&flagRedisDB, "redis_db", 0, "redis database number") + flag.StringVar(&flagRedisPassword, "redis_password", "", "password to use when connecting to redis server") + flag.StringVar(&flagRedisTLS, "redis_tls", "", "server name for TLS validation used when connecting to redis server") +} + // staticFileServer implements the http.Handler interface, so we can use it // to respond to HTTP requests. The path to the static directory and // path to the index file within that static directory are used to @@ -65,22 +84,30 @@ func (srv *staticFileServer) indexFilePath() string { return filepath.Join(srv.staticDirPath, srv.indexFileName) } -const ( - addr = "127.0.0.1:8080" - redisAddr = "localhost:6379" // TODO: make this configurable -) - //go:embed ui/build/* var staticContents embed.FS func main() { + flag.Parse() + + var tlsConfig *tls.Config + if flagRedisTLS != "" { + tlsConfig = &tls.Config{ServerName: flagRedisTLS} + } + inspector := asynq.NewInspector(asynq.RedisClientOpt{ - Addr: redisAddr, + Addr: flagRedisAddr, + DB: flagRedisDB, + Password: flagRedisPassword, + TLSConfig: tlsConfig, }) defer inspector.Close() rdb := redis.NewClient(&redis.Options{ - Addr: redisAddr, + Addr: flagRedisAddr, + DB: flagRedisDB, + Password: flagRedisPassword, + TLSConfig: tlsConfig, }) defer rdb.Close() @@ -156,11 +183,11 @@ func main() { srv := &http.Server{ Handler: handler, - Addr: addr, + Addr: fmt.Sprintf(":%d", flagPort), WriteTimeout: 10 * time.Second, ReadTimeout: 10 * time.Second, } - fmt.Printf("Asynq Monitoring WebUI server is running on %s\n", addr) + fmt.Printf("Asynq Monitoring WebUI server is listening on port %d\n", flagPort) log.Fatal(srv.ListenAndServe()) } diff --git a/redis_info_handlers.go b/redis_info_handlers.go index 560e9ac..2dc0cbe 100644 --- a/redis_info_handlers.go +++ b/redis_info_handlers.go @@ -30,7 +30,7 @@ func newRedisInfoHandlerFunc(rdb *redis.Client) http.HandlerFunc { } info := parseRedisInfo(res) resp := RedisInfoResponse{ - Addr: redisAddr, + Addr: flagRedisAddr, Info: info, RawInfo: res, } diff --git a/ui/src/api.ts b/ui/src/api.ts index 4d08822..4d2f3f4 100644 --- a/ui/src/api.ts +++ b/ui/src/api.ts @@ -1,7 +1,11 @@ import axios from "axios"; import queryString from "query-string"; -const BASE_URL = "http://localhost:8080/api"; +// In production build, API server is on listening on the same port as +// the static file server. +// In developement, we assume that the API server is listening on port 8080. +const BASE_URL = + process.env.NODE_ENV === "production" ? "/api" : "http://localhost:8080/api"; export interface ListQueuesResponse { queues: Queue[];