Refine the base bean.
This commit is contained in:
parent
aaf7578290
commit
e043b6b8d7
@ -3,7 +3,7 @@ package core
|
|||||||
/**
|
/**
|
||||||
* 系统中的Bean接口,即系统中单例模式
|
* 系统中的Bean接口,即系统中单例模式
|
||||||
*/
|
*/
|
||||||
type IBean interface {
|
type Bean interface {
|
||||||
//初始化方法
|
//初始化方法
|
||||||
Init()
|
Init()
|
||||||
//系统清理方法
|
//系统清理方法
|
||||||
|
@ -14,7 +14,7 @@ type Context interface {
|
|||||||
GetDB() *gorm.DB
|
GetDB() *gorm.DB
|
||||||
|
|
||||||
//获取一个Bean
|
//获取一个Bean
|
||||||
GetBean(bean IBean) IBean
|
GetBean(bean Bean) Bean
|
||||||
|
|
||||||
//获取全局的Session缓存
|
//获取全局的Session缓存
|
||||||
GetSessionCache() *cache.Table
|
GetSessionCache() *cache.Table
|
||||||
|
@ -3,7 +3,7 @@ package core
|
|||||||
import "net/http"
|
import "net/http"
|
||||||
|
|
||||||
type IController interface {
|
type IController interface {
|
||||||
IBean
|
Bean
|
||||||
//注册自己固定的路由。
|
//注册自己固定的路由。
|
||||||
RegisterRoutes() map[string]func(writer http.ResponseWriter, request *http.Request)
|
RegisterRoutes() map[string]func(writer http.ResponseWriter, request *http.Request)
|
||||||
//处理一些特殊的路由。
|
//处理一些特殊的路由。
|
||||||
|
@ -11,7 +11,7 @@ import (
|
|||||||
|
|
||||||
//@Service
|
//@Service
|
||||||
type AlienService struct {
|
type AlienService struct {
|
||||||
Bean
|
BaseBean
|
||||||
matterDao *MatterDao
|
matterDao *MatterDao
|
||||||
matterService *MatterService
|
matterService *MatterService
|
||||||
userDao *UserDao
|
userDao *UserDao
|
||||||
@ -23,7 +23,7 @@ type AlienService struct {
|
|||||||
|
|
||||||
//初始化方法
|
//初始化方法
|
||||||
func (this *AlienService) Init() {
|
func (this *AlienService) Init() {
|
||||||
this.Bean.Init()
|
this.BaseBean.Init()
|
||||||
|
|
||||||
//手动装填本实例的Bean. 这里必须要用中间变量方可。
|
//手动装填本实例的Bean. 这里必须要用中间变量方可。
|
||||||
b := core.CONTEXT.GetBean(this.matterDao)
|
b := core.CONTEXT.GetBean(this.matterDao)
|
||||||
|
@ -7,30 +7,30 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Bean struct {
|
type BaseBean struct {
|
||||||
logger core.Logger
|
logger core.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Bean) Init() {
|
func (this *BaseBean) Init() {
|
||||||
this.logger = core.LOGGER
|
this.logger = core.LOGGER
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Bean) Bootstrap() {
|
func (this *BaseBean) Bootstrap() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//系统大清理,一般时产品即将上线时,清除脏数据,只执行一次。
|
//系统大清理,一般时产品即将上线时,清除脏数据,只执行一次。
|
||||||
func (this *Bean) Cleanup() {
|
func (this *BaseBean) Cleanup() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//处理错误的统一方法 可以省去if err!=nil 这段代码
|
//处理错误的统一方法 可以省去if err!=nil 这段代码
|
||||||
func (this *Bean) PanicError(err error) {
|
func (this *BaseBean) PanicError(err error) {
|
||||||
util.PanicError(err)
|
util.PanicError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
//能找到一个user就找到一个
|
//能找到一个user就找到一个
|
||||||
func (this *Bean) findUser(writer http.ResponseWriter, request *http.Request) *User {
|
func (this *BaseBean) findUser(writer http.ResponseWriter, request *http.Request) *User {
|
||||||
|
|
||||||
//验证用户是否已经登录。
|
//验证用户是否已经登录。
|
||||||
//登录身份有效期以数据库中记录的为准
|
//登录身份有效期以数据库中记录的为准
|
||||||
@ -62,7 +62,7 @@ func (this *Bean) findUser(writer http.ResponseWriter, request *http.Request) *U
|
|||||||
}
|
}
|
||||||
|
|
||||||
//获取当前登录的用户,找不到就返回登录错误
|
//获取当前登录的用户,找不到就返回登录错误
|
||||||
func (this *Bean) checkUser(writer http.ResponseWriter, request *http.Request) *User {
|
func (this *BaseBean) checkUser(writer http.ResponseWriter, request *http.Request) *User {
|
||||||
if this.findUser(writer, request) == nil {
|
if this.findUser(writer, request) == nil {
|
||||||
panic(result.ConstWebResult(result.CODE_WRAPPER_LOGIN))
|
panic(result.ConstWebResult(result.CODE_WRAPPER_LOGIN))
|
||||||
} else {
|
} else {
|
@ -10,14 +10,14 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type BaseController struct {
|
type BaseController struct {
|
||||||
Bean
|
BaseBean
|
||||||
userDao *UserDao
|
userDao *UserDao
|
||||||
sessionDao *SessionDao
|
sessionDao *SessionDao
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *BaseController) Init() {
|
func (this *BaseController) Init() {
|
||||||
|
|
||||||
this.Bean.Init()
|
this.BaseBean.Init()
|
||||||
|
|
||||||
//手动装填本实例的Bean.
|
//手动装填本实例的Bean.
|
||||||
b := core.CONTEXT.GetBean(this.userDao)
|
b := core.CONTEXT.GetBean(this.userDao)
|
||||||
|
@ -3,7 +3,7 @@ package rest
|
|||||||
import "github.com/eyebluecn/tank/code/tool/builder"
|
import "github.com/eyebluecn/tank/code/tool/builder"
|
||||||
|
|
||||||
type BaseDao struct {
|
type BaseDao struct {
|
||||||
Bean
|
BaseBean
|
||||||
}
|
}
|
||||||
|
|
||||||
//根据一个sortMap,获取到order字符串
|
//根据一个sortMap,获取到order字符串
|
||||||
|
@ -9,7 +9,7 @@ import (
|
|||||||
|
|
||||||
//@Service
|
//@Service
|
||||||
type DashboardService struct {
|
type DashboardService struct {
|
||||||
Bean
|
BaseBean
|
||||||
dashboardDao *DashboardDao
|
dashboardDao *DashboardDao
|
||||||
footprintDao *FootprintDao
|
footprintDao *FootprintDao
|
||||||
matterDao *MatterDao
|
matterDao *MatterDao
|
||||||
@ -19,7 +19,7 @@ type DashboardService struct {
|
|||||||
|
|
||||||
//初始化方法
|
//初始化方法
|
||||||
func (this *DashboardService) Init() {
|
func (this *DashboardService) Init() {
|
||||||
this.Bean.Init()
|
this.BaseBean.Init()
|
||||||
|
|
||||||
//手动装填本实例的Bean. 这里必须要用中间变量方可。
|
//手动装填本实例的Bean. 这里必须要用中间变量方可。
|
||||||
b := core.CONTEXT.GetBean(this.dashboardDao)
|
b := core.CONTEXT.GetBean(this.dashboardDao)
|
||||||
|
@ -23,14 +23,14 @@ import (
|
|||||||
*/
|
*/
|
||||||
//@Service
|
//@Service
|
||||||
type DavService struct {
|
type DavService struct {
|
||||||
Bean
|
BaseBean
|
||||||
matterDao *MatterDao
|
matterDao *MatterDao
|
||||||
matterService *MatterService
|
matterService *MatterService
|
||||||
}
|
}
|
||||||
|
|
||||||
//初始化方法
|
//初始化方法
|
||||||
func (this *DavService) Init() {
|
func (this *DavService) Init() {
|
||||||
this.Bean.Init()
|
this.BaseBean.Init()
|
||||||
|
|
||||||
//手动装填本实例的Bean. 这里必须要用中间变量方可。
|
//手动装填本实例的Bean. 这里必须要用中间变量方可。
|
||||||
b := core.CONTEXT.GetBean(this.matterDao)
|
b := core.CONTEXT.GetBean(this.matterDao)
|
||||||
|
@ -12,14 +12,14 @@ import (
|
|||||||
|
|
||||||
//@Service
|
//@Service
|
||||||
type FootprintService struct {
|
type FootprintService struct {
|
||||||
Bean
|
BaseBean
|
||||||
footprintDao *FootprintDao
|
footprintDao *FootprintDao
|
||||||
userDao *UserDao
|
userDao *UserDao
|
||||||
}
|
}
|
||||||
|
|
||||||
//初始化方法
|
//初始化方法
|
||||||
func (this *FootprintService) Init() {
|
func (this *FootprintService) Init() {
|
||||||
this.Bean.Init()
|
this.BaseBean.Init()
|
||||||
|
|
||||||
//手动装填本实例的Bean. 这里必须要用中间变量方可。
|
//手动装填本实例的Bean. 这里必须要用中间变量方可。
|
||||||
b := core.CONTEXT.GetBean(this.footprintDao)
|
b := core.CONTEXT.GetBean(this.footprintDao)
|
||||||
|
@ -15,7 +15,7 @@ import (
|
|||||||
|
|
||||||
//@Service
|
//@Service
|
||||||
type ImageCacheService struct {
|
type ImageCacheService struct {
|
||||||
Bean
|
BaseBean
|
||||||
imageCacheDao *ImageCacheDao
|
imageCacheDao *ImageCacheDao
|
||||||
userDao *UserDao
|
userDao *UserDao
|
||||||
matterDao *MatterDao
|
matterDao *MatterDao
|
||||||
@ -23,7 +23,7 @@ type ImageCacheService struct {
|
|||||||
|
|
||||||
//初始化方法
|
//初始化方法
|
||||||
func (this *ImageCacheService) Init() {
|
func (this *ImageCacheService) Init() {
|
||||||
this.Bean.Init()
|
this.BaseBean.Init()
|
||||||
|
|
||||||
//手动装填本实例的Bean. 这里必须要用中间变量方可。
|
//手动装填本实例的Bean. 这里必须要用中间变量方可。
|
||||||
b := core.CONTEXT.GetBean(this.imageCacheDao)
|
b := core.CONTEXT.GetBean(this.imageCacheDao)
|
||||||
|
@ -18,7 +18,7 @@ import (
|
|||||||
*/
|
*/
|
||||||
//@Service
|
//@Service
|
||||||
type MatterService struct {
|
type MatterService struct {
|
||||||
Bean
|
BaseBean
|
||||||
matterDao *MatterDao
|
matterDao *MatterDao
|
||||||
userDao *UserDao
|
userDao *UserDao
|
||||||
userService *UserService
|
userService *UserService
|
||||||
@ -28,7 +28,7 @@ type MatterService struct {
|
|||||||
|
|
||||||
//初始化方法
|
//初始化方法
|
||||||
func (this *MatterService) Init() {
|
func (this *MatterService) Init() {
|
||||||
this.Bean.Init()
|
this.BaseBean.Init()
|
||||||
|
|
||||||
//手动装填本实例的Bean. 这里必须要用中间变量方可。
|
//手动装填本实例的Bean. 这里必须要用中间变量方可。
|
||||||
b := core.CONTEXT.GetBean(this.matterDao)
|
b := core.CONTEXT.GetBean(this.matterDao)
|
||||||
|
@ -4,14 +4,14 @@ import "github.com/eyebluecn/tank/code/core"
|
|||||||
|
|
||||||
//@Service
|
//@Service
|
||||||
type PreferenceService struct {
|
type PreferenceService struct {
|
||||||
Bean
|
BaseBean
|
||||||
preferenceDao *PreferenceDao
|
preferenceDao *PreferenceDao
|
||||||
preference *Preference
|
preference *Preference
|
||||||
}
|
}
|
||||||
|
|
||||||
//初始化方法
|
//初始化方法
|
||||||
func (this *PreferenceService) Init() {
|
func (this *PreferenceService) Init() {
|
||||||
this.Bean.Init()
|
this.BaseBean.Init()
|
||||||
|
|
||||||
//手动装填本实例的Bean. 这里必须要用中间变量方可。
|
//手动装填本实例的Bean. 这里必须要用中间变量方可。
|
||||||
b := core.CONTEXT.GetBean(this.preferenceDao)
|
b := core.CONTEXT.GetBean(this.preferenceDao)
|
||||||
|
@ -4,14 +4,14 @@ import "github.com/eyebluecn/tank/code/core"
|
|||||||
|
|
||||||
//@Service
|
//@Service
|
||||||
type SessionService struct {
|
type SessionService struct {
|
||||||
Bean
|
BaseBean
|
||||||
userDao *UserDao
|
userDao *UserDao
|
||||||
sessionDao *SessionDao
|
sessionDao *SessionDao
|
||||||
}
|
}
|
||||||
|
|
||||||
//初始化方法
|
//初始化方法
|
||||||
func (this *SessionService) Init() {
|
func (this *SessionService) Init() {
|
||||||
this.Bean.Init()
|
this.BaseBean.Init()
|
||||||
|
|
||||||
//手动装填本实例的Bean. 这里必须要用中间变量方可。
|
//手动装填本实例的Bean. 这里必须要用中间变量方可。
|
||||||
b := core.CONTEXT.GetBean(this.userDao)
|
b := core.CONTEXT.GetBean(this.userDao)
|
||||||
|
@ -10,7 +10,7 @@ import (
|
|||||||
|
|
||||||
//@Service
|
//@Service
|
||||||
type UserService struct {
|
type UserService struct {
|
||||||
Bean
|
BaseBean
|
||||||
userDao *UserDao
|
userDao *UserDao
|
||||||
sessionDao *SessionDao
|
sessionDao *SessionDao
|
||||||
|
|
||||||
@ -20,7 +20,7 @@ type UserService struct {
|
|||||||
|
|
||||||
//初始化方法
|
//初始化方法
|
||||||
func (this *UserService) Init() {
|
func (this *UserService) Init() {
|
||||||
this.Bean.Init()
|
this.BaseBean.Init()
|
||||||
|
|
||||||
//手动装填本实例的Bean. 这里必须要用中间变量方可。
|
//手动装填本实例的Bean. 这里必须要用中间变量方可。
|
||||||
b := core.CONTEXT.GetBean(this.userDao)
|
b := core.CONTEXT.GetBean(this.userDao)
|
||||||
|
@ -18,7 +18,7 @@ type TankContext struct {
|
|||||||
//session缓存
|
//session缓存
|
||||||
SessionCache *cache.Table
|
SessionCache *cache.Table
|
||||||
//各类的Bean Map。这里面是包含ControllerMap中所有元素
|
//各类的Bean Map。这里面是包含ControllerMap中所有元素
|
||||||
BeanMap map[string]core.IBean
|
BeanMap map[string]core.Bean
|
||||||
//只包含了Controller的map
|
//只包含了Controller的map
|
||||||
ControllerMap map[string]core.IController
|
ControllerMap map[string]core.IController
|
||||||
//处理所有路由请求
|
//处理所有路由请求
|
||||||
@ -32,7 +32,7 @@ func (this *TankContext) Init() {
|
|||||||
this.SessionCache = cache.NewTable()
|
this.SessionCache = cache.NewTable()
|
||||||
|
|
||||||
//初始化Map
|
//初始化Map
|
||||||
this.BeanMap = make(map[string]core.IBean)
|
this.BeanMap = make(map[string]core.Bean)
|
||||||
this.ControllerMap = make(map[string]core.IController)
|
this.ControllerMap = make(map[string]core.IController)
|
||||||
|
|
||||||
//注册各类Beans.在这个方法里面顺便把Controller装入ControllerMap中去。
|
//注册各类Beans.在这个方法里面顺便把Controller装入ControllerMap中去。
|
||||||
@ -97,12 +97,12 @@ func (this *TankContext) CloseDb() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//注册一个Bean
|
//注册一个Bean
|
||||||
func (this *TankContext) registerBean(bean core.IBean) {
|
func (this *TankContext) registerBean(bean core.Bean) {
|
||||||
|
|
||||||
typeOf := reflect.TypeOf(bean)
|
typeOf := reflect.TypeOf(bean)
|
||||||
typeName := typeOf.String()
|
typeName := typeOf.String()
|
||||||
|
|
||||||
if element, ok := bean.(core.IBean); ok {
|
if element, ok := bean.(core.Bean); ok {
|
||||||
|
|
||||||
err := fmt.Sprintf("【%s】已经被注册了,跳过。", typeName)
|
err := fmt.Sprintf("【%s】已经被注册了,跳过。", typeName)
|
||||||
if _, ok := this.BeanMap[typeName]; ok {
|
if _, ok := this.BeanMap[typeName]; ok {
|
||||||
@ -180,7 +180,7 @@ func (this *TankContext) registerBeans() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//从Map中获取某个Bean.
|
//从Map中获取某个Bean.
|
||||||
func (this *TankContext) GetBean(bean core.IBean) core.IBean {
|
func (this *TankContext) GetBean(bean core.Bean) core.Bean {
|
||||||
|
|
||||||
typeOf := reflect.TypeOf(bean)
|
typeOf := reflect.TypeOf(bean)
|
||||||
typeName := typeOf.String()
|
typeName := typeOf.String()
|
||||||
|
Loading…
Reference in New Issue
Block a user