Try to abstract the core package.

This commit is contained in:
zicla
2019-04-27 23:25:46 +08:00
parent 2732b14eae
commit 02805363ac
6 changed files with 28 additions and 28 deletions

View File

@ -1,7 +1,7 @@
package config package config
import ( import (
"github.com/eyebluecn/tank/code/tool/inter" "github.com/eyebluecn/tank/code/core"
"github.com/eyebluecn/tank/code/tool/util" "github.com/eyebluecn/tank/code/tool/util"
"github.com/json-iterator/go" "github.com/json-iterator/go"
"io/ioutil" "io/ioutil"
@ -65,7 +65,7 @@ type ConfigItem struct {
func (this *ConfigItem) validate() bool { func (this *ConfigItem) validate() bool {
if this.ServerPort == 0 { if this.ServerPort == 0 {
inter.LOGGER.Error("ServerPort 未配置") core.LOGGER.Error("ServerPort 未配置")
return false return false
} else { } else {
//只要配置文件中有配置端口,就使用。 //只要配置文件中有配置端口,就使用。
@ -73,27 +73,27 @@ func (this *ConfigItem) validate() bool {
} }
if this.MysqlUsername == "" { if this.MysqlUsername == "" {
inter.LOGGER.Error("MysqlUsername 未配置") core.LOGGER.Error("MysqlUsername 未配置")
return false return false
} }
if this.MysqlPassword == "" { if this.MysqlPassword == "" {
inter.LOGGER.Error("MysqlPassword 未配置") core.LOGGER.Error("MysqlPassword 未配置")
return false return false
} }
if this.MysqlHost == "" { if this.MysqlHost == "" {
inter.LOGGER.Error("MysqlHost 未配置") core.LOGGER.Error("MysqlHost 未配置")
return false return false
} }
if this.MysqlPort == 0 { if this.MysqlPort == 0 {
inter.LOGGER.Error("MysqlPort 未配置") core.LOGGER.Error("MysqlPort 未配置")
return false return false
} }
if this.MysqlSchema == "" { if this.MysqlSchema == "" {
inter.LOGGER.Error("MysqlSchema 未配置") core.LOGGER.Error("MysqlSchema 未配置")
return false return false
} }
@ -135,14 +135,14 @@ func (this *Config) ReadFromConfigFile() {
filePath := util.GetConfPath() + "/tank.json" filePath := util.GetConfPath() + "/tank.json"
content, err := ioutil.ReadFile(filePath) content, err := ioutil.ReadFile(filePath)
if err != nil { if err != nil {
inter.LOGGER.Warn("无法找到配置文件:%s 即将进入安装过程!", filePath) core.LOGGER.Warn("无法找到配置文件:%s 即将进入安装过程!", filePath)
this.Installed = false this.Installed = false
} else { } else {
this.Item = &ConfigItem{} this.Item = &ConfigItem{}
inter.LOGGER.Warn("读取配置文件:%s", filePath) core.LOGGER.Warn("读取配置文件:%s", filePath)
err := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(content, this.Item) err := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(content, this.Item)
if err != nil { if err != nil {
inter.LOGGER.Error("配置文件格式错误! 即将进入安装过程!") core.LOGGER.Error("配置文件格式错误! 即将进入安装过程!")
this.Installed = false this.Installed = false
return return
} }
@ -150,7 +150,7 @@ func (this *Config) ReadFromConfigFile() {
//验证项是否齐全 //验证项是否齐全
itemValidate := this.Item.validate() itemValidate := this.Item.validate()
if !itemValidate { if !itemValidate {
inter.LOGGER.Error("配置文件信息不齐全! 即将进入安装过程!") core.LOGGER.Error("配置文件信息不齐全! 即将进入安装过程!")
this.Installed = false this.Installed = false
return return
} }
@ -171,8 +171,8 @@ func (this *Config) ReadFromConfigFile() {
this.MysqlUrl = util.GetMysqlUrl(this.Item.MysqlPort, this.Item.MysqlHost, this.Item.MysqlSchema, this.Item.MysqlUsername, this.Item.MysqlPassword) this.MysqlUrl = util.GetMysqlUrl(this.Item.MysqlPort, this.Item.MysqlHost, this.Item.MysqlSchema, this.Item.MysqlUsername, this.Item.MysqlPassword)
this.Installed = true this.Installed = true
inter.LOGGER.Info("使用配置文件:%s", filePath) core.LOGGER.Info("使用配置文件:%s", filePath)
inter.LOGGER.Info("上传文件存放路径:%s", this.MatterPath) core.LOGGER.Info("上传文件存放路径:%s", this.MatterPath)
} }
} }

View File

@ -1,4 +1,4 @@
package inter package core
//日志系统必须高保 //日志系统必须高保
//全局唯一的日志对象(在main函数中初始化) //全局唯一的日志对象(在main函数中初始化)

View File

@ -2,7 +2,7 @@ package rest
import ( import (
"github.com/eyebluecn/tank/code/config" "github.com/eyebluecn/tank/code/config"
"github.com/eyebluecn/tank/code/tool/inter" "github.com/eyebluecn/tank/code/core"
"github.com/eyebluecn/tank/code/tool/result" "github.com/eyebluecn/tank/code/tool/result"
"github.com/eyebluecn/tank/code/tool/util" "github.com/eyebluecn/tank/code/tool/util"
"net/http" "net/http"
@ -20,11 +20,11 @@ type IBean interface {
} }
type Bean struct { type Bean struct {
logger inter.Logger logger core.Logger
} }
func (this *Bean) Init() { func (this *Bean) Init() {
this.logger = inter.LOGGER this.logger = core.LOGGER
} }
func (this *Bean) Bootstrap() { func (this *Bean) Bootstrap() {

View File

@ -3,8 +3,8 @@ package rest
import ( import (
"fmt" "fmt"
"github.com/eyebluecn/tank/code/config" "github.com/eyebluecn/tank/code/config"
"github.com/eyebluecn/tank/code/core"
cache2 "github.com/eyebluecn/tank/code/tool/cache" cache2 "github.com/eyebluecn/tank/code/tool/cache"
"github.com/eyebluecn/tank/code/tool/inter"
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
"reflect" "reflect"
) )
@ -56,7 +56,7 @@ func (this *Context) OpenDb() {
this.DB, err = gorm.Open("mysql", config.CONFIG.MysqlUrl) this.DB, err = gorm.Open("mysql", config.CONFIG.MysqlUrl)
if err != nil { if err != nil {
inter.LOGGER.Panic("failed to connect mysql database") core.LOGGER.Panic("failed to connect mysql database")
} }
//是否打开sql日志(在调试阶段可以打开以方便查看执行的SQL) //是否打开sql日志(在调试阶段可以打开以方便查看执行的SQL)
@ -68,7 +68,7 @@ func (this *Context) CloseDb() {
if this.DB != nil { if this.DB != nil {
err := this.DB.Close() err := this.DB.Close()
if err != nil { if err != nil {
inter.LOGGER.Error("关闭数据库连接出错 %s", err.Error()) core.LOGGER.Error("关闭数据库连接出错 %s", err.Error())
} }
} }
} }
@ -83,7 +83,7 @@ func (this *Context) registerBean(bean IBean) {
err := fmt.Sprintf("【%s】已经被注册了跳过。", typeName) err := fmt.Sprintf("【%s】已经被注册了跳过。", typeName)
if _, ok := this.BeanMap[typeName]; ok { if _, ok := this.BeanMap[typeName]; ok {
inter.LOGGER.Error(fmt.Sprintf(err)) core.LOGGER.Error(fmt.Sprintf(err))
} else { } else {
this.BeanMap[typeName] = element this.BeanMap[typeName] = element
@ -95,7 +95,7 @@ func (this *Context) registerBean(bean IBean) {
} }
} else { } else {
inter.LOGGER.Panic("注册的【%s】不是Bean类型。", typeName) core.LOGGER.Panic("注册的【%s】不是Bean类型。", typeName)
} }
} }
@ -165,7 +165,7 @@ func (this *Context) GetBean(bean IBean) IBean {
if val, ok := this.BeanMap[typeName]; ok { if val, ok := this.BeanMap[typeName]; ok {
return val return val
} else { } else {
inter.LOGGER.Panic("【%s】没有注册。", typeName) core.LOGGER.Panic("【%s】没有注册。", typeName)
return nil return nil
} }
} }

View File

@ -3,7 +3,7 @@ package rest
import ( import (
"fmt" "fmt"
"github.com/eyebluecn/tank/code/config" "github.com/eyebluecn/tank/code/config"
"github.com/eyebluecn/tank/code/tool/inter" "github.com/eyebluecn/tank/code/core"
"github.com/eyebluecn/tank/code/tool/result" "github.com/eyebluecn/tank/code/tool/result"
"github.com/eyebluecn/tank/code/tool/util" "github.com/eyebluecn/tank/code/tool/util"
"github.com/json-iterator/go" "github.com/json-iterator/go"
@ -72,7 +72,7 @@ func NewRouter() *Router {
func (this *Router) GlobalPanicHandler(writer http.ResponseWriter, request *http.Request, startTime time.Time) { func (this *Router) GlobalPanicHandler(writer http.ResponseWriter, request *http.Request, startTime time.Time) {
if err := recover(); err != nil { if err := recover(); err != nil {
inter.LOGGER.Error("错误: %v", err) core.LOGGER.Error("错误: %v", err)
var webResult *result.WebResult = nil var webResult *result.WebResult = nil
if value, ok := err.(string); ok { if value, ok := err.(string); ok {

View File

@ -3,9 +3,9 @@ package main
import ( 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/rest" "github.com/eyebluecn/tank/code/rest"
"github.com/eyebluecn/tank/code/support" "github.com/eyebluecn/tank/code/support"
"github.com/eyebluecn/tank/code/tool/inter"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
"log" "log"
"net/http" "net/http"
@ -17,7 +17,7 @@ func main() {
tankLogger := &support.TankLogger{} tankLogger := &support.TankLogger{}
tankLogger.Init() tankLogger.Init()
defer tankLogger.Destroy() defer tankLogger.Destroy()
inter.LOGGER = tankLogger core.LOGGER = tankLogger
//装载配置文件,这个决定了是否需要执行安装过程 //装载配置文件,这个决定了是否需要执行安装过程
config.CONFIG.Init() config.CONFIG.Init()
@ -28,7 +28,7 @@ func main() {
http.Handle("/", rest.CONTEXT.Router) http.Handle("/", rest.CONTEXT.Router)
inter.LOGGER.Info("App started at http://localhost:%v", config.CONFIG.ServerPort) core.LOGGER.Info("App started at http://localhost:%v", config.CONFIG.ServerPort)
dotPort := fmt.Sprintf(":%v", config.CONFIG.ServerPort) dotPort := fmt.Sprintf(":%v", config.CONFIG.ServerPort)
err1 := http.ListenAndServe(dotPort, nil) err1 := http.ListenAndServe(dotPort, nil)