diff options
author | Keuin <[email protected]> | 2023-07-29 21:59:17 +0800 |
---|---|---|
committer | Keuin <[email protected]> | 2023-07-29 21:59:17 +0800 |
commit | 1e8ea437254a818452111cd52549032a6ea61f71 (patch) | |
tree | 71face8fad55e3499199a8f27a5e4807b86fa027 | |
parent | 03d9784bf682b05e3c4a46eb6cd32f4319e8636b (diff) |
Make API server endpoint configurable in config file & CLI argapi-server
-rw-r--r-- | config.go | 3 | ||||
-rw-r--r-- | main.go | 43 |
2 files changed, 32 insertions, 14 deletions
@@ -3,5 +3,6 @@ package main import "github.com/keuin/slbr/recording" type GlobalConfig struct { - Tasks []recording.TaskConfig `mapstructure:"tasks"` + Tasks []recording.TaskConfig `mapstructure:"tasks"` + ApiServer string `mapstructure:"api_server"` } @@ -7,6 +7,7 @@ Task lifecycle management are implemented in recording package. import ( "context" + "errors" "fmt" "github.com/akamensky/argparse" "github.com/keuin/slbr/api" @@ -75,6 +76,14 @@ func getTasks() (tasks []recording.TaskConfig) { Default: 4194304, }, ) + apiServer := parser.String( + "l", "listen", + &argparse.Options{ + Required: false, + Help: "Run HTTP API server on specific address and port", + Default: nil, + }, + ) err = parser.Parse(os.Args) if err != nil { @@ -126,9 +135,19 @@ func getTasks() (tasks []recording.TaskConfig) { return } globalConfig = &gc + if gc.ApiServer != "" && *apiServer != "" && *apiServer != gc.ApiServer { + panic(errors.New("API server endpoint is defined twice")) + } + if gc.ApiServer == "" { + gc.ApiServer = *apiServer + } return globalConfig.Tasks } + globalConfig = &GlobalConfig{} + if *apiServer != "" { + globalConfig.ApiServer = *apiServer + } // generate task list from cli taskCount := len(*rooms) tasks = make([]recording.TaskConfig, taskCount) @@ -171,20 +190,18 @@ func main() { } fmt.Println("") - apiAddr := os.Getenv("BIND_ADDR") - if apiAddr == "" { - apiAddr = ":8080" - } - apiAgent := &agentImpl{ - tasks: &tasks, - } - go func() { - logger.Println("Starting API server...") - err := api.StartServer(apiAddr, apiAgent) - if err != nil { - logger.Fatalf("Failed to start API server: %v", err) + if globalConfig.ApiServer != "" { + apiAgent := &agentImpl{ + tasks: &tasks, } - }() + go func() { + logger.Println("Starting API server...") + err := api.StartServer(globalConfig.ApiServer, apiAgent) + if err != nil { + logger.Fatalf("Failed to start API server: %v", err) + } + }() + } logger.Printf("Starting tasks...") |