Finish all the i18n things of code.
This commit is contained in:
@ -1,8 +1,6 @@
|
||||
package support
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/eyebluecn/tank/code/core"
|
||||
"github.com/eyebluecn/tank/code/rest"
|
||||
"github.com/eyebluecn/tank/code/tool/cache"
|
||||
@ -11,45 +9,42 @@ import (
|
||||
"reflect"
|
||||
)
|
||||
|
||||
//上下文,管理数据库连接,管理所有路由请求,管理所有的单例component.
|
||||
type TankContext struct {
|
||||
//数据库连接
|
||||
//db connection
|
||||
db *gorm.DB
|
||||
//session缓存
|
||||
//session cache
|
||||
SessionCache *cache.Table
|
||||
//各类的Bean Map。这里面是包含ControllerMap中所有元素
|
||||
//bean map.
|
||||
BeanMap map[string]core.Bean
|
||||
//只包含了Controller的map
|
||||
//controller map
|
||||
ControllerMap map[string]core.Controller
|
||||
//处理所有路由请求
|
||||
//router
|
||||
Router *TankRouter
|
||||
}
|
||||
|
||||
//初始化上下文
|
||||
func (this *TankContext) Init() {
|
||||
|
||||
//创建一个用于存储session的缓存。
|
||||
//create session cache
|
||||
this.SessionCache = cache.NewTable()
|
||||
|
||||
//初始化Map
|
||||
//init map
|
||||
this.BeanMap = make(map[string]core.Bean)
|
||||
this.ControllerMap = make(map[string]core.Controller)
|
||||
|
||||
//注册各类Beans.在这个方法里面顺便把Controller装入ControllerMap中去。
|
||||
//register beans. This method will put Controllers to ControllerMap.
|
||||
this.registerBeans()
|
||||
|
||||
//初始化每个bean.
|
||||
//init every bean.
|
||||
this.initBeans()
|
||||
|
||||
//初始化Router. 这个方法要在Bean注册好了之后才能。
|
||||
//create and init router.
|
||||
this.Router = NewRouter()
|
||||
|
||||
//如果数据库信息配置好了,就直接打开数据库连接 同时执行Bean的ConfigPost方法
|
||||
//if the application is installed. Bean's Bootstrap method will be invoked.
|
||||
this.InstallOk()
|
||||
|
||||
}
|
||||
|
||||
//获取数据库对象
|
||||
func (this *TankContext) GetDB() *gorm.DB {
|
||||
return this.db
|
||||
}
|
||||
@ -68,7 +63,7 @@ func (this *TankContext) Cleanup() {
|
||||
}
|
||||
}
|
||||
|
||||
//响应http的能力
|
||||
//can serve as http server.
|
||||
func (this *TankContext) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
||||
this.Router.ServeHTTP(writer, request)
|
||||
}
|
||||
@ -82,7 +77,7 @@ func (this *TankContext) OpenDb() {
|
||||
core.LOGGER.Panic("failed to connect mysql database")
|
||||
}
|
||||
|
||||
//是否打开sql日志(在调试阶段可以打开,以方便查看执行的SQL)
|
||||
//whether open the db log. (only true when debug)
|
||||
this.db.LogMode(false)
|
||||
}
|
||||
|
||||
@ -91,12 +86,11 @@ func (this *TankContext) CloseDb() {
|
||||
if this.db != nil {
|
||||
err := this.db.Close()
|
||||
if err != nil {
|
||||
core.LOGGER.Error("关闭数据库连接出错 %s", err.Error())
|
||||
core.LOGGER.Error("occur error when closing db %s", err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//注册一个Bean
|
||||
func (this *TankContext) registerBean(bean core.Bean) {
|
||||
|
||||
typeOf := reflect.TypeOf(bean)
|
||||
@ -104,13 +98,12 @@ func (this *TankContext) registerBean(bean core.Bean) {
|
||||
|
||||
if element, ok := bean.(core.Bean); ok {
|
||||
|
||||
err := fmt.Sprintf("【%s】已经被注册了,跳过。", typeName)
|
||||
if _, ok := this.BeanMap[typeName]; ok {
|
||||
core.LOGGER.Error(fmt.Sprintf(err))
|
||||
core.LOGGER.Error("%s has been registerd, skip", typeName)
|
||||
} else {
|
||||
this.BeanMap[typeName] = element
|
||||
|
||||
//看看是不是controller类型,如果是,那么单独放在ControllerMap中。
|
||||
//if is controller type, put into ControllerMap
|
||||
if controller, ok1 := bean.(core.Controller); ok1 {
|
||||
this.ControllerMap[typeName] = controller
|
||||
}
|
||||
@ -118,12 +111,11 @@ func (this *TankContext) registerBean(bean core.Bean) {
|
||||
}
|
||||
|
||||
} else {
|
||||
core.LOGGER.Panic("注册的【%s】不是Bean类型。", typeName)
|
||||
core.LOGGER.Panic("%s is not the Bean type", typeName)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//注册各个Beans
|
||||
func (this *TankContext) registerBeans() {
|
||||
|
||||
//alien
|
||||
@ -188,7 +180,6 @@ func (this *TankContext) registerBeans() {
|
||||
|
||||
}
|
||||
|
||||
//从Map中获取某个Bean.
|
||||
func (this *TankContext) GetBean(bean core.Bean) core.Bean {
|
||||
|
||||
typeOf := reflect.TypeOf(bean)
|
||||
@ -197,12 +188,11 @@ func (this *TankContext) GetBean(bean core.Bean) core.Bean {
|
||||
if val, ok := this.BeanMap[typeName]; ok {
|
||||
return val
|
||||
} else {
|
||||
core.LOGGER.Panic("【%s】没有注册。", typeName)
|
||||
core.LOGGER.Panic("%s not registered", typeName)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
//初始化每个Bean
|
||||
func (this *TankContext) initBeans() {
|
||||
|
||||
for _, bean := range this.BeanMap {
|
||||
@ -210,7 +200,7 @@ func (this *TankContext) initBeans() {
|
||||
}
|
||||
}
|
||||
|
||||
//系统如果安装好了就调用这个方法。
|
||||
//if application installed. invoke this method.
|
||||
func (this *TankContext) InstallOk() {
|
||||
|
||||
if core.CONFIG.Installed() {
|
||||
@ -223,7 +213,6 @@ func (this *TankContext) InstallOk() {
|
||||
|
||||
}
|
||||
|
||||
//销毁的方法
|
||||
func (this *TankContext) Destroy() {
|
||||
this.CloseDb()
|
||||
}
|
||||
|
Reference in New Issue
Block a user