Support command-line flags to configure redis connection and port to use

This commit is contained in:
Ken Hibino 2021-01-08 07:16:48 -08:00
parent fabf9ce10d
commit 933127cc0e
4 changed files with 44 additions and 13 deletions

View File

@ -2,10 +2,10 @@ assets:
cd ./ui && yarn build cd ./ui && yarn build
# TODO: Update this once go1.16 is released. # TODO: Update this once go1.16 is released.
go_bin: go_binary:
go1.16beta1 build -o asynqmon . go1.16beta1 build -o asynqmon .
# Target to build a release binary. # Target to build a release binary.
build: assets go_bin build: assets go_binary

45
main.go
View File

@ -1,8 +1,10 @@
package main package main
import ( import (
"crypto/tls"
"embed" "embed"
"errors" "errors"
"flag"
"fmt" "fmt"
"io/fs" "io/fs"
"log" "log"
@ -16,6 +18,23 @@ import (
"github.com/rs/cors" "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 // staticFileServer implements the http.Handler interface, so we can use it
// to respond to HTTP requests. The path to the static directory and // to respond to HTTP requests. The path to the static directory and
// path to the index file within that static directory are used to // 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) 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/* //go:embed ui/build/*
var staticContents embed.FS var staticContents embed.FS
func main() { func main() {
flag.Parse()
var tlsConfig *tls.Config
if flagRedisTLS != "" {
tlsConfig = &tls.Config{ServerName: flagRedisTLS}
}
inspector := asynq.NewInspector(asynq.RedisClientOpt{ inspector := asynq.NewInspector(asynq.RedisClientOpt{
Addr: redisAddr, Addr: flagRedisAddr,
DB: flagRedisDB,
Password: flagRedisPassword,
TLSConfig: tlsConfig,
}) })
defer inspector.Close() defer inspector.Close()
rdb := redis.NewClient(&redis.Options{ rdb := redis.NewClient(&redis.Options{
Addr: redisAddr, Addr: flagRedisAddr,
DB: flagRedisDB,
Password: flagRedisPassword,
TLSConfig: tlsConfig,
}) })
defer rdb.Close() defer rdb.Close()
@ -156,11 +183,11 @@ func main() {
srv := &http.Server{ srv := &http.Server{
Handler: handler, Handler: handler,
Addr: addr, Addr: fmt.Sprintf(":%d", flagPort),
WriteTimeout: 10 * time.Second, WriteTimeout: 10 * time.Second,
ReadTimeout: 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()) log.Fatal(srv.ListenAndServe())
} }

View File

@ -30,7 +30,7 @@ func newRedisInfoHandlerFunc(rdb *redis.Client) http.HandlerFunc {
} }
info := parseRedisInfo(res) info := parseRedisInfo(res)
resp := RedisInfoResponse{ resp := RedisInfoResponse{
Addr: redisAddr, Addr: flagRedisAddr,
Info: info, Info: info,
RawInfo: res, RawInfo: res,
} }

View File

@ -1,7 +1,11 @@
import axios from "axios"; import axios from "axios";
import queryString from "query-string"; 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 { export interface ListQueuesResponse {
queues: Queue[]; queues: Queue[];