Skip to content

Commit

Permalink
Make API server endpoint configurable in config file & CLI arg
Browse files Browse the repository at this point in the history
  • Loading branch information
keuin committed Jul 29, 2023
1 parent 03d9784 commit 1e8ea43
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
3 changes: 2 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
43 changes: 30 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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...")

Expand Down

0 comments on commit 1e8ea43

Please sign in to comment.