-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathservice.go
85 lines (74 loc) · 1.79 KB
/
service.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
package beauty
import (
"net/http"
"github.com/rushteam/beauty/pkg/discover"
"github.com/rushteam/beauty/pkg/service/cron"
"github.com/rushteam/beauty/pkg/service/grpcserver"
"github.com/rushteam/beauty/pkg/service/webserver"
"google.golang.org/grpc"
)
func WithWebServer(addr string, mux http.Handler, opts ...ServiceOption) Option {
si := &discover.ServiceInfo{
Metadata: make(map[string]string),
}
for _, o := range opts {
o(si)
}
return WithService(webserver.New(
addr,
mux,
webserver.WithServiceName(si.Name),
webserver.WithMetadata(si.Metadata),
))
}
func WithGrpcServer(addr string, handler func(*grpc.Server), opts ...ServiceOption) Option {
si := &discover.ServiceInfo{
Metadata: make(map[string]string),
}
for _, o := range opts {
o(si)
}
return WithService(grpcserver.New(
addr,
handler,
grpcserver.WithServiceName(si.Name),
grpcserver.WithMetadata(si.Metadata),
))
}
func WithCrontab(opts ...cron.CronOptions) Option {
return WithService(cron.New(opts...))
}
// var WebLogger = middleware.Logger
// var WebRecoverer = middleware.Recoverer
// var DefaultMiddlewares = []func(next http.Handler) http.Handler{
// middleware.Logger,
// middleware.Recoverer,
// }
/*
type Route struct {
Method string
URI string
Handler http.HandlerFunc
}
type RouteOption func(r *chi.Mux)
func WithWebServerChi(addr string, routes []Route, opts ...RouteOption) Option {
r := chi.NewRouter()
for _, v := range opts {
v(r)
}
for _, v := range routes {
if v.Method == "" {
v.Method = http.MethodGet
}
r.Method(v.Method, v.URI, v.Handler)
}
return WithWebServer(addr, r)
}
func WithChiMiddleware(middlewares ...func(http.Handler) http.Handler) RouteOption {
return func(r *chi.Mux) {
for _, v := range middlewares {
r.Use(v)
}
}
}
*/