-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmojito.go
207 lines (170 loc) · 6.5 KB
/
mojito.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package mojito
import (
"fmt"
"log/slog"
"reflect"
"github.com/go-mojito/mojito/internal"
"github.com/go-mojito/mojito/pkg/router"
"github.com/go-mojito/mojito/pkg/stdlib"
"github.com/infinytum/injector"
)
func init() {
if err := injector.Singleton(func() Cache {
return stdlib.NewCache()
}); err != nil {
fmt.Println("Cannot register default cache dependency: ", err)
}
if err := injector.Singleton(func() Logger {
return slog.Default()
}); err != nil {
fmt.Println("Cannot register default logger dependency: ", err)
}
if err := injector.Singleton(func() Renderer {
return stdlib.NewRenderer()
}); err != nil {
fmt.Println("Cannot register default renderer dependency: ", err)
}
if err := injector.Singleton(func() Router {
return stdlib.NewRouter()
}); err != nil {
fmt.Println("Cannot register default router dependency: ", err)
}
router.RegisterStatefulHandlerArgFactory[Context](func(ctx router.Context, next router.HandlerFunc) (reflect.Value, error) {
return reflect.ValueOf(newMojitoContext(ctx)), nil
})
router.RegisterStatefulHandlerArgFactory[RendererContext](func(ctx router.Context, next router.HandlerFunc) (reflect.Value, error) {
return reflect.ValueOf(NewRenderContext(ctx)), nil
})
}
// DefaultCache will return the default cache instance for the mojito.Cache type
func DefaultCache() (cache Cache) {
if err := injector.InjectInto(&cache); err != nil {
DefaultLogger().With("dependency", "cache").Error(err.Error())
}
return
}
// DefaultLogger will return the default logger instance for the mojito.Logger type
func DefaultLogger() (logger Logger) {
if err := injector.InjectInto(&logger); err != nil {
fmt.Println("Cannot resolve default logger: ", err)
}
return
}
// DefaultRenderer will return the default renderer instance for the mojito.Renderer type
func DefaultRenderer() (renderer Renderer) {
if err := injector.InjectInto(&renderer); err != nil {
DefaultLogger().With("dependency", "renderer").Error(err.Error())
}
return
}
// DefaultRouter will return the default router instance for the mojito.Router type
func DefaultRouter() (router Router) {
if err := injector.InjectInto(&router); err != nil {
DefaultLogger().With("dependency", "router").Error(err.Error())
}
return
}
// Register will register a new dependency as default for the return type of the function
func Register(resolver interface{}, singleton bool) error {
if singleton {
return injector.Singleton(resolver)
}
return injector.Transient(resolver)
}
// RegisterNamed will register a new dependency under the given name
func RegisterNamed(name string, resolver interface{}, singleton bool) error {
if singleton {
return injector.Singleton(resolver, name)
}
return injector.Transient(resolver, name)
}
// Resolve will resolve a dependency based on the target objects type
func Resolve(obj interface{}) error {
return injector.InjectInto(obj)
}
// ResolveNamed will resolve a dependecy based on the given name
func ResolveNamed(name string, obj interface{}) error {
return injector.InjectInto(obj, name)
}
// SetResourcesDir will set the base directory where resources are located
func SetResourcesDir(path string) error {
internal.ResourcesDirVal = path
return nil
}
// ResourcesDir will return the base directory where resources are located
func ResourcesDir() string {
return internal.ResourcesDir()
}
/// Router Helpers
// GET will add a route to the default router for the get method
func GET(path string, handler interface{}) error {
return DefaultRouter().GET(path, handler)
}
// HEAD will add a route to the default router for the head method
func HEAD(path string, handler interface{}) error {
return DefaultRouter().HEAD(path, handler)
}
// POST will add a route to the default router for the post method
func POST(path string, handler interface{}) error {
return DefaultRouter().POST(path, handler)
}
// PUT will add a route to the default router for the put method
func PUT(path string, handler interface{}) error {
return DefaultRouter().PUT(path, handler)
}
// DELETE will add a route to the default router for the delete method
func DELETE(path string, handler interface{}) error {
return DefaultRouter().DELETE(path, handler)
}
// CONNECT will add a route to the default router for the connect method
func CONNECT(path string, handler interface{}) error {
return DefaultRouter().CONNECT(path, handler)
}
// OPTIONS will add a route to the default router for the options method
func OPTIONS(path string, handler interface{}) error {
return DefaultRouter().OPTIONS(path, handler)
}
// TRACE will add a route to the default router for the trace method
func TRACE(path string, handler interface{}) error {
return DefaultRouter().TRACE(path, handler)
}
// PATCH will add a route to the default router for the patch method
func PATCH(path string, handler interface{}) error {
return DefaultRouter().PATCH(path, handler)
}
// WithGroup will create a new route group for the given prefix on the default router
func WithGroup(prefix string, callback func(group router.Group)) error {
return DefaultRouter().WithGroup(prefix, callback)
}
// WithNotFoundHandler will set the not found handler for the default router
func WithNotFoundHandler(handler interface{}) error {
return DefaultRouter().WithNotFoundHandler(handler)
}
// WithMethodNotAllowedHandler will set the not allowed handler for the default router
func WithMethodNotAllowedHandler(handler interface{}) error {
return DefaultRouter().WithMethodNotAllowedHandler(handler)
}
// WithErrorHandler will add a error handler to the default router
func WithErrorHandler(handler interface{}) error {
return DefaultRouter().WithErrorHandler(handler)
}
// WithMiddleware will add a middleware to the default router
func WithMiddleware(handler interface{}) error {
return DefaultRouter().WithMiddleware(handler)
}
// WithRoute will add a new route with the given RouteMethod to the default router
func WithRoute(method string, path string, handler interface{}) error {
return DefaultRouter().WithRoute(method, path, handler)
}
// ListenAndServe will start an HTTP webserver on the given address with the default router
func ListenAndServe(address string) error {
return DefaultRouter().ListenAndServe(address)
}
// ListenAndServeTLS will start an HTTP/S webserver on the given address with the default router
func ListenAndServeTLS(address string, certFile string, keyFile string) error {
return DefaultRouter().ListenAndServeTLS(address, certFile, keyFile)
}
// Shutdown will gracefully shutdown the default router
func Shutdown() error {
return DefaultRouter().Shutdown()
}