-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.go
44 lines (35 loc) · 973 Bytes
/
init.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 service
import (
"context"
"errors"
"github.com/pkgz/logg"
"os"
"reflect"
)
// ARGS - default argument for application
type ARGS struct {
Port int `long:"port" env:"PORT" default:"8080" description:"service rest port"`
Debug bool `long:"debug" env:"DEBUG" description:"debug mode"`
}
var (
ErrMissingArgs = errors.New("helper.ARGS not find in args")
)
// Init - allows easily initialize app. Will parse environment arguments, and will initialize application context with cancel.
func Init(args interface{}) (context.Context, context.CancelFunc, error) {
if err := ParseEnv(args); err != nil {
return nil, nil, err
}
ctx, cancel := ContextWithCancel()
s := reflect.ValueOf(args)
value := s.Elem()
argsValue := value.FieldByName("ARGS")
if !argsValue.IsValid() {
return nil, nil, ErrMissingArgs
}
var debug = argsValue.FieldByName("Debug").Bool()
logg.NewGlobal(os.Stdout)
if debug {
logg.DebugMode()
}
return ctx, cancel, nil
}