diff --git a/code/core/context.go b/code/core/context.go index 9a8bc95..53745b6 100644 --- a/code/core/context.go +++ b/code/core/context.go @@ -1 +1,30 @@ package core + +import ( + "github.com/eyebluecn/tank/code/tool/cache" + "github.com/jinzhu/gorm" + "net/http" +) + +type Context interface { + //获取数据库链接 + GetDB() *gorm.DB + + //获取一个Bean + GetBean(bean IBean) IBean + + //获取全局的Session缓存 + GetSessionCache() *cache.Table + + //获取全局的ControllerMap + GetControllerMap() map[string]IController + + //响应http的能力 + ServeHTTP(writer http.ResponseWriter, request *http.Request) + + //系统安装成功 + InstallOk() + + //清空系统 + Cleanup() +} diff --git a/code/core/global.go b/code/core/global.go index f6d266b..1a1854d 100644 --- a/code/core/global.go +++ b/code/core/global.go @@ -5,3 +5,6 @@ package core //日志系统必须高保 //全局唯一的日志对象(在main函数中初始化) var LOGGER Logger + +//全局唯一的上下文(在main函数中初始化) +var CONTEXT Context diff --git a/code/rest/alien_controller.go b/code/rest/alien_controller.go index 04e439a..b499c40 100644 --- a/code/rest/alien_controller.go +++ b/code/rest/alien_controller.go @@ -2,12 +2,12 @@ package rest import ( "fmt" + "github.com/eyebluecn/tank/code/core" "github.com/eyebluecn/tank/code/tool/result" "github.com/eyebluecn/tank/code/tool/util" "net/http" "regexp" "strconv" - "time" ) @@ -27,37 +27,37 @@ func (this *AlienController) Init() { this.BaseController.Init() //手动装填本实例的Bean. - b := CONTEXT.GetBean(this.uploadTokenDao) + b := core.CONTEXT.GetBean(this.uploadTokenDao) if c, ok := b.(*UploadTokenDao); ok { this.uploadTokenDao = c } - b = CONTEXT.GetBean(this.downloadTokenDao) + b = core.CONTEXT.GetBean(this.downloadTokenDao) if c, ok := b.(*DownloadTokenDao); ok { this.downloadTokenDao = c } - b = CONTEXT.GetBean(this.matterDao) + b = core.CONTEXT.GetBean(this.matterDao) if c, ok := b.(*MatterDao); ok { this.matterDao = c } - b = CONTEXT.GetBean(this.matterService) + b = core.CONTEXT.GetBean(this.matterService) if c, ok := b.(*MatterService); ok { this.matterService = c } - b = CONTEXT.GetBean(this.imageCacheDao) + b = core.CONTEXT.GetBean(this.imageCacheDao) if c, ok := b.(*ImageCacheDao); ok { this.imageCacheDao = c } - b = CONTEXT.GetBean(this.imageCacheService) + b = core.CONTEXT.GetBean(this.imageCacheService) if c, ok := b.(*ImageCacheService); ok { this.imageCacheService = c } - b = CONTEXT.GetBean(this.alienService) + b = core.CONTEXT.GetBean(this.alienService) if c, ok := b.(*AlienService); ok { this.alienService = c } @@ -113,7 +113,7 @@ func (this *AlienController) CheckRequestUser(writer http.ResponseWriter, reques //根据用户登录信息取 user := this.findUser(writer, request) if user != nil { - return user; + return user } email := request.FormValue("email") diff --git a/code/rest/alien_service.go b/code/rest/alien_service.go index 804776f..673fdab 100644 --- a/code/rest/alien_service.go +++ b/code/rest/alien_service.go @@ -2,6 +2,7 @@ package rest import ( "fmt" + "github.com/eyebluecn/tank/code/core" "github.com/eyebluecn/tank/code/tool/result" "github.com/eyebluecn/tank/code/tool/util" "net/http" @@ -25,37 +26,37 @@ func (this *AlienService) Init() { this.Bean.Init() //手动装填本实例的Bean. 这里必须要用中间变量方可。 - b := CONTEXT.GetBean(this.matterDao) + b := core.CONTEXT.GetBean(this.matterDao) if b, ok := b.(*MatterDao); ok { this.matterDao = b } - b = CONTEXT.GetBean(this.matterService) + b = core.CONTEXT.GetBean(this.matterService) if b, ok := b.(*MatterService); ok { this.matterService = b } - b = CONTEXT.GetBean(this.userDao) + b = core.CONTEXT.GetBean(this.userDao) if b, ok := b.(*UserDao); ok { this.userDao = b } - b = CONTEXT.GetBean(this.uploadTokenDao) + b = core.CONTEXT.GetBean(this.uploadTokenDao) if c, ok := b.(*UploadTokenDao); ok { this.uploadTokenDao = c } - b = CONTEXT.GetBean(this.downloadTokenDao) + b = core.CONTEXT.GetBean(this.downloadTokenDao) if c, ok := b.(*DownloadTokenDao); ok { this.downloadTokenDao = c } - b = CONTEXT.GetBean(this.imageCacheDao) + b = core.CONTEXT.GetBean(this.imageCacheDao) if c, ok := b.(*ImageCacheDao); ok { this.imageCacheDao = c } - b = CONTEXT.GetBean(this.imageCacheService) + b = core.CONTEXT.GetBean(this.imageCacheService) if c, ok := b.(*ImageCacheService); ok { this.imageCacheService = c } diff --git a/code/rest/base_controller.go b/code/rest/base_controller.go index f6ffb2c..08bc979 100644 --- a/code/rest/base_controller.go +++ b/code/rest/base_controller.go @@ -2,6 +2,7 @@ package rest import ( "fmt" + "github.com/eyebluecn/tank/code/core" "github.com/eyebluecn/tank/code/tool/result" "github.com/json-iterator/go" "go/types" @@ -19,12 +20,12 @@ func (this *BaseController) Init() { this.Bean.Init() //手动装填本实例的Bean. - b := CONTEXT.GetBean(this.userDao) + b := core.CONTEXT.GetBean(this.userDao) if b, ok := b.(*UserDao); ok { this.userDao = b } - b = CONTEXT.GetBean(this.sessionDao) + b = core.CONTEXT.GetBean(this.sessionDao) if b, ok := b.(*SessionDao); ok { this.sessionDao = b } diff --git a/code/rest/bean.go b/code/rest/bean.go index 2b7b659..1a197f4 100644 --- a/code/rest/bean.go +++ b/code/rest/bean.go @@ -41,7 +41,7 @@ func (this *Bean) findUser(writer http.ResponseWriter, request *http.Request) *U } //去缓存中捞取看看 - cacheItem, err := CONTEXT.SessionCache.Value(sessionId) + cacheItem, err := core.CONTEXT.GetSessionCache().Value(sessionId) if err != nil { this.logger.Warn("获取缓存时出错了" + err.Error()) return nil diff --git a/code/rest/context.go b/code/rest/context.go index a1294e1..93c04f6 100644 --- a/code/rest/context.go +++ b/code/rest/context.go @@ -6,12 +6,10 @@ import ( "github.com/eyebluecn/tank/code/core" "github.com/eyebluecn/tank/code/tool/cache" "github.com/jinzhu/gorm" + "net/http" "reflect" ) -//全局唯一的上下文(在main函数中初始化) -var CONTEXT = &Context{} - //上下文,管理数据库连接,管理所有路由请求,管理所有的单例component. type Context struct { //数据库连接 @@ -55,6 +53,25 @@ func (this *Context) GetDB() *gorm.DB { return this.db } +func (this *Context) GetSessionCache() *cache.Table { + return this.SessionCache +} + +func (this *Context) GetControllerMap() map[string]core.IController { + return this.ControllerMap +} + +func (this *Context) Cleanup() { + for _, bean := range this.BeanMap { + bean.Cleanup() + } +} + +//响应http的能力 +func (this *Context) ServeHTTP(writer http.ResponseWriter, request *http.Request) { + this.Router.ServeHTTP(writer, request) +} + func (this *Context) OpenDb() { var err error = nil diff --git a/code/rest/dashboard_controller.go b/code/rest/dashboard_controller.go index 2bbf320..51bac3c 100644 --- a/code/rest/dashboard_controller.go +++ b/code/rest/dashboard_controller.go @@ -1,6 +1,7 @@ package rest import ( + "github.com/eyebluecn/tank/code/core" "github.com/eyebluecn/tank/code/tool/builder" "github.com/eyebluecn/tank/code/tool/result" "net/http" @@ -18,12 +19,12 @@ func (this *DashboardController) Init() { this.BaseController.Init() //手动装填本实例的Bean. 这里必须要用中间变量方可。 - b := CONTEXT.GetBean(this.dashboardDao) + b := core.CONTEXT.GetBean(this.dashboardDao) if b, ok := b.(*DashboardDao); ok { this.dashboardDao = b } - b = CONTEXT.GetBean(this.dashboardService) + b = core.CONTEXT.GetBean(this.dashboardService) if b, ok := b.(*DashboardService); ok { this.dashboardService = b } diff --git a/code/rest/dashboard_dao.go b/code/rest/dashboard_dao.go index 47a8e89..521ccd9 100644 --- a/code/rest/dashboard_dao.go +++ b/code/rest/dashboard_dao.go @@ -1,6 +1,7 @@ package rest import ( + "github.com/eyebluecn/tank/code/core" "github.com/eyebluecn/tank/code/tool/builder" "github.com/jinzhu/gorm" "github.com/nu7hatch/gouuid" @@ -19,7 +20,7 @@ func (this *DashboardDao) Create(dashboard *Dashboard) *Dashboard { dashboard.CreateTime = time.Now() dashboard.UpdateTime = time.Now() dashboard.Sort = time.Now().UnixNano() / 1e6 - db := CONTEXT.GetDB().Create(dashboard) + db := core.CONTEXT.GetDB().Create(dashboard) this.PanicError(db.Error) return dashboard @@ -29,7 +30,7 @@ func (this *DashboardDao) Create(dashboard *Dashboard) *Dashboard { func (this *DashboardDao) Save(dashboard *Dashboard) *Dashboard { dashboard.UpdateTime = time.Now() - db := CONTEXT.GetDB().Save(dashboard) + db := core.CONTEXT.GetDB().Save(dashboard) this.PanicError(db.Error) return dashboard @@ -38,7 +39,7 @@ func (this *DashboardDao) Save(dashboard *Dashboard) *Dashboard { //删除一条记录 func (this *DashboardDao) Delete(dashboard *Dashboard) { - db := CONTEXT.GetDB().Delete(&dashboard) + db := core.CONTEXT.GetDB().Delete(&dashboard) this.PanicError(db.Error) } @@ -47,7 +48,7 @@ func (this *DashboardDao) FindByDt(dt string) *Dashboard { // Read var dashboard Dashboard - db := CONTEXT.GetDB().Where(&Dashboard{Dt: dt}).First(&dashboard) + db := core.CONTEXT.GetDB().Where(&Dashboard{Dt: dt}).First(&dashboard) if db.Error != nil { return nil } @@ -64,7 +65,7 @@ func (this *DashboardDao) Page(page int, pageSize int, dt string, sortArray []bu } var conditionDB *gorm.DB - conditionDB = CONTEXT.GetDB().Model(&Dashboard{}).Where(wp.Query, wp.Args...) + conditionDB = core.CONTEXT.GetDB().Model(&Dashboard{}).Where(wp.Query, wp.Args...) count := 0 db := conditionDB.Count(&count) @@ -89,7 +90,7 @@ func (this *DashboardDao) ActiveIpTop10() []*DashboardIpTimes { Value: "DESC", }, } - rows, err := CONTEXT.GetDB().Model(&Footprint{}). + rows, err := core.CONTEXT.GetDB().Model(&Footprint{}). Select("ip,COUNT(uuid) as times"). Group("ip"). Order(this.GetSortString(sortArray)). @@ -115,6 +116,6 @@ func (this *DashboardDao) ActiveIpTop10() []*DashboardIpTimes { //执行清理操作 func (this *DashboardDao) Cleanup() { this.logger.Info("[DashboardDao]执行清理:清除数据库中所有Dashboard记录。") - db := CONTEXT.GetDB().Where("uuid is not null").Delete(Dashboard{}) + db := core.CONTEXT.GetDB().Where("uuid is not null").Delete(Dashboard{}) this.PanicError(db.Error) } diff --git a/code/rest/dashboard_service.go b/code/rest/dashboard_service.go index 321e87c..e5ddb44 100644 --- a/code/rest/dashboard_service.go +++ b/code/rest/dashboard_service.go @@ -1,6 +1,7 @@ package rest import ( + "github.com/eyebluecn/tank/code/core" "github.com/eyebluecn/tank/code/tool/util" "github.com/robfig/cron" "time" @@ -21,27 +22,27 @@ func (this *DashboardService) Init() { this.Bean.Init() //手动装填本实例的Bean. 这里必须要用中间变量方可。 - b := CONTEXT.GetBean(this.dashboardDao) + b := core.CONTEXT.GetBean(this.dashboardDao) if b, ok := b.(*DashboardDao); ok { this.dashboardDao = b } - b = CONTEXT.GetBean(this.footprintDao) + b = core.CONTEXT.GetBean(this.footprintDao) if b, ok := b.(*FootprintDao); ok { this.footprintDao = b } - b = CONTEXT.GetBean(this.matterDao) + b = core.CONTEXT.GetBean(this.matterDao) if b, ok := b.(*MatterDao); ok { this.matterDao = b } - b = CONTEXT.GetBean(this.imageCacheDao) + b = core.CONTEXT.GetBean(this.imageCacheDao) if b, ok := b.(*ImageCacheDao); ok { this.imageCacheDao = b } - b = CONTEXT.GetBean(this.userDao) + b = core.CONTEXT.GetBean(this.userDao) if b, ok := b.(*UserDao); ok { this.userDao = b } diff --git a/code/rest/dav_controller.go b/code/rest/dav_controller.go index 7ad279e..1d52401 100644 --- a/code/rest/dav_controller.go +++ b/code/rest/dav_controller.go @@ -3,6 +3,7 @@ package rest import ( "bytes" "fmt" + "github.com/eyebluecn/tank/code/core" "github.com/eyebluecn/tank/code/tool/result" "github.com/eyebluecn/tank/code/tool/util" "io/ioutil" @@ -35,37 +36,37 @@ func (this *DavController) Init() { this.BaseController.Init() //手动装填本实例的Bean. - b := CONTEXT.GetBean(this.uploadTokenDao) + b := core.CONTEXT.GetBean(this.uploadTokenDao) if c, ok := b.(*UploadTokenDao); ok { this.uploadTokenDao = c } - b = CONTEXT.GetBean(this.downloadTokenDao) + b = core.CONTEXT.GetBean(this.downloadTokenDao) if c, ok := b.(*DownloadTokenDao); ok { this.downloadTokenDao = c } - b = CONTEXT.GetBean(this.matterDao) + b = core.CONTEXT.GetBean(this.matterDao) if c, ok := b.(*MatterDao); ok { this.matterDao = c } - b = CONTEXT.GetBean(this.matterService) + b = core.CONTEXT.GetBean(this.matterService) if c, ok := b.(*MatterService); ok { this.matterService = c } - b = CONTEXT.GetBean(this.imageCacheDao) + b = core.CONTEXT.GetBean(this.imageCacheDao) if c, ok := b.(*ImageCacheDao); ok { this.imageCacheDao = c } - b = CONTEXT.GetBean(this.imageCacheService) + b = core.CONTEXT.GetBean(this.imageCacheService) if c, ok := b.(*ImageCacheService); ok { this.imageCacheService = c } - b = CONTEXT.GetBean(this.davService) + b = core.CONTEXT.GetBean(this.davService) if c, ok := b.(*DavService); ok { this.davService = c } diff --git a/code/rest/dav_service.go b/code/rest/dav_service.go index 5aaa484..003f331 100644 --- a/code/rest/dav_service.go +++ b/code/rest/dav_service.go @@ -2,6 +2,7 @@ package rest import ( "fmt" + "github.com/eyebluecn/tank/code/core" "github.com/eyebluecn/tank/code/tool/dav" "github.com/eyebluecn/tank/code/tool/dav/xml" "github.com/eyebluecn/tank/code/tool/result" @@ -32,12 +33,12 @@ func (this *DavService) Init() { this.Bean.Init() //手动装填本实例的Bean. 这里必须要用中间变量方可。 - b := CONTEXT.GetBean(this.matterDao) + b := core.CONTEXT.GetBean(this.matterDao) if b, ok := b.(*MatterDao); ok { this.matterDao = b } - b = CONTEXT.GetBean(this.matterService) + b = core.CONTEXT.GetBean(this.matterService) if b, ok := b.(*MatterService); ok { this.matterService = b } diff --git a/code/rest/download_token_dao.go b/code/rest/download_token_dao.go index 42ded18..4b76c24 100644 --- a/code/rest/download_token_dao.go +++ b/code/rest/download_token_dao.go @@ -1,6 +1,7 @@ package rest import ( + "github.com/eyebluecn/tank/code/core" "github.com/nu7hatch/gouuid" "time" ) @@ -14,7 +15,7 @@ func (this *DownloadTokenDao) FindByUuid(uuid string) *DownloadToken { // Read var downloadToken = &DownloadToken{} - db := CONTEXT.GetDB().Where(&DownloadToken{Base: Base{Uuid: uuid}}).First(downloadToken) + db := core.CONTEXT.GetDB().Where(&DownloadToken{Base: Base{Uuid: uuid}}).First(downloadToken) if db.Error != nil { return nil } @@ -27,7 +28,7 @@ func (this *DownloadTokenDao) CheckByUuid(uuid string) *DownloadToken { // Read var downloadToken = &DownloadToken{} - db := CONTEXT.GetDB().Where(&DownloadToken{Base: Base{Uuid: uuid}}).First(downloadToken) + db := core.CONTEXT.GetDB().Where(&DownloadToken{Base: Base{Uuid: uuid}}).First(downloadToken) this.PanicError(db.Error) return downloadToken @@ -42,7 +43,7 @@ func (this *DownloadTokenDao) Create(downloadToken *DownloadToken) *DownloadToke downloadToken.CreateTime = time.Now() downloadToken.UpdateTime = time.Now() downloadToken.Sort = time.Now().UnixNano() / 1e6 - db := CONTEXT.GetDB().Create(downloadToken) + db := core.CONTEXT.GetDB().Create(downloadToken) this.PanicError(db.Error) return downloadToken @@ -52,7 +53,7 @@ func (this *DownloadTokenDao) Create(downloadToken *DownloadToken) *DownloadToke func (this *DownloadTokenDao) Save(downloadToken *DownloadToken) *DownloadToken { downloadToken.UpdateTime = time.Now() - db := CONTEXT.GetDB().Save(downloadToken) + db := core.CONTEXT.GetDB().Save(downloadToken) this.PanicError(db.Error) return downloadToken @@ -61,6 +62,6 @@ func (this *DownloadTokenDao) Save(downloadToken *DownloadToken) *DownloadToken //执行清理操作 func (this *DownloadTokenDao) Cleanup() { this.logger.Info("[DownloadTokenDao]执行清理:清除数据库中所有DownloadToken记录。") - db := CONTEXT.GetDB().Where("uuid is not null").Delete(DownloadToken{}) + db := core.CONTEXT.GetDB().Where("uuid is not null").Delete(DownloadToken{}) this.PanicError(db.Error) } diff --git a/code/rest/footprint_controller.go b/code/rest/footprint_controller.go index a478e0e..9e7e0d5 100644 --- a/code/rest/footprint_controller.go +++ b/code/rest/footprint_controller.go @@ -1,6 +1,7 @@ package rest import ( + "github.com/eyebluecn/tank/code/core" "github.com/eyebluecn/tank/code/tool/builder" "github.com/eyebluecn/tank/code/tool/result" "net/http" @@ -18,12 +19,12 @@ func (this *FootprintController) Init() { this.BaseController.Init() //手动装填本实例的Bean. 这里必须要用中间变量方可。 - b := CONTEXT.GetBean(this.footprintDao) + b := core.CONTEXT.GetBean(this.footprintDao) if b, ok := b.(*FootprintDao); ok { this.footprintDao = b } - b = CONTEXT.GetBean(this.footprintService) + b = core.CONTEXT.GetBean(this.footprintService) if b, ok := b.(*FootprintService); ok { this.footprintService = b } diff --git a/code/rest/footprint_dao.go b/code/rest/footprint_dao.go index a61931a..5bbfe86 100644 --- a/code/rest/footprint_dao.go +++ b/code/rest/footprint_dao.go @@ -1,6 +1,7 @@ package rest import ( + "github.com/eyebluecn/tank/code/core" "github.com/eyebluecn/tank/code/tool/builder" "github.com/jinzhu/gorm" @@ -17,7 +18,7 @@ func (this *FootprintDao) FindByUuid(uuid string) *Footprint { // Read var footprint Footprint - db := CONTEXT.GetDB().Where(&Footprint{Base: Base{Uuid: uuid}}).First(&footprint) + db := core.CONTEXT.GetDB().Where(&Footprint{Base: Base{Uuid: uuid}}).First(&footprint) if db.Error != nil { return nil } @@ -29,7 +30,7 @@ func (this *FootprintDao) CheckByUuid(uuid string) *Footprint { // Read var footprint Footprint - db := CONTEXT.GetDB().Where(&Footprint{Base: Base{Uuid: uuid}}).First(&footprint) + db := core.CONTEXT.GetDB().Where(&Footprint{Base: Base{Uuid: uuid}}).First(&footprint) this.PanicError(db.Error) return &footprint @@ -46,7 +47,7 @@ func (this *FootprintDao) Page(page int, pageSize int, userUuid string, sortArra } var conditionDB *gorm.DB - conditionDB = CONTEXT.GetDB().Model(&Footprint{}).Where(wp.Query, wp.Args...) + conditionDB = core.CONTEXT.GetDB().Model(&Footprint{}).Where(wp.Query, wp.Args...) count := 0 db := conditionDB.Count(&count) @@ -68,7 +69,7 @@ func (this *FootprintDao) Create(footprint *Footprint) *Footprint { footprint.CreateTime = time.Now() footprint.UpdateTime = time.Now() footprint.Sort = time.Now().UnixNano() / 1e6 - db := CONTEXT.GetDB().Create(footprint) + db := core.CONTEXT.GetDB().Create(footprint) this.PanicError(db.Error) return footprint @@ -78,7 +79,7 @@ func (this *FootprintDao) Create(footprint *Footprint) *Footprint { func (this *FootprintDao) Save(footprint *Footprint) *Footprint { footprint.UpdateTime = time.Now() - db := CONTEXT.GetDB().Save(footprint) + db := core.CONTEXT.GetDB().Save(footprint) this.PanicError(db.Error) return footprint @@ -87,14 +88,14 @@ func (this *FootprintDao) Save(footprint *Footprint) *Footprint { //删除一条记录 func (this *FootprintDao) Delete(footprint *Footprint) { - db := CONTEXT.GetDB().Delete(&footprint) + db := core.CONTEXT.GetDB().Delete(&footprint) this.PanicError(db.Error) } //获取一段时间中,总的数量 func (this *FootprintDao) CountBetweenTime(startTime time.Time, endTime time.Time) int64 { var count int64 - db := CONTEXT.GetDB().Model(&Footprint{}).Where("create_time >= ? AND create_time <= ?", startTime, endTime).Count(&count) + db := core.CONTEXT.GetDB().Model(&Footprint{}).Where("create_time >= ? AND create_time <= ?", startTime, endTime).Count(&count) this.PanicError(db.Error) return count } @@ -102,7 +103,7 @@ func (this *FootprintDao) CountBetweenTime(startTime time.Time, endTime time.Tim //获取一段时间中UV的数量 func (this *FootprintDao) UvBetweenTime(startTime time.Time, endTime time.Time) int64 { var count int64 - db := CONTEXT.GetDB().Model(&Footprint{}).Where("create_time >= ? AND create_time <= ?", startTime, endTime).Select("COUNT(DISTINCT(ip))") + db := core.CONTEXT.GetDB().Model(&Footprint{}).Where("create_time >= ? AND create_time <= ?", startTime, endTime).Select("COUNT(DISTINCT(ip))") this.PanicError(db.Error) row := db.Row() row.Scan(&count) @@ -112,7 +113,7 @@ func (this *FootprintDao) UvBetweenTime(startTime time.Time, endTime time.Time) //获取一段时间中平均耗时 func (this *FootprintDao) AvgCostBetweenTime(startTime time.Time, endTime time.Time) int64 { var cost float64 - db := CONTEXT.GetDB().Model(&Footprint{}).Where("create_time >= ? AND create_time <= ?", startTime, endTime).Select("AVG(cost)") + db := core.CONTEXT.GetDB().Model(&Footprint{}).Where("create_time >= ? AND create_time <= ?", startTime, endTime).Select("AVG(cost)") this.PanicError(db.Error) row := db.Row() row.Scan(&cost) @@ -121,13 +122,13 @@ func (this *FootprintDao) AvgCostBetweenTime(startTime time.Time, endTime time.T //删除某个时刻之前的记录 func (this *FootprintDao) DeleteByCreateTimeBefore(createTime time.Time) { - db := CONTEXT.GetDB().Where("create_time < ?", createTime).Delete(Footprint{}) + db := core.CONTEXT.GetDB().Where("create_time < ?", createTime).Delete(Footprint{}) this.PanicError(db.Error) } //执行清理操作 func (this *FootprintDao) Cleanup() { this.logger.Info("[FootprintDao]执行清理:清除数据库中所有Footprint记录。") - db := CONTEXT.GetDB().Where("uuid is not null").Delete(Footprint{}) + db := core.CONTEXT.GetDB().Where("uuid is not null").Delete(Footprint{}) this.PanicError(db.Error) } diff --git a/code/rest/footprint_service.go b/code/rest/footprint_service.go index 271490f..a758d5f 100644 --- a/code/rest/footprint_service.go +++ b/code/rest/footprint_service.go @@ -3,6 +3,7 @@ package rest import ( "encoding/json" "github.com/eyebluecn/tank/code/config" + "github.com/eyebluecn/tank/code/core" "github.com/eyebluecn/tank/code/tool/util" "github.com/robfig/cron" "net/http" @@ -21,12 +22,12 @@ func (this *FootprintService) Init() { this.Bean.Init() //手动装填本实例的Bean. 这里必须要用中间变量方可。 - b := CONTEXT.GetBean(this.footprintDao) + b := core.CONTEXT.GetBean(this.footprintDao) if b, ok := b.(*FootprintDao); ok { this.footprintDao = b } - b = CONTEXT.GetBean(this.userDao) + b = core.CONTEXT.GetBean(this.userDao) if b, ok := b.(*UserDao); ok { this.userDao = b } diff --git a/code/rest/image_cache_controller.go b/code/rest/image_cache_controller.go index b08b719..f650525 100644 --- a/code/rest/image_cache_controller.go +++ b/code/rest/image_cache_controller.go @@ -1,6 +1,7 @@ package rest import ( + "github.com/eyebluecn/tank/code/core" "github.com/eyebluecn/tank/code/tool/builder" "github.com/eyebluecn/tank/code/tool/result" "net/http" @@ -19,12 +20,12 @@ func (this *ImageCacheController) Init() { this.BaseController.Init() //手动装填本实例的Bean. 这里必须要用中间变量方可。 - b := CONTEXT.GetBean(this.imageCacheDao) + b := core.CONTEXT.GetBean(this.imageCacheDao) if b, ok := b.(*ImageCacheDao); ok { this.imageCacheDao = b } - b = CONTEXT.GetBean(this.imageCacheService) + b = core.CONTEXT.GetBean(this.imageCacheService) if b, ok := b.(*ImageCacheService); ok { this.imageCacheService = b } diff --git a/code/rest/image_cache_dao.go b/code/rest/image_cache_dao.go index d553449..1886be9 100644 --- a/code/rest/image_cache_dao.go +++ b/code/rest/image_cache_dao.go @@ -2,6 +2,7 @@ package rest import ( "fmt" + "github.com/eyebluecn/tank/code/core" "github.com/eyebluecn/tank/code/tool/builder" "github.com/eyebluecn/tank/code/tool/util" "github.com/jinzhu/gorm" @@ -20,7 +21,7 @@ func (this *ImageCacheDao) FindByUuid(uuid string) *ImageCache { // Read var imageCache ImageCache - db := CONTEXT.GetDB().Where(&ImageCache{Base: Base{Uuid: uuid}}).First(&imageCache) + db := core.CONTEXT.GetDB().Where(&ImageCache{Base: Base{Uuid: uuid}}).First(&imageCache) if db.Error != nil { return nil } @@ -32,7 +33,7 @@ func (this *ImageCacheDao) CheckByUuid(uuid string) *ImageCache { // Read var imageCache ImageCache - db := CONTEXT.GetDB().Where(&ImageCache{Base: Base{Uuid: uuid}}).First(&imageCache) + db := core.CONTEXT.GetDB().Where(&ImageCache{Base: Base{Uuid: uuid}}).First(&imageCache) this.PanicError(db.Error) return &imageCache @@ -53,7 +54,7 @@ func (this *ImageCacheDao) FindByMatterUuidAndMode(matterUuid string, mode strin } var imageCache = &ImageCache{} - db := CONTEXT.GetDB().Model(&ImageCache{}).Where(wp.Query, wp.Args...).First(imageCache) + db := core.CONTEXT.GetDB().Model(&ImageCache{}).Where(wp.Query, wp.Args...).First(imageCache) if db.Error != nil { return nil @@ -67,7 +68,7 @@ func (this *ImageCacheDao) CheckByUuidAndUserUuid(uuid string, userUuid string) // Read var imageCache = &ImageCache{} - db := CONTEXT.GetDB().Where(&ImageCache{Base: Base{Uuid: uuid}, UserUuid: userUuid}).First(imageCache) + db := core.CONTEXT.GetDB().Where(&ImageCache{Base: Base{Uuid: uuid}, UserUuid: userUuid}).First(imageCache) this.PanicError(db.Error) return imageCache @@ -79,7 +80,7 @@ func (this *ImageCacheDao) ListByUserUuidAndPuuidAndDirAndName(userUuid string) var imageCaches []*ImageCache - db := CONTEXT.GetDB(). + db := core.CONTEXT.GetDB(). Where(ImageCache{UserUuid: userUuid}). Find(&imageCaches) this.PanicError(db.Error) @@ -101,7 +102,7 @@ func (this *ImageCacheDao) Page(page int, pageSize int, userUuid string, matterU } var conditionDB *gorm.DB - conditionDB = CONTEXT.GetDB().Model(&ImageCache{}).Where(wp.Query, wp.Args...) + conditionDB = core.CONTEXT.GetDB().Model(&ImageCache{}).Where(wp.Query, wp.Args...) count := 0 db := conditionDB.Count(&count) @@ -123,7 +124,7 @@ func (this *ImageCacheDao) Create(imageCache *ImageCache) *ImageCache { imageCache.CreateTime = time.Now() imageCache.UpdateTime = time.Now() imageCache.Sort = time.Now().UnixNano() / 1e6 - db := CONTEXT.GetDB().Create(imageCache) + db := core.CONTEXT.GetDB().Create(imageCache) this.PanicError(db.Error) return imageCache @@ -133,7 +134,7 @@ func (this *ImageCacheDao) Create(imageCache *ImageCache) *ImageCache { func (this *ImageCacheDao) Save(imageCache *ImageCache) *ImageCache { imageCache.UpdateTime = time.Now() - db := CONTEXT.GetDB().Save(imageCache) + db := core.CONTEXT.GetDB().Save(imageCache) this.PanicError(db.Error) return imageCache @@ -160,7 +161,7 @@ func (this *ImageCacheDao) deleteFileAndDir(imageCache *ImageCache) { //删除一个文件,数据库中删除,物理磁盘上删除。 func (this *ImageCacheDao) Delete(imageCache *ImageCache) { - db := CONTEXT.GetDB().Delete(&imageCache) + db := core.CONTEXT.GetDB().Delete(&imageCache) this.PanicError(db.Error) this.deleteFileAndDir(imageCache) @@ -176,11 +177,11 @@ func (this *ImageCacheDao) DeleteByMatterUuid(matterUuid string) { //查询出即将删除的图片缓存 var imageCaches []*ImageCache - db := CONTEXT.GetDB().Where(wp.Query, wp.Args).Find(&imageCaches) + db := core.CONTEXT.GetDB().Where(wp.Query, wp.Args).Find(&imageCaches) this.PanicError(db.Error) //删除文件记录 - db = CONTEXT.GetDB().Where(wp.Query, wp.Args).Delete(ImageCache{}) + db = core.CONTEXT.GetDB().Where(wp.Query, wp.Args).Delete(ImageCache{}) this.PanicError(db.Error) //删除文件实体 @@ -193,7 +194,7 @@ func (this *ImageCacheDao) DeleteByMatterUuid(matterUuid string) { //获取一段时间中文件总大小 func (this *ImageCacheDao) SizeBetweenTime(startTime time.Time, endTime time.Time) int64 { var size int64 - db := CONTEXT.GetDB().Model(&ImageCache{}).Where("create_time >= ? AND create_time <= ?", startTime, endTime).Select("SUM(size)") + db := core.CONTEXT.GetDB().Model(&ImageCache{}).Where("create_time >= ? AND create_time <= ?", startTime, endTime).Select("SUM(size)") this.PanicError(db.Error) row := db.Row() row.Scan(&size) @@ -203,6 +204,6 @@ func (this *ImageCacheDao) SizeBetweenTime(startTime time.Time, endTime time.Tim //执行清理操作 func (this *ImageCacheDao) Cleanup() { this.logger.Info("[ImageCacheDao]执行清理:清除数据库中所有ImageCache记录。") - db := CONTEXT.GetDB().Where("uuid is not null").Delete(ImageCache{}) + db := core.CONTEXT.GetDB().Where("uuid is not null").Delete(ImageCache{}) this.PanicError(db.Error) } diff --git a/code/rest/image_cache_service.go b/code/rest/image_cache_service.go index fb8a1fa..31eeff8 100644 --- a/code/rest/image_cache_service.go +++ b/code/rest/image_cache_service.go @@ -3,6 +3,7 @@ package rest import ( "fmt" "github.com/disintegration/imaging" + "github.com/eyebluecn/tank/code/core" "github.com/eyebluecn/tank/code/tool/util" "image" "net/http" @@ -25,17 +26,17 @@ func (this *ImageCacheService) Init() { this.Bean.Init() //手动装填本实例的Bean. 这里必须要用中间变量方可。 - b := CONTEXT.GetBean(this.imageCacheDao) + b := core.CONTEXT.GetBean(this.imageCacheDao) if b, ok := b.(*ImageCacheDao); ok { this.imageCacheDao = b } - b = CONTEXT.GetBean(this.userDao) + b = core.CONTEXT.GetBean(this.userDao) if b, ok := b.(*UserDao); ok { this.userDao = b } - b = CONTEXT.GetBean(this.matterDao) + b = core.CONTEXT.GetBean(this.matterDao) if b, ok := b.(*MatterDao); ok { this.matterDao = b } diff --git a/code/rest/install_controller.go b/code/rest/install_controller.go index 995f987..77b42ef 100644 --- a/code/rest/install_controller.go +++ b/code/rest/install_controller.go @@ -3,6 +3,7 @@ package rest import ( "fmt" "github.com/eyebluecn/tank/code/config" + "github.com/eyebluecn/tank/code/core" "github.com/eyebluecn/tank/code/tool/builder" "github.com/eyebluecn/tank/code/tool/result" "github.com/eyebluecn/tank/code/tool/util" @@ -34,32 +35,32 @@ func (this *InstallController) Init() { this.BaseController.Init() //手动装填本实例的Bean. - b := CONTEXT.GetBean(this.uploadTokenDao) + b := core.CONTEXT.GetBean(this.uploadTokenDao) if c, ok := b.(*UploadTokenDao); ok { this.uploadTokenDao = c } - b = CONTEXT.GetBean(this.downloadTokenDao) + b = core.CONTEXT.GetBean(this.downloadTokenDao) if c, ok := b.(*DownloadTokenDao); ok { this.downloadTokenDao = c } - b = CONTEXT.GetBean(this.matterDao) + b = core.CONTEXT.GetBean(this.matterDao) if c, ok := b.(*MatterDao); ok { this.matterDao = c } - b = CONTEXT.GetBean(this.matterService) + b = core.CONTEXT.GetBean(this.matterService) if c, ok := b.(*MatterService); ok { this.matterService = c } - b = CONTEXT.GetBean(this.imageCacheDao) + b = core.CONTEXT.GetBean(this.imageCacheDao) if c, ok := b.(*ImageCacheDao); ok { this.imageCacheDao = c } - b = CONTEXT.GetBean(this.imageCacheService) + b = core.CONTEXT.GetBean(this.imageCacheService) if c, ok := b.(*ImageCacheService); ok { this.imageCacheService = c } @@ -460,7 +461,7 @@ func (this *InstallController) Finish(writer http.ResponseWriter, request *http. config.CONFIG.InstallOk() //通知全局上下文,说系统安装好了 - CONTEXT.InstallOk() + core.CONTEXT.InstallOk() return this.Success("OK") } diff --git a/code/rest/matter_controller.go b/code/rest/matter_controller.go index e3eb929..b4e97fb 100644 --- a/code/rest/matter_controller.go +++ b/code/rest/matter_controller.go @@ -1,6 +1,7 @@ package rest import ( + "github.com/eyebluecn/tank/code/core" "github.com/eyebluecn/tank/code/tool/builder" "github.com/eyebluecn/tank/code/tool/result" "net/http" @@ -22,26 +23,26 @@ func (this *MatterController) Init() { this.BaseController.Init() //手动装填本实例的Bean. 这里必须要用中间变量方可。 - b := CONTEXT.GetBean(this.matterDao) + b := core.CONTEXT.GetBean(this.matterDao) if b, ok := b.(*MatterDao); ok { this.matterDao = b } - b = CONTEXT.GetBean(this.matterService) + b = core.CONTEXT.GetBean(this.matterService) if b, ok := b.(*MatterService); ok { this.matterService = b } - b = CONTEXT.GetBean(this.downloadTokenDao) + b = core.CONTEXT.GetBean(this.downloadTokenDao) if b, ok := b.(*DownloadTokenDao); ok { this.downloadTokenDao = b } - b = CONTEXT.GetBean(this.imageCacheDao) + b = core.CONTEXT.GetBean(this.imageCacheDao) if b, ok := b.(*ImageCacheDao); ok { this.imageCacheDao = b } - b = CONTEXT.GetBean(this.imageCacheService) + b = core.CONTEXT.GetBean(this.imageCacheService) if b, ok := b.(*ImageCacheService); ok { this.imageCacheService = b } @@ -193,7 +194,7 @@ func (this *MatterController) CreateDirectory(writer http.ResponseWriter, reques dirMatter = this.matterDao.CheckByUuid(puuid) } - matter := this.matterService.AtomicCreateDirectory(dirMatter, name, user); + matter := this.matterService.AtomicCreateDirectory(dirMatter, name, user) return this.Success(matter) } diff --git a/code/rest/matter_dao.go b/code/rest/matter_dao.go index 8f17479..33ebe80 100644 --- a/code/rest/matter_dao.go +++ b/code/rest/matter_dao.go @@ -2,6 +2,7 @@ package rest import ( "github.com/eyebluecn/tank/code/config" + "github.com/eyebluecn/tank/code/core" "github.com/eyebluecn/tank/code/tool/builder" "github.com/eyebluecn/tank/code/tool/result" "github.com/eyebluecn/tank/code/tool/util" @@ -21,7 +22,7 @@ func (this *MatterDao) Init() { this.BaseDao.Init() //手动装填本实例的Bean. 这里必须要用中间变量方可。 - b := CONTEXT.GetBean(this.imageCacheDao) + b := core.CONTEXT.GetBean(this.imageCacheDao) if b, ok := b.(*ImageCacheDao); ok { this.imageCacheDao = b } @@ -32,7 +33,7 @@ func (this *MatterDao) FindByUuid(uuid string) *Matter { // Read var matter Matter - db := CONTEXT.GetDB().Where(&Matter{Base: Base{Uuid: uuid}}).First(&matter) + db := core.CONTEXT.GetDB().Where(&Matter{Base: Base{Uuid: uuid}}).First(&matter) if db.Error != nil { if db.Error.Error() == result.DB_ERROR_NOT_FOUND { return nil @@ -111,7 +112,7 @@ func (this *MatterDao) FindByUserUuidAndPuuidAndNameAndDirTrue(userUuid string, wp = wp.And(&builder.WherePair{Query: "dir = ?", Args: []interface{}{1}}) var matter = &Matter{} - db := CONTEXT.GetDB().Model(&Matter{}).Where(wp.Query, wp.Args...).First(matter) + db := core.CONTEXT.GetDB().Model(&Matter{}).Where(wp.Query, wp.Args...).First(matter) if db.Error != nil { return nil @@ -125,7 +126,7 @@ func (this *MatterDao) CheckByUuidAndUserUuid(uuid string, userUuid string) *Mat // Read var matter = &Matter{} - db := CONTEXT.GetDB().Where(&Matter{Base: Base{Uuid: uuid}, UserUuid: userUuid}).First(matter) + db := core.CONTEXT.GetDB().Where(&Matter{Base: Base{Uuid: uuid}, UserUuid: userUuid}).First(matter) this.PanicError(db.Error) return matter @@ -154,7 +155,7 @@ func (this *MatterDao) CountByUserUuidAndPuuidAndDirAndName(userUuid string, puu wp = wp.And(&builder.WherePair{Query: "dir = ?", Args: []interface{}{dir}}) - db := CONTEXT.GetDB(). + db := core.CONTEXT.GetDB(). Model(&matter). Where(wp.Query, wp.Args...). Count(&count) @@ -168,7 +169,7 @@ func (this *MatterDao) ListByUserUuidAndPuuidAndDirAndName(userUuid string, puui var matters []*Matter - db := CONTEXT.GetDB(). + db := core.CONTEXT.GetDB(). Where(Matter{UserUuid: userUuid, Puuid: puuid, Dir: dir, Name: name}). Find(&matters) this.PanicError(db.Error) @@ -180,7 +181,7 @@ func (this *MatterDao) ListByUserUuidAndPuuidAndDirAndName(userUuid string, puui func (this *MatterDao) List(puuid string, userUuid string, sortArray []builder.OrderPair) []*Matter { var matters []*Matter - db := CONTEXT.GetDB().Where(Matter{UserUuid: userUuid, Puuid: puuid}).Order(this.GetSortString(sortArray)).Find(&matters) + db := core.CONTEXT.GetDB().Where(Matter{UserUuid: userUuid, Puuid: puuid}).Order(this.GetSortString(sortArray)).Find(&matters) this.PanicError(db.Error) return matters @@ -223,9 +224,9 @@ func (this *MatterDao) Page(page int, pageSize int, puuid string, userUuid strin orWp = orWp.Or(&builder.WherePair{Query: "name LIKE ?", Args: []interface{}{"%." + v}}) } - conditionDB = CONTEXT.GetDB().Model(&Matter{}).Where(wp.Query, wp.Args...).Where(orWp.Query, orWp.Args...) + conditionDB = core.CONTEXT.GetDB().Model(&Matter{}).Where(wp.Query, wp.Args...).Where(orWp.Query, orWp.Args...) } else { - conditionDB = CONTEXT.GetDB().Model(&Matter{}).Where(wp.Query, wp.Args...) + conditionDB = core.CONTEXT.GetDB().Model(&Matter{}).Where(wp.Query, wp.Args...) } count := 0 @@ -248,7 +249,7 @@ func (this *MatterDao) Create(matter *Matter) *Matter { matter.CreateTime = time.Now() matter.UpdateTime = time.Now() matter.Sort = time.Now().UnixNano() / 1e6 - db := CONTEXT.GetDB().Create(matter) + db := core.CONTEXT.GetDB().Create(matter) this.PanicError(db.Error) return matter @@ -258,7 +259,7 @@ func (this *MatterDao) Create(matter *Matter) *Matter { func (this *MatterDao) Save(matter *Matter) *Matter { matter.UpdateTime = time.Now() - db := CONTEXT.GetDB().Save(matter) + db := core.CONTEXT.GetDB().Save(matter) this.PanicError(db.Error) return matter @@ -266,7 +267,7 @@ func (this *MatterDao) Save(matter *Matter) *Matter { //计数器加一 func (this *MatterDao) TimesIncrement(matterUuid string) { - db := CONTEXT.GetDB().Model(&Matter{}).Where("uuid = ?", matterUuid).Update("times", gorm.Expr("times + 1")) + db := core.CONTEXT.GetDB().Model(&Matter{}).Where("uuid = ?", matterUuid).Update("times", gorm.Expr("times + 1")) this.PanicError(db.Error) } @@ -282,7 +283,7 @@ func (this *MatterDao) Delete(matter *Matter) { } //删除数据库中文件夹本身 - db := CONTEXT.GetDB().Delete(&matter) + db := core.CONTEXT.GetDB().Delete(&matter) this.PanicError(db.Error) //从磁盘中删除该文件夹。 @@ -291,7 +292,7 @@ func (this *MatterDao) Delete(matter *Matter) { } else { //删除数据库中文件记录 - db := CONTEXT.GetDB().Delete(&matter) + db := core.CONTEXT.GetDB().Delete(&matter) this.PanicError(db.Error) //删除对应的缓存图片。 @@ -311,7 +312,7 @@ func (this *MatterDao) Delete(matter *Matter) { //获取一段时间中,总的数量 func (this *MatterDao) CountBetweenTime(startTime time.Time, endTime time.Time) int64 { var count int64 - db := CONTEXT.GetDB().Model(&Matter{}).Where("create_time >= ? AND create_time <= ?", startTime, endTime).Count(&count) + db := core.CONTEXT.GetDB().Model(&Matter{}).Where("create_time >= ? AND create_time <= ?", startTime, endTime).Count(&count) this.PanicError(db.Error) return count } @@ -319,7 +320,7 @@ func (this *MatterDao) CountBetweenTime(startTime time.Time, endTime time.Time) //获取一段时间中文件总大小 func (this *MatterDao) SizeBetweenTime(startTime time.Time, endTime time.Time) int64 { var size int64 - db := CONTEXT.GetDB().Model(&Matter{}).Where("create_time >= ? AND create_time <= ?", startTime, endTime).Select("SUM(size)") + db := core.CONTEXT.GetDB().Model(&Matter{}).Where("create_time >= ? AND create_time <= ?", startTime, endTime).Select("SUM(size)") this.PanicError(db.Error) row := db.Row() err := row.Scan(&size) @@ -333,7 +334,7 @@ func (this *MatterDao) findByUserUuidAndPath(userUuid string, path string) *Matt var wp = &builder.WherePair{Query: "user_uuid = ? AND path = ?", Args: []interface{}{userUuid, path}} var matter = &Matter{} - db := CONTEXT.GetDB().Model(&Matter{}).Where(wp.Query, wp.Args...).First(matter) + db := core.CONTEXT.GetDB().Model(&Matter{}).Where(wp.Query, wp.Args...).First(matter) if db.Error != nil { if db.Error.Error() == result.DB_ERROR_NOT_FOUND { @@ -363,7 +364,7 @@ func (this *MatterDao) checkByUserUuidAndPath(userUuid string, path string) *Mat //执行清理操作 func (this *MatterDao) Cleanup() { this.logger.Info("[MatterDao]执行清理:清除数据库中所有Matter记录。删除磁盘中所有Matter文件。") - db := CONTEXT.GetDB().Where("uuid is not null").Delete(Matter{}) + db := core.CONTEXT.GetDB().Where("uuid is not null").Delete(Matter{}) this.PanicError(db.Error) err := os.RemoveAll(config.CONFIG.MatterPath) diff --git a/code/rest/matter_service.go b/code/rest/matter_service.go index c91c43a..899449a 100644 --- a/code/rest/matter_service.go +++ b/code/rest/matter_service.go @@ -1,6 +1,7 @@ package rest import ( + "github.com/eyebluecn/tank/code/core" "github.com/eyebluecn/tank/code/tool/download" "github.com/eyebluecn/tank/code/tool/result" "github.com/eyebluecn/tank/code/tool/util" @@ -30,27 +31,27 @@ func (this *MatterService) Init() { this.Bean.Init() //手动装填本实例的Bean. 这里必须要用中间变量方可。 - b := CONTEXT.GetBean(this.matterDao) + b := core.CONTEXT.GetBean(this.matterDao) if b, ok := b.(*MatterDao); ok { this.matterDao = b } - b = CONTEXT.GetBean(this.userDao) + b = core.CONTEXT.GetBean(this.userDao) if b, ok := b.(*UserDao); ok { this.userDao = b } - b = CONTEXT.GetBean(this.userService) + b = core.CONTEXT.GetBean(this.userService) if b, ok := b.(*UserService); ok { this.userService = b } - b = CONTEXT.GetBean(this.imageCacheDao) + b = core.CONTEXT.GetBean(this.imageCacheDao) if b, ok := b.(*ImageCacheDao); ok { this.imageCacheDao = b } - b = CONTEXT.GetBean(this.imageCacheService) + b = core.CONTEXT.GetBean(this.imageCacheService) if b, ok := b.(*ImageCacheService); ok { this.imageCacheService = b } diff --git a/code/rest/preference_controller.go b/code/rest/preference_controller.go index 32ce9fb..8b0e6c4 100644 --- a/code/rest/preference_controller.go +++ b/code/rest/preference_controller.go @@ -1,6 +1,7 @@ package rest import ( + "github.com/eyebluecn/tank/code/core" "github.com/eyebluecn/tank/code/tool/result" "github.com/eyebluecn/tank/code/tool/util" "net/http" @@ -17,12 +18,12 @@ func (this *PreferenceController) Init() { this.BaseController.Init() //手动装填本实例的Bean. 这里必须要用中间变量方可。 - b := CONTEXT.GetBean(this.preferenceDao) + b := core.CONTEXT.GetBean(this.preferenceDao) if b, ok := b.(*PreferenceDao); ok { this.preferenceDao = b } - b = CONTEXT.GetBean(this.preferenceService) + b = core.CONTEXT.GetBean(this.preferenceService) if b, ok := b.(*PreferenceService); ok { this.preferenceService = b } @@ -95,9 +96,8 @@ func (this *PreferenceController) SystemCleanup(writer http.ResponseWriter, requ panic(result.BadRequest("密码错误,不能执行!")) } - for _, bean := range CONTEXT.BeanMap { - bean.Cleanup() - } + //清空系统 + core.CONTEXT.Cleanup() return this.Success("OK") } diff --git a/code/rest/preference_dao.go b/code/rest/preference_dao.go index c16817b..5e0e5be 100644 --- a/code/rest/preference_dao.go +++ b/code/rest/preference_dao.go @@ -1,6 +1,7 @@ package rest import ( + "github.com/eyebluecn/tank/code/core" "github.com/eyebluecn/tank/code/tool/result" "github.com/nu7hatch/gouuid" "time" @@ -15,7 +16,7 @@ func (this *PreferenceDao) Fetch() *Preference { // Read var preference = &Preference{} - db := CONTEXT.GetDB().First(preference) + db := core.CONTEXT.GetDB().First(preference) if db.Error != nil { if db.Error.Error() == result.DB_ERROR_NOT_FOUND { @@ -39,7 +40,7 @@ func (this *PreferenceDao) Create(preference *Preference) *Preference { preference.CreateTime = time.Now() preference.UpdateTime = time.Now() preference.Sort = time.Now().UnixNano() / 1e6 - db := CONTEXT.GetDB().Create(preference) + db := core.CONTEXT.GetDB().Create(preference) this.PanicError(db.Error) return preference @@ -49,7 +50,7 @@ func (this *PreferenceDao) Create(preference *Preference) *Preference { func (this *PreferenceDao) Save(preference *Preference) *Preference { preference.UpdateTime = time.Now() - db := CONTEXT.GetDB().Save(preference) + db := core.CONTEXT.GetDB().Save(preference) this.PanicError(db.Error) return preference @@ -59,6 +60,6 @@ func (this *PreferenceDao) Save(preference *Preference) *Preference { func (this *PreferenceDao) Cleanup() { this.logger.Info("[PreferenceDao]执行清理:清除数据库中所有Preference记录。") - db := CONTEXT.GetDB().Where("uuid is not null").Delete(Preference{}) + db := core.CONTEXT.GetDB().Where("uuid is not null").Delete(Preference{}) this.PanicError(db.Error) } diff --git a/code/rest/preference_service.go b/code/rest/preference_service.go index e12c6de..8d0e0c7 100644 --- a/code/rest/preference_service.go +++ b/code/rest/preference_service.go @@ -1,5 +1,7 @@ package rest +import "github.com/eyebluecn/tank/code/core" + //@Service type PreferenceService struct { Bean @@ -12,7 +14,7 @@ func (this *PreferenceService) Init() { this.Bean.Init() //手动装填本实例的Bean. 这里必须要用中间变量方可。 - b := CONTEXT.GetBean(this.preferenceDao) + b := core.CONTEXT.GetBean(this.preferenceDao) if b, ok := b.(*PreferenceDao); ok { this.preferenceDao = b } diff --git a/code/rest/router.go b/code/rest/router.go index 598d01e..66c3dae 100644 --- a/code/rest/router.go +++ b/code/rest/router.go @@ -31,25 +31,25 @@ func NewRouter() *Router { } //installController. - b := CONTEXT.GetBean(router.installController) + b := core.CONTEXT.GetBean(router.installController) if b, ok := b.(*InstallController); ok { router.installController = b } //装载userService. - b = CONTEXT.GetBean(router.userService) + b = core.CONTEXT.GetBean(router.userService) if b, ok := b.(*UserService); ok { router.userService = b } //装载footprintService - b = CONTEXT.GetBean(router.footprintService) + b = core.CONTEXT.GetBean(router.footprintService) if b, ok := b.(*FootprintService); ok { router.footprintService = b } //将Controller中的路由规则装载进来,InstallController中的除外 - for _, controller := range CONTEXT.ControllerMap { + for _, controller := range core.CONTEXT.GetControllerMap() { if controller == router.installController { routes := controller.RegisterRoutes() @@ -142,7 +142,7 @@ func (this *Router) ServeHTTP(writer http.ResponseWriter, request *http.Request) } else { //直接将请求扔给每个controller,看看他们能不能处理,如果都不能处理,那就抛出找不到的错误 canHandle := false - for _, controller := range CONTEXT.ControllerMap { + for _, controller := range core.CONTEXT.GetControllerMap() { if handler, exist := controller.HandleRoutes(writer, request); exist { canHandle = true handler(writer, request) diff --git a/code/rest/session_dao.go b/code/rest/session_dao.go index 0f960c4..0d0d7fa 100644 --- a/code/rest/session_dao.go +++ b/code/rest/session_dao.go @@ -1,6 +1,7 @@ package rest import ( + "github.com/eyebluecn/tank/code/core" "github.com/nu7hatch/gouuid" "time" ) @@ -14,7 +15,7 @@ func (this *SessionDao) FindByUuid(uuid string) *Session { // Read var session = &Session{} - db := CONTEXT.GetDB().Where(&Session{Base: Base{Uuid: uuid}}).First(session) + db := core.CONTEXT.GetDB().Where(&Session{Base: Base{Uuid: uuid}}).First(session) if db.Error != nil { return nil } @@ -26,7 +27,7 @@ func (this *SessionDao) CheckByUuid(uuid string) *Session { // Read var session = &Session{} - db := CONTEXT.GetDB().Where(&Session{Base: Base{Uuid: uuid}}).First(session) + db := core.CONTEXT.GetDB().Where(&Session{Base: Base{Uuid: uuid}}).First(session) this.PanicError(db.Error) return session } @@ -39,7 +40,7 @@ func (this *SessionDao) Create(session *Session) *Session { session.CreateTime = time.Now() session.UpdateTime = time.Now() session.Sort = time.Now().UnixNano() / 1e6 - db := CONTEXT.GetDB().Create(session) + db := core.CONTEXT.GetDB().Create(session) this.PanicError(db.Error) return session @@ -49,7 +50,7 @@ func (this *SessionDao) Create(session *Session) *Session { func (this *SessionDao) Save(session *Session) *Session { session.UpdateTime = time.Now() - db := CONTEXT.GetDB().Save(session) + db := core.CONTEXT.GetDB().Save(session) this.PanicError(db.Error) return session @@ -60,7 +61,7 @@ func (this *SessionDao) Delete(uuid string) { session := this.CheckByUuid(uuid) session.ExpireTime = time.Now() - db := CONTEXT.GetDB().Delete(session) + db := core.CONTEXT.GetDB().Delete(session) this.PanicError(db.Error) @@ -69,6 +70,6 @@ func (this *SessionDao) Delete(uuid string) { //执行清理操作 func (this *SessionDao) Cleanup() { this.logger.Info("[SessionDao]执行清理:清除数据库中所有Session记录。") - db := CONTEXT.GetDB().Where("uuid is not null").Delete(Session{}) + db := core.CONTEXT.GetDB().Where("uuid is not null").Delete(Session{}) this.PanicError(db.Error) } diff --git a/code/rest/session_service.go b/code/rest/session_service.go index bb39541..3b1b85c 100644 --- a/code/rest/session_service.go +++ b/code/rest/session_service.go @@ -1,5 +1,7 @@ package rest +import "github.com/eyebluecn/tank/code/core" + //@Service type SessionService struct { Bean @@ -12,12 +14,12 @@ func (this *SessionService) Init() { this.Bean.Init() //手动装填本实例的Bean. 这里必须要用中间变量方可。 - b := CONTEXT.GetBean(this.userDao) + b := core.CONTEXT.GetBean(this.userDao) if b, ok := b.(*UserDao); ok { this.userDao = b } - b = CONTEXT.GetBean(this.sessionDao) + b = core.CONTEXT.GetBean(this.sessionDao) if b, ok := b.(*SessionDao); ok { this.sessionDao = b } @@ -27,7 +29,7 @@ func (this *SessionService) Init() { //执行清理操作 func (this *SessionService) Cleanup() { - this.logger.Info("[SessionService]执行清理:清除缓存中所有Session记录,共%d条。", CONTEXT.SessionCache.Count()) + this.logger.Info("[SessionService]执行清理:清除缓存中所有Session记录,共%d条。", core.CONTEXT.GetSessionCache().Count()) - CONTEXT.SessionCache.Truncate() + core.CONTEXT.GetSessionCache().Truncate() } diff --git a/code/rest/upload_token_dao.go b/code/rest/upload_token_dao.go index e100686..89d6ffc 100644 --- a/code/rest/upload_token_dao.go +++ b/code/rest/upload_token_dao.go @@ -1,6 +1,7 @@ package rest import ( + "github.com/eyebluecn/tank/code/core" "github.com/nu7hatch/gouuid" "time" ) @@ -14,7 +15,7 @@ func (this *UploadTokenDao) FindByUuid(uuid string) *UploadToken { // Read var uploadToken = &UploadToken{} - db := CONTEXT.GetDB().Where(&UploadToken{Base: Base{Uuid: uuid}}).First(uploadToken) + db := core.CONTEXT.GetDB().Where(&UploadToken{Base: Base{Uuid: uuid}}).First(uploadToken) if db.Error != nil { return nil } @@ -32,7 +33,7 @@ func (this *UploadTokenDao) Create(uploadToken *UploadToken) *UploadToken { uploadToken.CreateTime = time.Now() uploadToken.UpdateTime = time.Now() uploadToken.Sort = time.Now().UnixNano() / 1e6 - db := CONTEXT.GetDB().Create(uploadToken) + db := core.CONTEXT.GetDB().Create(uploadToken) this.PanicError(db.Error) return uploadToken @@ -42,7 +43,7 @@ func (this *UploadTokenDao) Create(uploadToken *UploadToken) *UploadToken { func (this *UploadTokenDao) Save(uploadToken *UploadToken) *UploadToken { uploadToken.UpdateTime = time.Now() - db := CONTEXT.GetDB().Save(uploadToken) + db := core.CONTEXT.GetDB().Save(uploadToken) this.PanicError(db.Error) return uploadToken diff --git a/code/rest/user_controller.go b/code/rest/user_controller.go index 5c2a101..602c2dd 100644 --- a/code/rest/user_controller.go +++ b/code/rest/user_controller.go @@ -2,6 +2,7 @@ package rest import ( "github.com/eyebluecn/tank/code/config" + "github.com/eyebluecn/tank/code/core" "github.com/eyebluecn/tank/code/tool/builder" "github.com/eyebluecn/tank/code/tool/result" "github.com/eyebluecn/tank/code/tool/util" @@ -231,7 +232,7 @@ func (this *UserController) Logout(writer http.ResponseWriter, request *http.Req } //删掉session缓存 - _, err = CONTEXT.SessionCache.Delete(sessionId) + _, err = core.CONTEXT.GetSessionCache().Delete(sessionId) if err != nil { this.logger.Error("删除用户session缓存时出错") } diff --git a/code/rest/user_dao.go b/code/rest/user_dao.go index 4a319df..48326a4 100644 --- a/code/rest/user_dao.go +++ b/code/rest/user_dao.go @@ -1,6 +1,7 @@ package rest import ( + "github.com/eyebluecn/tank/code/core" "github.com/eyebluecn/tank/code/tool/builder" "github.com/nu7hatch/gouuid" "time" @@ -24,7 +25,7 @@ func (this *UserDao) Create(user *User) *User { user.LastTime = time.Now() user.Sort = time.Now().UnixNano() / 1e6 - db := CONTEXT.GetDB().Create(user) + db := core.CONTEXT.GetDB().Create(user) this.PanicError(db.Error) return user @@ -35,7 +36,7 @@ func (this *UserDao) FindByUuid(uuid string) *User { // Read var user *User = &User{} - db := CONTEXT.GetDB().Where(&User{Base: Base{Uuid: uuid}}).First(user) + db := core.CONTEXT.GetDB().Where(&User{Base: Base{Uuid: uuid}}).First(user) if db.Error != nil { return nil } @@ -51,7 +52,7 @@ func (this *UserDao) CheckByUuid(uuid string) *User { // Read var user = &User{} - db := CONTEXT.GetDB().Where(&User{Base: Base{Uuid: uuid}}).First(user) + db := core.CONTEXT.GetDB().Where(&User{Base: Base{Uuid: uuid}}).First(user) this.PanicError(db.Error) return user } @@ -60,7 +61,7 @@ func (this *UserDao) CheckByUuid(uuid string) *User { func (this *UserDao) FindByUsername(username string) *User { var user = &User{} - db := CONTEXT.GetDB().Where(&User{Username: username}).First(user) + db := core.CONTEXT.GetDB().Where(&User{Username: username}).First(user) if db.Error != nil { return nil } @@ -71,7 +72,7 @@ func (this *UserDao) FindByUsername(username string) *User { func (this *UserDao) FindByEmail(email string) *User { var user *User = &User{} - db := CONTEXT.GetDB().Where(&User{Email: email}).First(user) + db := core.CONTEXT.GetDB().Where(&User{Email: email}).First(user) if db.Error != nil { return nil } @@ -100,15 +101,15 @@ func (this *UserDao) Page(page int, pageSize int, username string, email string, } count := 0 - db := CONTEXT.GetDB().Model(&User{}).Where(wp.Query, wp.Args...).Count(&count) + db := core.CONTEXT.GetDB().Model(&User{}).Where(wp.Query, wp.Args...).Count(&count) this.PanicError(db.Error) var users []*User orderStr := this.GetSortString(sortArray) if orderStr == "" { - db = CONTEXT.GetDB().Where(wp.Query, wp.Args...).Offset(page * pageSize).Limit(pageSize).Find(&users) + db = core.CONTEXT.GetDB().Where(wp.Query, wp.Args...).Offset(page * pageSize).Limit(pageSize).Find(&users) } else { - db = CONTEXT.GetDB().Where(wp.Query, wp.Args...).Order(orderStr).Offset(page * pageSize).Limit(pageSize).Find(&users) + db = core.CONTEXT.GetDB().Where(wp.Query, wp.Args...).Order(orderStr).Offset(page * pageSize).Limit(pageSize).Find(&users) } this.PanicError(db.Error) @@ -121,7 +122,7 @@ func (this *UserDao) Page(page int, pageSize int, username string, email string, //查询某个用户名是否已经有用户了 func (this *UserDao) CountByUsername(username string) int { var count int - db := CONTEXT.GetDB(). + db := core.CONTEXT.GetDB(). Model(&User{}). Where("username = ?", username). Count(&count) @@ -132,7 +133,7 @@ func (this *UserDao) CountByUsername(username string) int { //查询某个邮箱是否已经有用户了 func (this *UserDao) CountByEmail(email string) int { var count int - db := CONTEXT.GetDB(). + db := core.CONTEXT.GetDB(). Model(&User{}). Where("email = ?", email). Count(&count) @@ -144,7 +145,7 @@ func (this *UserDao) CountByEmail(email string) int { func (this *UserDao) Save(user *User) *User { user.UpdateTime = time.Now() - db := CONTEXT.GetDB(). + db := core.CONTEXT.GetDB(). Save(user) this.PanicError(db.Error) return user @@ -153,6 +154,6 @@ func (this *UserDao) Save(user *User) *User { //执行清理操作 func (this *UserDao) Cleanup() { this.logger.Info("[UserDao]执行清理:清除数据库中所有User记录。") - db := CONTEXT.GetDB().Where("uuid is not null and role != ?", USER_ROLE_ADMINISTRATOR).Delete(User{}) + db := core.CONTEXT.GetDB().Where("uuid is not null and role != ?", USER_ROLE_ADMINISTRATOR).Delete(User{}) this.PanicError(db.Error) } diff --git a/code/rest/user_service.go b/code/rest/user_service.go index e7903a3..3c8e3c0 100644 --- a/code/rest/user_service.go +++ b/code/rest/user_service.go @@ -2,6 +2,7 @@ package rest import ( "github.com/eyebluecn/tank/code/config" + "github.com/eyebluecn/tank/code/core" "github.com/eyebluecn/tank/code/tool/cache" "github.com/eyebluecn/tank/code/tool/result" "net/http" @@ -23,12 +24,12 @@ func (this *UserService) Init() { this.Bean.Init() //手动装填本实例的Bean. 这里必须要用中间变量方可。 - b := CONTEXT.GetBean(this.userDao) + b := core.CONTEXT.GetBean(this.userDao) if b, ok := b.(*UserDao); ok { this.userDao = b } - b = CONTEXT.GetBean(this.sessionDao) + b = core.CONTEXT.GetBean(this.sessionDao) if b, ok := b.(*SessionDao); ok { this.sessionDao = b } @@ -84,7 +85,7 @@ func (this *UserService) preHandle(writer http.ResponseWriter, request *http.Req sessionId := sessionCookie.Value //去缓存中捞取 - cacheItem, err := CONTEXT.SessionCache.Value(sessionId) + cacheItem, err := core.CONTEXT.GetSessionCache().Value(sessionId) if err != nil { this.logger.Error("获取缓存时出错了" + err.Error()) } @@ -100,7 +101,7 @@ func (this *UserService) preHandle(writer http.ResponseWriter, request *http.Req user := this.userDao.FindByUuid(session.UserUuid) if user != nil { //将用户装填进缓存中 - CONTEXT.SessionCache.Add(sessionCookie.Value, duration, user) + core.CONTEXT.GetSessionCache().Add(sessionCookie.Value, duration, user) } else { this.logger.Error("没有找到对应的user " + session.UserUuid) } diff --git a/main.go b/main.go index e00cdc2..c8fc965 100644 --- a/main.go +++ b/main.go @@ -23,10 +23,12 @@ func main() { config.CONFIG.Init() //全局运行的上下文 - rest.CONTEXT.Init() - defer rest.CONTEXT.Destroy() + tankContext := &rest.Context{} + tankContext.Init() + defer tankContext.Destroy() + core.CONTEXT = tankContext - http.Handle("/", rest.CONTEXT.Router) + http.Handle("/", core.CONTEXT) core.LOGGER.Info("App started at http://localhost:%v", config.CONFIG.ServerPort)