File tree Expand file tree Collapse file tree 4 files changed +22
-5
lines changed Expand file tree Collapse file tree 4 files changed +22
-5
lines changed Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ package api
2
2
3
3
import (
4
4
"encoding/json"
5
+ "fmt"
5
6
"log"
6
7
"sync"
7
8
"time"
@@ -21,7 +22,7 @@ type APIv1 struct {
21
22
engine * gin.Engine
22
23
ApiGroup * gin.RouterGroup
23
24
Host string
24
- Port string
25
+ Port uint
25
26
}
26
27
27
28
type APIRouteRegistrar interface {
@@ -43,7 +44,7 @@ func WithHost(host string) APIOption {
43
44
}
44
45
}
45
46
46
- func WithPort (port string ) APIOption {
47
+ func WithPort (port uint ) APIOption {
47
48
return func (a * APIv1 ) {
48
49
a .Port = port
49
50
}
@@ -57,7 +58,7 @@ func New(debug bool, options ...APIOption) *APIv1 {
57
58
apiInstance = & APIv1 {
58
59
engine : ConfigureRouter (debug ),
59
60
Host : "0.0.0.0" ,
60
- Port : " 8080" ,
61
+ Port : 8080 ,
61
62
}
62
63
for _ , opt := range options {
63
64
opt (apiInstance )
@@ -87,7 +88,7 @@ func (a *APIv1) Engine() *gin.Engine {
87
88
// @license.name Apache 2.0
88
89
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
89
90
func (a * APIv1 ) Start () error {
90
- address := a . Host + ":" + a .Port
91
+ address := fmt . Sprintf ( "%s:%d" , a . Host , a .Port )
91
92
// Use buffered channel to not block goroutine
92
93
errChan := make (chan error , 1 )
93
94
Original file line number Diff line number Diff line change @@ -118,7 +118,8 @@ func main() {
118
118
// Create API instance with debug disabled
119
119
apiInstance := api .New (false ,
120
120
api .WithGroup ("/v1" ),
121
- api .WithPort ("8080" ))
121
+ api .WithHost (cfg .Api .ListenAddress ),
122
+ api .WithPort (cfg .Api .ListenPort ))
122
123
123
124
// Create pipeline
124
125
pipe := pipeline .New ()
Original file line number Diff line number Diff line change 1
1
---
2
2
# Example config
3
3
4
+ # Api server address
5
+ api:
6
+ address: localhost
7
+ port: 8080
8
+
4
9
# Logging options
5
10
logging:
6
11
# Log level
Original file line number Diff line number Diff line change @@ -31,6 +31,7 @@ const (
31
31
)
32
32
33
33
type Config struct {
34
+ Api ApiConfig `yaml:"api"`
34
35
ConfigFile string `yaml:"-"`
35
36
Version bool `yaml:"-"`
36
37
Logging LoggingConfig `yaml:"logging"`
@@ -40,6 +41,11 @@ type Config struct {
40
41
Plugin map [string ]map [string ]map [interface {}]interface {} `yaml:"plugins"`
41
42
}
42
43
44
+ type ApiConfig struct {
45
+ ListenAddress string `yaml:"address" envconfig:"API_ADDRESS"`
46
+ ListenPort uint `yaml:"port" envconfig:"API_PORT"`
47
+ }
48
+
43
49
type LoggingConfig struct {
44
50
Level string `yaml:"level" envconfig:"LOGGING_LEVEL"`
45
51
}
@@ -51,6 +57,10 @@ type DebugConfig struct {
51
57
52
58
// Singleton config instance with default values
53
59
var globalConfig = & Config {
60
+ Api : ApiConfig {
61
+ ListenAddress : "0.0.0.0" ,
62
+ ListenPort : 8080 ,
63
+ },
54
64
Logging : LoggingConfig {
55
65
Level : "info" ,
56
66
},
You can’t perform that action at this time.
0 commit comments