136 lines
2.9 KiB
Go
136 lines
2.9 KiB
Go
|
package lightCore
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"github.com/gin-gonic/gin"
|
||
|
"net/http"
|
||
|
"reflect"
|
||
|
)
|
||
|
|
||
|
type LightCore struct {
|
||
|
gin *gin.Engine
|
||
|
g *gin.RouterGroup
|
||
|
props []interface{}
|
||
|
}
|
||
|
|
||
|
func StartEngine() *LightCore {
|
||
|
gin.SetMode(ConfigValue.Server.RunMode)
|
||
|
g := &LightCore{gin: gin.New(), props: make([]interface{}, 0)}
|
||
|
g.gin.Use(gin.Logger(), gin.Recovery())
|
||
|
return g
|
||
|
}
|
||
|
func (t *LightCore) GetGin() *gin.Engine {
|
||
|
return t.gin
|
||
|
}
|
||
|
|
||
|
func (t *LightCore) Launch() {
|
||
|
err := t.gin.Run(fmt.Sprintf(":%d", ConfigValue.Server.HttpPort))
|
||
|
if err != nil {
|
||
|
panic("Gin Launch Error")
|
||
|
}
|
||
|
|
||
|
//server := &http.Server{
|
||
|
// Addr: fmt.Sprintf(":%d", addr),
|
||
|
// Handler: t,
|
||
|
// MaxHeaderBytes: 1 << 20,
|
||
|
//}
|
||
|
//err := server.ListenAndServe()
|
||
|
//if err != nil {
|
||
|
// panic("Gin Launch Error")
|
||
|
//}
|
||
|
}
|
||
|
|
||
|
func (t *LightCore) Attach(f Fairing) *LightCore {
|
||
|
t.gin.Use(func(context *gin.Context) {
|
||
|
err := f.OnRequest(context)
|
||
|
if err != nil {
|
||
|
context.AbortWithStatusJSON(http.StatusOK, gin.H{"error": "onRequest"})
|
||
|
} else {
|
||
|
context.Next()
|
||
|
}
|
||
|
})
|
||
|
return t
|
||
|
}
|
||
|
func (t *LightCore) LoadHtml(path string) *LightCore {
|
||
|
t.gin.LoadHTMLGlob(path)
|
||
|
return t
|
||
|
}
|
||
|
|
||
|
func (t *LightCore) StaticFS(path string, fs http.FileSystem) *LightCore {
|
||
|
t.gin.StaticFS(path, fs)
|
||
|
return t
|
||
|
}
|
||
|
|
||
|
func (t *LightCore) Beans(beans ...interface{}) *LightCore {
|
||
|
t.props = append(t.props, beans...)
|
||
|
return t
|
||
|
}
|
||
|
|
||
|
func (t *LightCore) Handle(httpMethod, relativePath string, handler interface{}, mids ...gin.HandlerFunc) *LightCore {
|
||
|
if h := Convert(handler); h != nil {
|
||
|
if len(mids) > 0 {
|
||
|
mids = append(mids, h)
|
||
|
t.g.Handle(httpMethod, relativePath, mids...)
|
||
|
} else {
|
||
|
t.g.Handle(httpMethod, relativePath, h)
|
||
|
}
|
||
|
|
||
|
}
|
||
|
return t
|
||
|
}
|
||
|
|
||
|
func (t *LightCore) MountRaw(f func(s *gin.Engine)) *LightCore {
|
||
|
f(t.GetGin())
|
||
|
return t
|
||
|
}
|
||
|
|
||
|
func (t *LightCore) Mount(group string, errorHandlerFunc []gin.HandlerFunc, classes ...IClass) *LightCore {
|
||
|
t.g = t.gin.Group(group)
|
||
|
if len(errorHandlerFunc) > 0 {
|
||
|
t.g.Use(errorHandlerFunc...)
|
||
|
}
|
||
|
for _, class := range classes {
|
||
|
class.Build(t)
|
||
|
//其实就是找到相同类型的数据就赋值上去
|
||
|
t.setProp(class)
|
||
|
}
|
||
|
return t
|
||
|
}
|
||
|
|
||
|
func (t *LightCore) Mounts(routers []*Routers) *LightCore {
|
||
|
for _, router := range routers {
|
||
|
t.Mount(router.groupPath, router.cs.errorHandler, router.cs.Handles()...)
|
||
|
}
|
||
|
return t
|
||
|
}
|
||
|
|
||
|
func (t *LightCore) SetNoRoute(f func(context *gin.Context)) *LightCore {
|
||
|
t.gin.NoRoute(f)
|
||
|
return t
|
||
|
}
|
||
|
|
||
|
// 查找对应名字的属性
|
||
|
func (t *LightCore) getProp(tp reflect.Type) interface{} {
|
||
|
for _, p := range t.props {
|
||
|
if tp == reflect.TypeOf(p) {
|
||
|
return p
|
||
|
}
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (t *LightCore) setProp(class IClass) {
|
||
|
vClass := reflect.ValueOf(class).Elem()
|
||
|
for i := 0; i < vClass.NumField(); i++ {
|
||
|
f := vClass.Field(i)
|
||
|
if !f.IsNil() || f.Kind() != reflect.Ptr {
|
||
|
//已经赋值了,不需要初始化了。
|
||
|
continue
|
||
|
}
|
||
|
if p := t.getProp(f.Type()); p != nil {
|
||
|
f.Set(reflect.New(f.Type().Elem()))
|
||
|
f.Elem().Set(reflect.ValueOf(p).Elem())
|
||
|
}
|
||
|
}
|
||
|
}
|