/
app.go
44 lines (33 loc) 路 1.07 KB
/
app.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package server
import (
"log"
"net"
"net/http"
"github.com/linkernetworks/logger"
"github.com/linkernetworks/vortex/src/config"
"github.com/linkernetworks/vortex/src/serviceprovider"
)
// App is the structure to set config & service provider of APP
type App struct {
Config config.Config
ServiceProvider *serviceprovider.Container
}
// LoadConfig consumes a string of path to the json config file and read config file into Config.
func (a *App) LoadConfig(configPath string) *App {
if configPath == "" {
log.Fatal("-config option is required.")
}
a.Config = config.MustRead(configPath)
return a
}
// Start consumes two strings, host and port, invoke service initilization and serve on desired host:port
func (a *App) Start(host, port string) error {
a.InitilizeService()
bind := net.JoinHostPort(host, port)
return http.ListenAndServe(bind, a.AppRoute())
}
// InitilizeService weavering services with global variables inside server package
func (a *App) InitilizeService() {
logger.Setup(a.Config.Logger)
a.ServiceProvider = serviceprovider.New(a.Config)
}