-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.go
More file actions
118 lines (102 loc) · 2.24 KB
/
app.go
File metadata and controls
118 lines (102 loc) · 2.24 KB
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
package ginny
import (
"context"
"fmt"
"github.com/google/wire"
"github.com/goriller/ginny/config"
"github.com/goriller/ginny/logger"
"github.com/goriller/ginny/server"
"github.com/pkg/errors"
"github.com/spf13/viper"
"go.uber.org/zap"
)
var (
// AppProviderSet
AppProviderSet = wire.NewSet(
logger.Default,
config.ConfigProviderSet,
NewOption, NewApp,
)
Logo = `
┌─┐┬┌┐┌┌┐┌┬ ┬
│ ┬│││││││└┬┘
└─┘┴┘└┘┘└┘ ┴
https://github.com/goriller/ginny
`
)
func init() {
fmt.Printf("\x1b[35;1m%s\x1b[0m\n", Logo)
}
// RegistrarFunc
type RegistrarFunc func(app *Application) error
// Application
type Application struct {
Name string
Version string
Option *Option
Logger *zap.Logger
Server *server.Server
Ctx context.Context
regFunc RegistrarFunc
}
// Option
type Option struct {
Name string
Version string
GrpcAddr string
HttpAddr string
MetricsAddr string
}
// NewOption
func NewOption(v *viper.Viper) (*Option, error) {
var err error
o := new(Option)
if err = v.UnmarshalKey("app", o); err != nil {
return nil, errors.Wrap(err, "unmarshal app option error")
}
return o, nil
}
// NewApp
func NewApp(
ctx context.Context,
option *Option,
logger *zap.Logger,
regFunc RegistrarFunc,
opts ...server.Option,
) (*Application, error) {
app := &Application{
Name: option.Name,
Version: option.Version,
Option: option,
regFunc: regFunc,
Logger: logger.With(zap.String("action", "App")),
}
opt := []server.Option{
server.WithGrpcAddr(option.GrpcAddr),
}
if option.HttpAddr != "" {
opts = append(opts,
server.WithHttpAddr(option.HttpAddr),
)
}
if option.MetricsAddr != "" {
opts = append(opts,
server.WithMetricsAddr(option.MetricsAddr),
)
}
opts = append(opts, opt...)
app.Server = server.NewServer(ctx, logger, opts...)
return app, nil
}
// Start
func (a *Application) Start(ctx context.Context) error {
if err := a.regFunc(a); err != nil {
return err
}
a.Server.Start(ctx)
return nil
}
// Stop
func (a *Application) Stop(ctx context.Context) error {
return a.Server.Close(ctx)
}