-
Notifications
You must be signed in to change notification settings - Fork 0
/
hyper.go
161 lines (137 loc) · 3.47 KB
/
hyper.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright 2022 tink <[email protected]>. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package hyper
import (
"encoding/json"
"fmt"
"github.com/cyub/hyper/app"
_cache "github.com/cyub/hyper/cache"
_config "github.com/cyub/hyper/config"
"github.com/cyub/hyper/logger"
"github.com/cyub/hyper/mysql"
"github.com/cyub/hyper/pkg/cache"
"github.com/cyub/hyper/pkg/config"
"github.com/cyub/hyper/pkg/queue"
"github.com/cyub/hyper/pkg/registry"
_queue "github.com/cyub/hyper/queue"
_redis "github.com/cyub/hyper/redis"
_registry "github.com/cyub/hyper/registry"
"github.com/go-redis/redis/v7"
"github.com/jinzhu/gorm"
"github.com/robfig/cron/v3"
"github.com/sirupsen/logrus"
)
// NewApp return application
func NewApp(opts ...app.Option) *app.App {
return app.NewApp(opts...)
}
// DB return gorm.DB
func DB() *gorm.DB {
return mysql.Instance()
}
// Redis return redis.Client
func Redis() *redis.Client {
return _redis.Instance()
}
// RedisCluster return redis.ClusterClient
func RedisCluster() *redis.ClusterClient {
return _redis.ClusterInstance()
}
// Config return config.Config
func Config() *config.Config {
return _config.Instance()
}
// Logger return logrus.Logger
func Logger() *logrus.Logger {
return logger.Instance()
}
// Queue return queue.Queue
func Queue() queue.Queuer {
return _queue.Instance()
}
// Cache return cache.Cache
func Cache() cache.Cache {
return _cache.Instance()
}
// Registry return registry.Registry
func Registry() registry.Registry {
return _registry.Instance()
}
// NewJob return job
func NewJob(name string, data interface{}, tries int) (*queue.Job, error) {
payload, err := json.Marshal(data)
if err != nil {
return nil, fmt.Errorf("job payload can't marshal %s", err.Error())
}
job := &queue.Job{
Name: name,
Payload: payload,
MaxTries: tries,
}
return job, nil
}
// InQueue use for job enqueue
func InQueue(name string, data interface{}) error {
return InQueueWithRetry(name, data, 1)
}
// InQueueWithRetry use for job enqueue
func InQueueWithRetry(name string, data interface{}, tries int) error {
job, err := NewJob(name, data, tries)
if err != nil {
return err
}
return _queue.Instance().In(job)
}
// AddCronJob use add cron job
func AddCronJob(spec string, cmd func()) {
app.AddCronJob(spec, cmd)
}
// WithName use for set app's name
func WithName(name string) app.Option {
return func(o *app.Options) {
o.Name = name
}
}
// WithAddr use for set app's addr
func WithAddr(addr string) app.Option {
return func(o *app.Options) {
o.Addr = addr
}
}
// WithRunMode use for set app's run mode
func WithRunMode(mode string) app.Option {
return func(o *app.Options) {
o.RunMode = mode
}
}
// WithCfgAddr use for set app's config center addr
func WithCfgAddr(cfgAddr string) app.Option {
return func(o *app.Options) {
o.CfgCenterAddr = cfgAddr
}
}
// WithCfgPath use for set app's config center path
func WithCfgPath(cfgPath string) app.Option {
return func(o *app.Options) {
o.CfgCenterPath = cfgPath
}
}
// WithHideBanner use for set hide hyper banner
func WithHideBanner() app.Option {
return func(o *app.Options) {
o.ShowBanner = false
}
}
// WithCronEnable use for enable cron. default is disable
func WithCronEnable() app.Option {
return func(o *app.Options) {
o.CronEnable = true
}
}
// WithCron use for set user defined cron object
func WithCron(cron *cron.Cron) app.Option {
return func(o *app.Options) {
o.Cron = cron
}
}