-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathapp.go
More file actions
112 lines (95 loc) · 2.97 KB
/
app.go
File metadata and controls
112 lines (95 loc) · 2.97 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
package spine
import (
"strings"
"github.com/NARUBROWN/spine/core"
"github.com/NARUBROWN/spine/internal/bootstrap"
"github.com/NARUBROWN/spine/internal/event/consumer"
"github.com/NARUBROWN/spine/internal/router"
"github.com/NARUBROWN/spine/internal/ws"
"github.com/NARUBROWN/spine/pkg/boot"
)
type App interface {
// 생성자 선언
Constructor(constructors ...any)
// 라우트 선언
Route(method string, path string, handler any, opts ...router.RouteOption)
// 인터셉터 선언
Interceptor(interceptors ...core.Interceptor)
// HTTP Transport 확장 (Echo 등)
Transport(fn func(any))
// 독립 실행되는 Custom Transport 등록
RegisterTransport(t core.CustomTransport)
// 실행
Run(opts boot.Options) error
// 이벤트 소비자 레지스트리 반환
Consumers() *consumer.Registry
// 웹 소켓 레지스트리 반환
WebSocket() *ws.Registry
}
type app struct {
constructors []any
routes []router.RouteSpec
interceptors []core.Interceptor
transportHooks []func(any)
customTransports []core.CustomTransport
consumerRegistry *consumer.Registry
websocketRegistry *ws.Registry
}
func New() App {
return &app{}
}
func (a *app) Constructor(constructors ...any) {
a.constructors = append(a.constructors, constructors...)
}
func (a *app) Route(method string, path string, handler any, opts ...router.RouteOption) {
// HTTP 메서드를 대문자로 변환해 라우팅 시 대소문자 불일치 문제를 방지합니다.
method = strings.ToUpper(strings.TrimSpace(method))
spec := router.RouteSpec{
Method: method,
Path: path,
Handler: handler,
}
for _, opt := range opts {
opt(&spec)
}
a.routes = append(a.routes, spec)
}
func (a *app) Interceptor(interceptors ...core.Interceptor) {
a.interceptors = append(a.interceptors, interceptors...)
}
func (a *app) Transport(fn func(any)) {
a.transportHooks = append(a.transportHooks, fn)
}
func (a *app) RegisterTransport(t core.CustomTransport) {
a.customTransports = append(a.customTransports, t)
}
func (a *app) Run(opts boot.Options) error {
internalConfig := bootstrap.Config{
Address: opts.Address,
Constructors: a.constructors,
Routes: a.routes,
Interceptors: a.interceptors,
TransportHooks: a.transportHooks,
CustomTransports: a.customTransports,
EnableGracefulShutdown: opts.EnableGracefulShutdown,
ShutdownTimeout: opts.ShutdownTimeout,
Kafka: opts.Kafka,
RabbitMQ: opts.RabbitMQ,
ConsumerRegistry: a.consumerRegistry,
WebSocketRegistry: a.websocketRegistry,
HTTP: opts.HTTP,
}
return bootstrap.Run(internalConfig)
}
func (a *app) Consumers() *consumer.Registry {
if a.consumerRegistry == nil {
a.consumerRegistry = consumer.NewRegistry()
}
return a.consumerRegistry
}
func (a *app) WebSocket() *ws.Registry {
if a.websocketRegistry == nil {
a.websocketRegistry = ws.NewRegistry()
}
return a.websocketRegistry
}