-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinweb.go
107 lines (91 loc) · 3.11 KB
/
linweb.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
//Copyright 2021 Codexiaoyi
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
package linweb
import (
"net/http"
"sync"
"github.com/Codexiaoyi/linweb/interfaces"
"github.com/Codexiaoyi/linweb/pkg/model"
)
var (
pluginsModel interfaces.IModel
Cache interfaces.ICache
)
type Linweb struct {
markRouter interfaces.IRouter
markContext interfaces.IContext
// this middleware means an implement, every request need create a new middleware from New() by markMiddleware.
markMiddleware interfaces.IMiddleware
middlewareFunc []interfaces.HandlerFunc
markModel interfaces.IModel
markInject interfaces.IInjector
markCache interfaces.ICache
contextPool sync.Pool
}
// Create a new Linweb.
// Add customize plugins with method of plugins.go, otherwise use default plugins.
func NewLinweb(plugins ...CustomizePlugins) *Linweb {
lin := &Linweb{}
defaultPlugins()(lin)
for _, plugin := range plugins {
plugin(lin)
}
pluginsModel = lin.markModel
Cache = lin.markCache
return lin
}
// AddSingleton Add objects to DI container with a single instance in every request, they must all be of pointer type.
func (lin *Linweb) AddSingleton(objs ...interface{}) {
lin.markInject.AddSingleton(objs...)
}
// AddTransient Add objects to DI container with new instance in every request, they must all be of pointer type.
func (lin *Linweb) AddTransient(objs ...interface{}) {
lin.markInject.AddTransient(objs...)
}
// AddControllers Add all controllers, they must all be of pointer type
func (lin *Linweb) AddControllers(obj ...interface{}) {
lin.markRouter.AddControllers(obj)
}
// AddMiddlewares Add global middlewares
func (lin *Linweb) AddMiddlewares(middlewareFunc ...interfaces.HandlerFunc) {
lin.middlewareFunc = middlewareFunc
}
// Run you project to listen the "addr", enjoy yourself!
func (lin *Linweb) Run(addr string) error {
lin.contextPool.New = func() interface{} {
//clone a new context for current request
return lin.markContext.Clone()
}
return http.ListenAndServe(addr, lin)
}
// Serve HTTP.
func (lin *Linweb) ServeHTTP(w http.ResponseWriter, req *http.Request) {
//clone a new middleware to current request
middleware := lin.markMiddleware.Clone()
//add user middlewares
middleware.AddMiddlewares(lin.middlewareFunc...)
// get a context from pool
ctx := lin.contextPool.Get().(interfaces.IContext)
ctx.Reset(w, req, middleware)
lin.markRouter.Handle(ctx, lin.markInject)
//end handle, take context to pool
lin.contextPool.Put(ctx)
}
// NewModel Create a new model plugin.
func NewModel(m interface{}) interfaces.IModel {
if pluginsModel == nil {
pluginsModel = &model.Model{}
}
return pluginsModel.New(m)
}