-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.go
136 lines (115 loc) · 3.42 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package rock
import (
"fmt"
"github.com/byte-power/rockgo/util"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/core/host"
)
const ConfigFilename = "rockgo"
type PanicHandler func(ctx iris.Context, err error)
type Application interface {
// Name returns app_name in rockgo config.
Name() string
// Iris returns iris application.
Iris() *iris.Application
// Set panic handler, it would working on sentry.repanic is true only.
SetPanicHandler(fn PanicHandler)
// Serve make Service to register handler.
//
// - Parameters:
// - name: for statsd
// - path: path from root, e.g. "foo" for "/foo"
Serve(name, path string) *Service
// ServeGroup make ServiceGroup to handle multiple Services like directory.
//
// - Parameters:
// - name: for statsd
// - path: directory name in path from root
ServeGroup(name, path string) *ServiceGroup
// Run server with <host> and multiple [conf].
//
// - Parameters:
// - host: [server name] with port. e.g. "mydomain.com:80" or ":8080" (equal "0.0.0.0:8080")
Run(host string, conf ...host.Configurator) error
}
// NewApplication would load config files from <configDir>, and then make Application with rockgo.yaml (or json) in <configDir>.
func NewApplication(configDir string) (Application, error) {
err := ImportConfigFilesFromDirectory(configDir)
if err != nil {
return nil, util.NewError(ErrNameApplicationInitFailure, err)
}
cfg := util.AnyToAnyMap(ConfigIn(ConfigFilename))
if cfg == nil {
return nil, fmt.Errorf("%s %s/%s.yaml (or json) not exists", ErrNameApplicationInitFailure, configDir, ConfigFilename)
}
a := &application{iris: iris.New()}
a.rootGroup = ServiceGroup{app: a}
err = a.init(cfg)
if err != nil {
return nil, util.NewError(ErrNameApplicationInitFailure, err)
}
a.iris.Use(newRockMiddleware(a))
return a, nil
}
type application struct {
name string
iris *iris.Application
rootGroup ServiceGroup
panicHandler PanicHandler
}
func (a *application) Name() string {
return a.name
}
func (a *application) init(cfg util.AnyMap) error {
appName := util.AnyToString(cfg["app_name"])
// create loggers
if cfgIt := util.AnyToStrMap(cfg["log"]); cfgIt != nil {
for name, v := range cfgIt {
vs := util.AnyToAnyMap(v)
if vs == nil {
continue
}
logger, err := parseLogger(appName, name, vs)
if err != nil {
return err
}
loggers[name] = logger
}
}
if it := loggers["default"]; it != nil {
defaultLogger = *it
}
if cfgIt := util.AnyToAnyMap(cfg["metric"]); cfgIt != nil {
initMetric(appName, cfgIt)
}
if cfgIt := util.AnyToAnyMap(cfg["sentry"]); cfgIt != nil {
sentryMW, err := newSentryMiddleware(parseSentryOption(cfgIt))
if err != nil {
return err
}
a.iris.Use(sentryMW)
}
if appName == "" {
defaultLogger.Warnf("'app_name' was not defined or it is empty in config (statsd and fluent depended on it).")
}
a.name = appName
return nil
}
func (a *application) Serve(name, path string) *Service {
return a.rootGroup.Serve(name, path)
}
func (a *application) ServeGroup(name, path string) *ServiceGroup {
return a.rootGroup.ServeGroup(name, path)
}
func (a *application) Iris() *iris.Application {
return a.iris
}
func (a *application) PanicHandler() PanicHandler {
return a.panicHandler
}
func (a *application) SetPanicHandler(fn PanicHandler) {
a.panicHandler = fn
}
func (a *application) Run(host string, conf ...host.Configurator) error {
return a.iris.Run(iris.Addr(host, conf...))
}