Skip to content

Commit

Permalink
feat: add API global config for host,port
Browse files Browse the repository at this point in the history
Signed-off-by: Ales Verbic <[email protected]>
  • Loading branch information
verbotenj committed Nov 29, 2023
1 parent b2cc8e7 commit dbbac7a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
9 changes: 5 additions & 4 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"encoding/json"
"fmt"
"log"
"sync"
"time"
Expand All @@ -21,7 +22,7 @@ type APIv1 struct {
engine *gin.Engine
ApiGroup *gin.RouterGroup
Host string
Port string
Port uint
}

type APIRouteRegistrar interface {
Expand All @@ -43,7 +44,7 @@ func WithHost(host string) APIOption {
}
}

func WithPort(port string) APIOption {
func WithPort(port uint) APIOption {
return func(a *APIv1) {
a.Port = port
}
Expand All @@ -57,7 +58,7 @@ func New(debug bool, options ...APIOption) *APIv1 {
apiInstance = &APIv1{
engine: ConfigureRouter(debug),
Host: "0.0.0.0",
Port: "8080",
Port: 8080,
}
for _, opt := range options {
opt(apiInstance)
Expand Down Expand Up @@ -87,7 +88,7 @@ func (a *APIv1) Engine() *gin.Engine {
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
func (a *APIv1) Start() error {
address := a.Host + ":" + a.Port
address := fmt.Sprintf("%s:%d", a.Host, a.Port)
// Use buffered channel to not block goroutine
errChan := make(chan error, 1)

Expand Down
3 changes: 2 additions & 1 deletion cmd/snek/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ func main() {
// Create API instance with debug disabled
apiInstance := api.New(false,
api.WithGroup("/v1"),
api.WithPort("8080"))
api.WithHost(cfg.Api.ListenAddress),
api.WithPort(cfg.Api.ListenPort))

// Create pipeline
pipe := pipeline.New()
Expand Down
5 changes: 5 additions & 0 deletions configs/snek.yaml.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
---
# Example config

# Api server address
api
address: localhost
port: 8080

# Logging options
logging:
# Log level
Expand Down
10 changes: 10 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const (
)

type Config struct {
Api ApiConfig `yaml:"api"`
ConfigFile string `yaml:"-"`
Version bool `yaml:"-"`
Logging LoggingConfig `yaml:"logging"`
Expand All @@ -40,6 +41,11 @@ type Config struct {
Plugin map[string]map[string]map[interface{}]interface{} `yaml:"plugins"`
}

type ApiConfig struct {
ListenAddress string `yaml:"address" envconfig:"API_ADDRESS"`
ListenPort uint `yaml:"port" envconfig:"API_PORT"`
}

type LoggingConfig struct {
Level string `yaml:"level" envconfig:"LOGGING_LEVEL"`
}
Expand All @@ -51,6 +57,10 @@ type DebugConfig struct {

// Singleton config instance with default values
var globalConfig = &Config{
Api: ApiConfig{
ListenAddress: "0.0.0.0",
ListenPort: 8080,
},
Logging: LoggingConfig{
Level: "info",
},
Expand Down

0 comments on commit dbbac7a

Please sign in to comment.