Skip to content

Commit 2161329

Browse files
authored
Merge pull request #128 from blinklabs-io/feat-add-api-port-config
feat: add API global config for host,port
2 parents b2cc8e7 + 783cf26 commit 2161329

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

api/api.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package api
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"log"
67
"sync"
78
"time"
@@ -21,7 +22,7 @@ type APIv1 struct {
2122
engine *gin.Engine
2223
ApiGroup *gin.RouterGroup
2324
Host string
24-
Port string
25+
Port uint
2526
}
2627

2728
type APIRouteRegistrar interface {
@@ -43,7 +44,7 @@ func WithHost(host string) APIOption {
4344
}
4445
}
4546

46-
func WithPort(port string) APIOption {
47+
func WithPort(port uint) APIOption {
4748
return func(a *APIv1) {
4849
a.Port = port
4950
}
@@ -57,7 +58,7 @@ func New(debug bool, options ...APIOption) *APIv1 {
5758
apiInstance = &APIv1{
5859
engine: ConfigureRouter(debug),
5960
Host: "0.0.0.0",
60-
Port: "8080",
61+
Port: 8080,
6162
}
6263
for _, opt := range options {
6364
opt(apiInstance)
@@ -87,7 +88,7 @@ func (a *APIv1) Engine() *gin.Engine {
8788
// @license.name Apache 2.0
8889
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
8990
func (a *APIv1) Start() error {
90-
address := a.Host + ":" + a.Port
91+
address := fmt.Sprintf("%s:%d", a.Host, a.Port)
9192
// Use buffered channel to not block goroutine
9293
errChan := make(chan error, 1)
9394

cmd/snek/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ func main() {
118118
// Create API instance with debug disabled
119119
apiInstance := api.New(false,
120120
api.WithGroup("/v1"),
121-
api.WithPort("8080"))
121+
api.WithHost(cfg.Api.ListenAddress),
122+
api.WithPort(cfg.Api.ListenPort))
122123

123124
// Create pipeline
124125
pipe := pipeline.New()

configs/snek.yaml.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
---
22
# Example config
33

4+
# Api server address
5+
api:
6+
address: localhost
7+
port: 8080
8+
49
# Logging options
510
logging:
611
# Log level

internal/config/config.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const (
3131
)
3232

3333
type Config struct {
34+
Api ApiConfig `yaml:"api"`
3435
ConfigFile string `yaml:"-"`
3536
Version bool `yaml:"-"`
3637
Logging LoggingConfig `yaml:"logging"`
@@ -40,6 +41,11 @@ type Config struct {
4041
Plugin map[string]map[string]map[interface{}]interface{} `yaml:"plugins"`
4142
}
4243

44+
type ApiConfig struct {
45+
ListenAddress string `yaml:"address" envconfig:"API_ADDRESS"`
46+
ListenPort uint `yaml:"port" envconfig:"API_PORT"`
47+
}
48+
4349
type LoggingConfig struct {
4450
Level string `yaml:"level" envconfig:"LOGGING_LEVEL"`
4551
}
@@ -51,6 +57,10 @@ type DebugConfig struct {
5157

5258
// Singleton config instance with default values
5359
var globalConfig = &Config{
60+
Api: ApiConfig{
61+
ListenAddress: "0.0.0.0",
62+
ListenPort: 8080,
63+
},
5464
Logging: LoggingConfig{
5565
Level: "info",
5666
},

0 commit comments

Comments
 (0)