-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.go
38 lines (33 loc) · 836 Bytes
/
run.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
package context
import (
"flag"
"os"
"runtime/debug"
"github.com/fiamma-chain/fiamma-go-sdk/log"
)
// Run service
func Run(handle func(Context) error) {
var h bool
var c string
flag.BoolVar(&h, "h", false, "this help")
flag.StringVar(&c, "c", "etc/fiamma/conf.yml", "the configuration file")
flag.Parse()
if h {
flag.Usage()
return
}
context := NewContext(c)
defer func() {
if r := recover(); r != nil {
context.Log().Error("service is stopped with panic", log.Any("panic", r), log.Any("stack", string(debug.Stack())))
}
}()
pwd, _ := os.Getwd()
context.Log().Info("service starting", log.Any("args", os.Args), log.Any("pwd", pwd))
err := handle(context)
if err != nil {
context.Log().Error("service has stopped with error", log.Error(err))
} else {
context.Log().Info("service has stopped")
}
}