Ready to refine the context and router bean.
This commit is contained in:
15
code/core/bean.go
Normal file
15
code/core/bean.go
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 系统中的Bean接口,即系统中单例模式
|
||||||
|
*/
|
||||||
|
type IBean interface {
|
||||||
|
//初始化方法
|
||||||
|
Init()
|
||||||
|
//系统清理方法
|
||||||
|
Cleanup()
|
||||||
|
//所有配置都加载完成后调用的方法,包括数据库加载完毕
|
||||||
|
Bootstrap()
|
||||||
|
//快速的Panic方法
|
||||||
|
PanicError(err error)
|
||||||
|
}
|
1
code/core/context.go
Normal file
1
code/core/context.go
Normal file
@ -0,0 +1 @@
|
|||||||
|
package core
|
11
code/core/controller.go
Normal file
11
code/core/controller.go
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
import "net/http"
|
||||||
|
|
||||||
|
type IController interface {
|
||||||
|
IBean
|
||||||
|
//注册自己固定的路由。
|
||||||
|
RegisterRoutes() map[string]func(writer http.ResponseWriter, request *http.Request)
|
||||||
|
//处理一些特殊的路由。
|
||||||
|
HandleRoutes(writer http.ResponseWriter, request *http.Request) (func(writer http.ResponseWriter, request *http.Request), bool)
|
||||||
|
}
|
7
code/core/global.go
Normal file
7
code/core/global.go
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
//该文件中记录的是应用系统中全局变量。主要有日志LOGGER和上下文CONTEXT
|
||||||
|
|
||||||
|
//日志系统必须高保
|
||||||
|
//全局唯一的日志对象(在main函数中初始化)
|
||||||
|
var LOGGER Logger
|
@ -1,9 +1,5 @@
|
|||||||
package core
|
package core
|
||||||
|
|
||||||
//日志系统必须高保
|
|
||||||
//全局唯一的日志对象(在main函数中初始化)
|
|
||||||
var LOGGER Logger
|
|
||||||
|
|
||||||
type Logger interface {
|
type Logger interface {
|
||||||
//处理日志的统一方法。
|
//处理日志的统一方法。
|
||||||
Log(prefix string, format string, v ...interface{})
|
Log(prefix string, format string, v ...interface{})
|
||||||
|
@ -8,13 +8,6 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
type IController interface {
|
|
||||||
IBean
|
|
||||||
//注册自己固定的路由。
|
|
||||||
RegisterRoutes() map[string]func(writer http.ResponseWriter, request *http.Request)
|
|
||||||
//处理一些特殊的路由。
|
|
||||||
HandleRoutes(writer http.ResponseWriter, request *http.Request) (func(writer http.ResponseWriter, request *http.Request), bool)
|
|
||||||
}
|
|
||||||
type BaseController struct {
|
type BaseController struct {
|
||||||
Bean
|
Bean
|
||||||
userDao *UserDao
|
userDao *UserDao
|
||||||
|
@ -8,17 +8,6 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
type IBean interface {
|
|
||||||
//初始化方法
|
|
||||||
Init()
|
|
||||||
//系统清理方法
|
|
||||||
Cleanup()
|
|
||||||
//所有配置都加载完成后调用的方法,包括数据库加载完毕
|
|
||||||
Bootstrap()
|
|
||||||
//快速的Panic方法
|
|
||||||
PanicError(err error)
|
|
||||||
}
|
|
||||||
|
|
||||||
type Bean struct {
|
type Bean struct {
|
||||||
logger core.Logger
|
logger core.Logger
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/eyebluecn/tank/code/config"
|
"github.com/eyebluecn/tank/code/config"
|
||||||
"github.com/eyebluecn/tank/code/core"
|
"github.com/eyebluecn/tank/code/core"
|
||||||
cache2 "github.com/eyebluecn/tank/code/tool/cache"
|
"github.com/eyebluecn/tank/code/tool/cache"
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
"reflect"
|
"reflect"
|
||||||
)
|
)
|
||||||
@ -17,9 +17,9 @@ type Context struct {
|
|||||||
//数据库连接
|
//数据库连接
|
||||||
DB *gorm.DB
|
DB *gorm.DB
|
||||||
//session缓存
|
//session缓存
|
||||||
SessionCache *cache2.Table
|
SessionCache *cache.Table
|
||||||
//各类的Bean Map。这里面是包含ControllerMap中所有元素
|
//各类的Bean Map。这里面是包含ControllerMap中所有元素
|
||||||
BeanMap map[string]IBean
|
BeanMap map[string]core.IBean
|
||||||
//只包含了Controller的map
|
//只包含了Controller的map
|
||||||
ControllerMap map[string]IController
|
ControllerMap map[string]IController
|
||||||
//处理所有路由请求
|
//处理所有路由请求
|
||||||
@ -30,10 +30,10 @@ type Context struct {
|
|||||||
func (this *Context) Init() {
|
func (this *Context) Init() {
|
||||||
|
|
||||||
//创建一个用于存储session的缓存。
|
//创建一个用于存储session的缓存。
|
||||||
this.SessionCache = cache2.NewTable()
|
this.SessionCache = cache.NewTable()
|
||||||
|
|
||||||
//初始化Map
|
//初始化Map
|
||||||
this.BeanMap = make(map[string]IBean)
|
this.BeanMap = make(map[string]core.IBean)
|
||||||
this.ControllerMap = make(map[string]IController)
|
this.ControllerMap = make(map[string]IController)
|
||||||
|
|
||||||
//注册各类Beans.在这个方法里面顺便把Controller装入ControllerMap中去。
|
//注册各类Beans.在这个方法里面顺便把Controller装入ControllerMap中去。
|
||||||
@ -74,12 +74,12 @@ func (this *Context) CloseDb() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//注册一个Bean
|
//注册一个Bean
|
||||||
func (this *Context) registerBean(bean IBean) {
|
func (this *Context) registerBean(bean core.IBean) {
|
||||||
|
|
||||||
typeOf := reflect.TypeOf(bean)
|
typeOf := reflect.TypeOf(bean)
|
||||||
typeName := typeOf.String()
|
typeName := typeOf.String()
|
||||||
|
|
||||||
if element, ok := bean.(IBean); ok {
|
if element, ok := bean.(core.IBean); ok {
|
||||||
|
|
||||||
err := fmt.Sprintf("【%s】已经被注册了,跳过。", typeName)
|
err := fmt.Sprintf("【%s】已经被注册了,跳过。", typeName)
|
||||||
if _, ok := this.BeanMap[typeName]; ok {
|
if _, ok := this.BeanMap[typeName]; ok {
|
||||||
@ -157,7 +157,7 @@ func (this *Context) registerBeans() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//从Map中获取某个Bean.
|
//从Map中获取某个Bean.
|
||||||
func (this *Context) GetBean(bean IBean) IBean {
|
func (this *Context) GetBean(bean core.IBean) core.IBean {
|
||||||
|
|
||||||
typeOf := reflect.TypeOf(bean)
|
typeOf := reflect.TypeOf(bean)
|
||||||
typeName := typeOf.String()
|
typeName := typeOf.String()
|
||||||
|
1
code/support/tank_context.go
Normal file
1
code/support/tank_context.go
Normal file
@ -0,0 +1 @@
|
|||||||
|
package support
|
Reference in New Issue
Block a user