Finish refining all the logger things.
This commit is contained in:
parent
4af940647d
commit
6485e3b48e
8
main.go
8
main.go
@ -10,9 +10,14 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
|
//日志第一优先级保障
|
||||||
|
rest.LOGGER.Init()
|
||||||
|
defer rest.LOGGER.Destroy()
|
||||||
|
|
||||||
//将运行时参数装填到config中去。
|
//将运行时参数装填到config中去。
|
||||||
rest.PrepareConfigs()
|
rest.PrepareConfigs()
|
||||||
|
|
||||||
|
//全局运行的上下文
|
||||||
rest.CONTEXT.Init()
|
rest.CONTEXT.Init()
|
||||||
defer rest.CONTEXT.Destroy()
|
defer rest.CONTEXT.Destroy()
|
||||||
|
|
||||||
@ -20,8 +25,7 @@ func main() {
|
|||||||
|
|
||||||
dotPort := fmt.Sprintf(":%v", rest.CONFIG.ServerPort)
|
dotPort := fmt.Sprintf(":%v", rest.CONFIG.ServerPort)
|
||||||
|
|
||||||
info := fmt.Sprintf("App started at http://localhost%v", dotPort)
|
rest.LOGGER.Info("App started at http://localhost:%v", rest.CONFIG.ServerPort)
|
||||||
rest.LogInfo(info)
|
|
||||||
|
|
||||||
err1 := http.ListenAndServe(dotPort, nil)
|
err1 := http.ListenAndServe(dotPort, nil)
|
||||||
if err1 != nil {
|
if err1 != nil {
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
package rest
|
package rest
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
"fmt"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
//@Service
|
//@Service
|
||||||
@ -20,6 +20,7 @@ type AlienService struct {
|
|||||||
|
|
||||||
//初始化方法
|
//初始化方法
|
||||||
func (this *AlienService) Init() {
|
func (this *AlienService) Init() {
|
||||||
|
this.Bean.Init()
|
||||||
|
|
||||||
//手动装填本实例的Bean. 这里必须要用中间变量方可。
|
//手动装填本实例的Bean. 这里必须要用中间变量方可。
|
||||||
b := CONTEXT.GetBean(this.matterDao)
|
b := CONTEXT.GetBean(this.matterDao)
|
||||||
@ -67,7 +68,7 @@ func (this *AlienService) PreviewOrDownload(
|
|||||||
operator *User,
|
operator *User,
|
||||||
withContentDisposition bool) {
|
withContentDisposition bool) {
|
||||||
|
|
||||||
LogInfo("预览或下载文件 " + uuid + " " + filename)
|
this.logger.Info("预览或下载文件 " + uuid + " " + filename)
|
||||||
|
|
||||||
matter := this.matterDao.CheckByUuid(uuid)
|
matter := this.matterDao.CheckByUuid(uuid)
|
||||||
|
|
||||||
|
@ -136,30 +136,30 @@ func (this *BaseController) findUser(writer http.ResponseWriter, request *http.R
|
|||||||
//验证用户是否已经登录。
|
//验证用户是否已经登录。
|
||||||
sessionCookie, err := request.Cookie(COOKIE_AUTH_KEY)
|
sessionCookie, err := request.Cookie(COOKIE_AUTH_KEY)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
LogInfo("获取用户cookie时出错啦")
|
this.logger.Warn("获取用户cookie信息失败啦~")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
sessionId := sessionCookie.Value
|
sessionId := sessionCookie.Value
|
||||||
|
|
||||||
LogInfo("findUser sessionId = " + sessionId)
|
this.logger.Info("findUser sessionId = %s", sessionId)
|
||||||
|
|
||||||
//去缓存中捞取看看
|
//去缓存中捞取看看
|
||||||
cacheItem, err := CONTEXT.SessionCache.Value(sessionId)
|
cacheItem, err := CONTEXT.SessionCache.Value(sessionId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
LogError("获取缓存时出错了" + err.Error())
|
this.logger.Warn("获取缓存时出错了" + err.Error())
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if cacheItem.Data() == nil {
|
if cacheItem.Data() == nil {
|
||||||
LogError("cache item中已经不存在了 " + err.Error())
|
this.logger.Warn("cache item中已经不存在了 " + err.Error())
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if value, ok := cacheItem.Data().(*User); ok {
|
if value, ok := cacheItem.Data().(*User); ok {
|
||||||
return value
|
return value
|
||||||
} else {
|
} else {
|
||||||
LogError("cache item中的类型不是*User ")
|
this.logger.Error("cache item中的类型不是*User ")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
10
rest/bean.go
10
rest/bean.go
@ -1,6 +1,8 @@
|
|||||||
package rest
|
package rest
|
||||||
|
|
||||||
import "net/http"
|
import (
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
type IBean interface {
|
type IBean interface {
|
||||||
Init()
|
Init()
|
||||||
@ -9,11 +11,11 @@ type IBean interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Bean struct {
|
type Bean struct {
|
||||||
|
logger *Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Bean) Init() {
|
func (this *Bean) Init() {
|
||||||
|
this.logger = LOGGER
|
||||||
}
|
}
|
||||||
|
|
||||||
//处理错误的统一方法
|
//处理错误的统一方法
|
||||||
@ -27,5 +29,3 @@ func (this *Bean) PanicError(err error) {
|
|||||||
func (this *Bean) PanicWebError(msg string, httpStatusCode int) {
|
func (this *Bean) PanicWebError(msg string, httpStatusCode int) {
|
||||||
panic(&WebError{Msg: msg, Code: httpStatusCode})
|
panic(&WebError{Msg: msg, Code: httpStatusCode})
|
||||||
}
|
}
|
||||||
|
|
||||||
//处理日志的统一方法。
|
|
||||||
|
@ -6,10 +6,10 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/json-iterator/go"
|
"github.com/json-iterator/go"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"time"
|
|
||||||
"unsafe"
|
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"time"
|
||||||
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -39,10 +39,6 @@ var (
|
|||||||
|
|
||||||
//默认监听端口号
|
//默认监听端口号
|
||||||
ServerPort: 6010,
|
ServerPort: 6010,
|
||||||
//将日志输出到控制台。
|
|
||||||
LogToConsole: true,
|
|
||||||
//日志的保存路径,如果没有指定,默认在根目录下的log文件夹中
|
|
||||||
LogPath: "",
|
|
||||||
//上传的文件路径,如果没有指定,默认在根目录下的matter文件夹中
|
//上传的文件路径,如果没有指定,默认在根目录下的matter文件夹中
|
||||||
MatterPath: "",
|
MatterPath: "",
|
||||||
//mysql相关配置。
|
//mysql相关配置。
|
||||||
@ -71,12 +67,6 @@ var (
|
|||||||
type Config struct {
|
type Config struct {
|
||||||
//默认监听端口号
|
//默认监听端口号
|
||||||
ServerPort int
|
ServerPort int
|
||||||
|
|
||||||
//将日志输出到控制台。
|
|
||||||
LogToConsole bool
|
|
||||||
|
|
||||||
//日志的保存路径,要求不以/结尾。如果没有指定,默认在根目录下的log文件夹中。eg: /var/log/tank
|
|
||||||
LogPath string
|
|
||||||
//上传的文件路径,要求不以/结尾。如果没有指定,默认在根目录下的matter文件夹中。eg: /var/www/matter
|
//上传的文件路径,要求不以/结尾。如果没有指定,默认在根目录下的matter文件夹中。eg: /var/www/matter
|
||||||
MatterPath string
|
MatterPath string
|
||||||
|
|
||||||
@ -106,27 +96,27 @@ type Config struct {
|
|||||||
func (this *Config) validate() {
|
func (this *Config) validate() {
|
||||||
|
|
||||||
if this.ServerPort == 0 {
|
if this.ServerPort == 0 {
|
||||||
LogPanic("ServerPort 未配置")
|
LOGGER.Error("ServerPort 未配置")
|
||||||
}
|
}
|
||||||
|
|
||||||
if this.MysqlUsername == "" {
|
if this.MysqlUsername == "" {
|
||||||
LogPanic("MysqlUsername 未配置")
|
LOGGER.Error("MysqlUsername 未配置")
|
||||||
}
|
}
|
||||||
|
|
||||||
if this.MysqlPassword == "" {
|
if this.MysqlPassword == "" {
|
||||||
LogPanic("MysqlPassword 未配置")
|
LOGGER.Error("MysqlPassword 未配置")
|
||||||
}
|
}
|
||||||
|
|
||||||
if this.MysqlHost == "" {
|
if this.MysqlHost == "" {
|
||||||
LogPanic("MysqlHost 未配置")
|
LOGGER.Error("MysqlHost 未配置")
|
||||||
}
|
}
|
||||||
|
|
||||||
if this.MysqlPort == 0 {
|
if this.MysqlPort == 0 {
|
||||||
LogPanic("MysqlPort 未配置")
|
LOGGER.Error("MysqlPort 未配置")
|
||||||
}
|
}
|
||||||
|
|
||||||
if this.MysqlSchema == "" {
|
if this.MysqlSchema == "" {
|
||||||
LogPanic("MysqlSchema 未配置")
|
LOGGER.Error("MysqlSchema 未配置")
|
||||||
}
|
}
|
||||||
|
|
||||||
this.MysqlUrl = fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8&parseTime=True&loc=Local", this.MysqlUsername, this.MysqlPassword, this.MysqlHost, this.MysqlPort, this.MysqlSchema)
|
this.MysqlUrl = fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8&parseTime=True&loc=Local", this.MysqlUsername, this.MysqlPassword, this.MysqlHost, this.MysqlPort, this.MysqlSchema)
|
||||||
@ -160,12 +150,12 @@ func LoadConfigFromFile() {
|
|||||||
filePath := GetConfPath() + "/tank.json"
|
filePath := GetConfPath() + "/tank.json"
|
||||||
content, err := ioutil.ReadFile(filePath)
|
content, err := ioutil.ReadFile(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
LogWarning(fmt.Sprintf("无法找到配置文件:%s,错误:%v\n将使用config.go中的默认配置项。", filePath, err))
|
LOGGER.Warn(fmt.Sprintf("无法找到配置文件:%s,错误:%v\n将使用config.go中的默认配置项。", filePath, err))
|
||||||
} else {
|
} else {
|
||||||
// 用 json.Unmarshal
|
// 用 json.Unmarshal
|
||||||
err := json.Unmarshal(content, CONFIG)
|
err := json.Unmarshal(content, CONFIG)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
LogPanic("配置文件格式错误!")
|
LOGGER.Panic("配置文件格式错误!")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -180,15 +170,10 @@ func LoadConfigFromEnvironment() {
|
|||||||
if e == nil {
|
if e == nil {
|
||||||
CONFIG.ServerPort = i
|
CONFIG.ServerPort = i
|
||||||
} else {
|
} else {
|
||||||
LogPanic(fmt.Sprintf("环境变量TANK_SERVER_PORT必须为整数!%v", tmpServerPort))
|
LOGGER.Panic(fmt.Sprintf("环境变量TANK_SERVER_PORT必须为整数!%v", tmpServerPort))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tmpLogPath := os.Getenv("TANK_LOG_PATH")
|
|
||||||
if tmpLogPath != "" {
|
|
||||||
CONFIG.LogPath = tmpLogPath
|
|
||||||
}
|
|
||||||
|
|
||||||
tmpMatterPath := os.Getenv("TANK_MATTER_PATH")
|
tmpMatterPath := os.Getenv("TANK_MATTER_PATH")
|
||||||
if tmpMatterPath != "" {
|
if tmpMatterPath != "" {
|
||||||
CONFIG.MatterPath = tmpMatterPath
|
CONFIG.MatterPath = tmpMatterPath
|
||||||
@ -200,7 +185,7 @@ func LoadConfigFromEnvironment() {
|
|||||||
if e == nil {
|
if e == nil {
|
||||||
CONFIG.MysqlPort = i
|
CONFIG.MysqlPort = i
|
||||||
} else {
|
} else {
|
||||||
LogPanic(fmt.Sprintf("环境变量TANK_MYSQL_PORT必须为整数!%v", tmpMysqlPort))
|
LOGGER.Panic(fmt.Sprintf("环境变量TANK_MYSQL_PORT必须为整数!%v", tmpMysqlPort))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -242,11 +227,7 @@ func LoadConfigFromArguments() {
|
|||||||
//系统端口号
|
//系统端口号
|
||||||
ServerPortPtr := flag.Int("ServerPort", CONFIG.ServerPort, "server port")
|
ServerPortPtr := flag.Int("ServerPort", CONFIG.ServerPort, "server port")
|
||||||
|
|
||||||
//系统端口号
|
|
||||||
LogToConsolePtr := flag.Bool("LogToConsole", CONFIG.LogToConsole, "write log to console. for debug.")
|
|
||||||
|
|
||||||
//日志和上传文件的路径
|
//日志和上传文件的路径
|
||||||
LogPathPtr := flag.String("LogPath", CONFIG.LogPath, "log path")
|
|
||||||
MatterPathPtr := flag.String("MatterPath", CONFIG.MatterPath, "matter path")
|
MatterPathPtr := flag.String("MatterPath", CONFIG.MatterPath, "matter path")
|
||||||
|
|
||||||
//mysql相关配置。
|
//mysql相关配置。
|
||||||
@ -268,14 +249,6 @@ func LoadConfigFromArguments() {
|
|||||||
CONFIG.ServerPort = *ServerPortPtr
|
CONFIG.ServerPort = *ServerPortPtr
|
||||||
}
|
}
|
||||||
|
|
||||||
if *LogToConsolePtr != CONFIG.LogToConsole {
|
|
||||||
CONFIG.LogToConsole = *LogToConsolePtr
|
|
||||||
}
|
|
||||||
|
|
||||||
if *LogPathPtr != CONFIG.LogPath {
|
|
||||||
CONFIG.LogPath = *LogPathPtr
|
|
||||||
}
|
|
||||||
|
|
||||||
if *MatterPathPtr != CONFIG.MatterPath {
|
if *MatterPathPtr != CONFIG.MatterPath {
|
||||||
CONFIG.MatterPath = *MatterPathPtr
|
CONFIG.MatterPath = *MatterPathPtr
|
||||||
}
|
}
|
||||||
@ -326,11 +299,6 @@ func PrepareConfigs() {
|
|||||||
//第三级. 从程序参数中读取配置项
|
//第三级. 从程序参数中读取配置项
|
||||||
LoadConfigFromArguments()
|
LoadConfigFromArguments()
|
||||||
|
|
||||||
//对于日志路径和文件路径还需要进行特殊处理
|
|
||||||
if CONFIG.LogPath == "" {
|
|
||||||
CONFIG.LogPath = GetHomePath() + "/log"
|
|
||||||
}
|
|
||||||
MakeDirAll(CONFIG.LogPath)
|
|
||||||
if CONFIG.MatterPath == "" {
|
if CONFIG.MatterPath == "" {
|
||||||
CONFIG.MatterPath = GetHomePath() + "/matter"
|
CONFIG.MatterPath = GetHomePath() + "/matter"
|
||||||
}
|
}
|
||||||
|
@ -15,8 +15,6 @@ type Context struct {
|
|||||||
DB *gorm.DB
|
DB *gorm.DB
|
||||||
//session缓存
|
//session缓存
|
||||||
SessionCache *CacheTable
|
SessionCache *CacheTable
|
||||||
//TODO:日志相关内容
|
|
||||||
|
|
||||||
//各类的Bean Map。这里面是包含ControllerMap中所有元素
|
//各类的Bean Map。这里面是包含ControllerMap中所有元素
|
||||||
BeanMap map[string]IBean
|
BeanMap map[string]IBean
|
||||||
//只包含了Controller的map
|
//只包含了Controller的map
|
||||||
@ -25,7 +23,6 @@ type Context struct {
|
|||||||
Router *Router
|
Router *Router
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//初始化上下文
|
//初始化上下文
|
||||||
func (this *Context) Init() {
|
func (this *Context) Init() {
|
||||||
|
|
||||||
@ -72,7 +69,6 @@ func (this *Context) CloseDb() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//注册一个Bean
|
//注册一个Bean
|
||||||
func (this *Context) registerBean(bean IBean) {
|
func (this *Context) registerBean(bean IBean) {
|
||||||
|
|
||||||
@ -83,7 +79,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 {
|
||||||
LogError(fmt.Sprintf(err))
|
LOGGER.Error(fmt.Sprintf(err))
|
||||||
} else {
|
} else {
|
||||||
this.BeanMap[typeName] = element
|
this.BeanMap[typeName] = element
|
||||||
|
|
||||||
@ -170,5 +166,4 @@ func (this *Context) initBeans() {
|
|||||||
//销毁的方法
|
//销毁的方法
|
||||||
func (this *Context) Destroy() {
|
func (this *Context) Destroy() {
|
||||||
this.CloseDb()
|
this.CloseDb()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,7 @@ type FootprintService struct {
|
|||||||
|
|
||||||
//初始化方法
|
//初始化方法
|
||||||
func (this *FootprintService) Init() {
|
func (this *FootprintService) Init() {
|
||||||
|
this.Bean.Init()
|
||||||
|
|
||||||
//手动装填本实例的Bean. 这里必须要用中间变量方可。
|
//手动装填本实例的Bean. 这里必须要用中间变量方可。
|
||||||
b := CONTEXT.GetBean(this.footprintDao)
|
b := CONTEXT.GetBean(this.footprintDao)
|
||||||
|
@ -149,13 +149,13 @@ func (this *ImageCacheDao) deleteFileAndDir(imageCache *ImageCache) {
|
|||||||
//删除文件
|
//删除文件
|
||||||
err := os.Remove(filePath)
|
err := os.Remove(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
LogError(fmt.Sprintf("删除磁盘上的文件%s出错,不做任何处理 %s", filePath, err.Error()))
|
this.logger.Error(fmt.Sprintf("删除磁盘上的文件%s出错,不做任何处理 %s", filePath, err.Error()))
|
||||||
}
|
}
|
||||||
|
|
||||||
//删除这一层文件夹
|
//删除这一层文件夹
|
||||||
err = os.Remove(dirPath)
|
err = os.Remove(dirPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
LogError(fmt.Sprintf("删除磁盘上的文件夹%s出错,不做任何处理 %s", dirPath, err.Error()))
|
this.logger.Error(fmt.Sprintf("删除磁盘上的文件夹%s出错,不做任何处理 %s", dirPath, err.Error()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,6 +19,7 @@ type ImageCacheService struct {
|
|||||||
|
|
||||||
//初始化方法
|
//初始化方法
|
||||||
func (this *ImageCacheService) Init() {
|
func (this *ImageCacheService) Init() {
|
||||||
|
this.Bean.Init()
|
||||||
|
|
||||||
//手动装填本实例的Bean. 这里必须要用中间变量方可。
|
//手动装填本实例的Bean. 这里必须要用中间变量方可。
|
||||||
b := CONTEXT.GetBean(this.imageCacheDao)
|
b := CONTEXT.GetBean(this.imageCacheDao)
|
||||||
|
@ -13,7 +13,7 @@ func InstallDatabase() {
|
|||||||
|
|
||||||
db, err := gorm.Open("mysql", CONFIG.MysqlUrl)
|
db, err := gorm.Open("mysql", CONFIG.MysqlUrl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
LogPanic(fmt.Sprintf("无法打开%s", CONFIG.MysqlUrl))
|
LOGGER.Panic(fmt.Sprintf("无法打开%s", CONFIG.MysqlUrl))
|
||||||
}
|
}
|
||||||
if db != nil {
|
if db != nil {
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
@ -28,9 +28,9 @@ func InstallDatabase() {
|
|||||||
createDownloadToken := "CREATE TABLE `tank20_download_token` (`uuid` char(36) NOT NULL,`user_uuid` char(36) DEFAULT NULL COMMENT '用户uuid',`matter_uuid` char(36) DEFAULT NULL COMMENT '文件标识',`expire_time` timestamp NULL DEFAULT NULL COMMENT '授权访问的次数',`ip` varchar(45) DEFAULT NULL COMMENT '消费者的ip',`sort` bigint(20) DEFAULT NULL,`modify_time` timestamp NULL DEFAULT NULL,`create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',PRIMARY KEY (`uuid`),UNIQUE KEY `id_UNIQUE` (`uuid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='下载的token表';"
|
createDownloadToken := "CREATE TABLE `tank20_download_token` (`uuid` char(36) NOT NULL,`user_uuid` char(36) DEFAULT NULL COMMENT '用户uuid',`matter_uuid` char(36) DEFAULT NULL COMMENT '文件标识',`expire_time` timestamp NULL DEFAULT NULL COMMENT '授权访问的次数',`ip` varchar(45) DEFAULT NULL COMMENT '消费者的ip',`sort` bigint(20) DEFAULT NULL,`modify_time` timestamp NULL DEFAULT NULL,`create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',PRIMARY KEY (`uuid`),UNIQUE KEY `id_UNIQUE` (`uuid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='下载的token表';"
|
||||||
db = db.Exec(createDownloadToken)
|
db = db.Exec(createDownloadToken)
|
||||||
if db.Error != nil {
|
if db.Error != nil {
|
||||||
LogPanic(db.Error)
|
LOGGER.Panic(db.Error.Error())
|
||||||
}
|
}
|
||||||
LogInfo("创建DownloadToken表")
|
LOGGER.Info("创建DownloadToken表")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,9 +40,9 @@ func InstallDatabase() {
|
|||||||
createMatter := "CREATE TABLE `tank20_matter` (`uuid` char(36) NOT NULL,`puuid` varchar(45) DEFAULT NULL COMMENT '上一级的uuid',`user_uuid` char(36) DEFAULT NULL COMMENT '上传的用户id',`dir` tinyint(1) DEFAULT '0' COMMENT '是否是文件夹',`alien` tinyint(1) DEFAULT '0',`name` varchar(255) DEFAULT NULL COMMENT '文件名称',`md5` varchar(45) DEFAULT NULL COMMENT '文件的md5值',`size` bigint(20) DEFAULT '0' COMMENT '文件大小',`privacy` tinyint(1) DEFAULT '0' COMMENT '文件是否是公有的',`path` varchar(255) DEFAULT NULL,`sort` bigint(20) DEFAULT NULL,`modify_time` timestamp NULL DEFAULT NULL,`create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',PRIMARY KEY (`uuid`),UNIQUE KEY `id_UNIQUE` (`uuid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='file表';"
|
createMatter := "CREATE TABLE `tank20_matter` (`uuid` char(36) NOT NULL,`puuid` varchar(45) DEFAULT NULL COMMENT '上一级的uuid',`user_uuid` char(36) DEFAULT NULL COMMENT '上传的用户id',`dir` tinyint(1) DEFAULT '0' COMMENT '是否是文件夹',`alien` tinyint(1) DEFAULT '0',`name` varchar(255) DEFAULT NULL COMMENT '文件名称',`md5` varchar(45) DEFAULT NULL COMMENT '文件的md5值',`size` bigint(20) DEFAULT '0' COMMENT '文件大小',`privacy` tinyint(1) DEFAULT '0' COMMENT '文件是否是公有的',`path` varchar(255) DEFAULT NULL,`sort` bigint(20) DEFAULT NULL,`modify_time` timestamp NULL DEFAULT NULL,`create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',PRIMARY KEY (`uuid`),UNIQUE KEY `id_UNIQUE` (`uuid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='file表';"
|
||||||
db = db.Exec(createMatter)
|
db = db.Exec(createMatter)
|
||||||
if db.Error != nil {
|
if db.Error != nil {
|
||||||
LogPanic(db.Error)
|
LOGGER.Panic(db.Error.Error())
|
||||||
}
|
}
|
||||||
LogInfo("创建Matter表")
|
LOGGER.Info("创建Matter表")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,9 +52,9 @@ func InstallDatabase() {
|
|||||||
createPreference := "CREATE TABLE `tank20_preference` (`uuid` char(36) NOT NULL,`name` varchar(45) DEFAULT NULL COMMENT '网站名称',`logo_url` varchar(255) DEFAULT NULL,`favicon_url` varchar(255) DEFAULT NULL,`footer_line1` varchar(1024) DEFAULT NULL,`footer_line2` varchar(1024) DEFAULT NULL,`version` varchar(45) DEFAULT NULL,`sort` bigint(20) DEFAULT NULL,`modify_time` timestamp NULL DEFAULT NULL,`create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',PRIMARY KEY (`uuid`),UNIQUE KEY `id_UNIQUE` (`uuid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='网站偏好设置表';"
|
createPreference := "CREATE TABLE `tank20_preference` (`uuid` char(36) NOT NULL,`name` varchar(45) DEFAULT NULL COMMENT '网站名称',`logo_url` varchar(255) DEFAULT NULL,`favicon_url` varchar(255) DEFAULT NULL,`footer_line1` varchar(1024) DEFAULT NULL,`footer_line2` varchar(1024) DEFAULT NULL,`version` varchar(45) DEFAULT NULL,`sort` bigint(20) DEFAULT NULL,`modify_time` timestamp NULL DEFAULT NULL,`create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',PRIMARY KEY (`uuid`),UNIQUE KEY `id_UNIQUE` (`uuid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='网站偏好设置表';"
|
||||||
db = db.Exec(createPreference)
|
db = db.Exec(createPreference)
|
||||||
if db.Error != nil {
|
if db.Error != nil {
|
||||||
LogPanic(db.Error)
|
LOGGER.Panic(db.Error.Error())
|
||||||
}
|
}
|
||||||
LogInfo("创建Preference表")
|
LOGGER.Info("创建Preference表")
|
||||||
}
|
}
|
||||||
|
|
||||||
session := &Session{}
|
session := &Session{}
|
||||||
@ -64,9 +64,9 @@ func InstallDatabase() {
|
|||||||
createSession := "CREATE TABLE `tank20_session` (`uuid` char(36) NOT NULL,`authentication` char(36) DEFAULT NULL COMMENT '认证身份,存放在cookie中',`user_uuid` char(36) DEFAULT NULL COMMENT '用户uuid',`ip` varchar(45) DEFAULT NULL COMMENT '用户的ip地址',`expire_time` timestamp NULL DEFAULT NULL,`sort` bigint(20) DEFAULT NULL,`modify_time` timestamp NULL DEFAULT NULL,`create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',PRIMARY KEY (`uuid`),UNIQUE KEY `id_UNIQUE` (`uuid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='session表';"
|
createSession := "CREATE TABLE `tank20_session` (`uuid` char(36) NOT NULL,`authentication` char(36) DEFAULT NULL COMMENT '认证身份,存放在cookie中',`user_uuid` char(36) DEFAULT NULL COMMENT '用户uuid',`ip` varchar(45) DEFAULT NULL COMMENT '用户的ip地址',`expire_time` timestamp NULL DEFAULT NULL,`sort` bigint(20) DEFAULT NULL,`modify_time` timestamp NULL DEFAULT NULL,`create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',PRIMARY KEY (`uuid`),UNIQUE KEY `id_UNIQUE` (`uuid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='session表';"
|
||||||
db = db.Exec(createSession)
|
db = db.Exec(createSession)
|
||||||
if db.Error != nil {
|
if db.Error != nil {
|
||||||
LogPanic(db.Error)
|
LOGGER.Panic(db.Error.Error())
|
||||||
}
|
}
|
||||||
LogInfo("创建Session表")
|
LOGGER.Info("创建Session表")
|
||||||
}
|
}
|
||||||
|
|
||||||
uploadToken := &UploadToken{}
|
uploadToken := &UploadToken{}
|
||||||
@ -76,9 +76,9 @@ func InstallDatabase() {
|
|||||||
createUploadToken := "CREATE TABLE `tank20_upload_token` (`uuid` char(36) NOT NULL,`user_uuid` char(36) DEFAULT NULL COMMENT '用户uuid',`folder_uuid` char(36) DEFAULT NULL,`matter_uuid` char(36) DEFAULT NULL,`filename` varchar(255) DEFAULT NULL COMMENT '文件后缀名的过滤,可以只允许用户上传特定格式的文件。',`privacy` tinyint(1) DEFAULT '1',`size` bigint(20) DEFAULT '0',`expire_time` timestamp NULL DEFAULT NULL,`ip` varchar(45) DEFAULT NULL COMMENT '消费者的ip',`sort` bigint(20) DEFAULT NULL,`modify_time` timestamp NULL DEFAULT NULL,`create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',PRIMARY KEY (`uuid`),UNIQUE KEY `id_UNIQUE` (`uuid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='上传的token表';"
|
createUploadToken := "CREATE TABLE `tank20_upload_token` (`uuid` char(36) NOT NULL,`user_uuid` char(36) DEFAULT NULL COMMENT '用户uuid',`folder_uuid` char(36) DEFAULT NULL,`matter_uuid` char(36) DEFAULT NULL,`filename` varchar(255) DEFAULT NULL COMMENT '文件后缀名的过滤,可以只允许用户上传特定格式的文件。',`privacy` tinyint(1) DEFAULT '1',`size` bigint(20) DEFAULT '0',`expire_time` timestamp NULL DEFAULT NULL,`ip` varchar(45) DEFAULT NULL COMMENT '消费者的ip',`sort` bigint(20) DEFAULT NULL,`modify_time` timestamp NULL DEFAULT NULL,`create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',PRIMARY KEY (`uuid`),UNIQUE KEY `id_UNIQUE` (`uuid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='上传的token表';"
|
||||||
db = db.Exec(createUploadToken)
|
db = db.Exec(createUploadToken)
|
||||||
if db.Error != nil {
|
if db.Error != nil {
|
||||||
LogPanic(db.Error)
|
LOGGER.Panic(db.Error.Error())
|
||||||
}
|
}
|
||||||
LogInfo("创建UploadToken表")
|
LOGGER.Info("创建UploadToken表")
|
||||||
}
|
}
|
||||||
|
|
||||||
user := &User{}
|
user := &User{}
|
||||||
@ -87,23 +87,23 @@ func InstallDatabase() {
|
|||||||
|
|
||||||
//验证超级管理员的信息
|
//验证超级管理员的信息
|
||||||
if m, _ := regexp.MatchString(`^[0-9a-zA-Z_]+$`, CONFIG.AdminUsername); !m {
|
if m, _ := regexp.MatchString(`^[0-9a-zA-Z_]+$`, CONFIG.AdminUsername); !m {
|
||||||
LogPanic(`超级管理员用户名必填,且只能包含字母,数字和'_''`)
|
LOGGER.Panic(`超级管理员用户名必填,且只能包含字母,数字和'_''`)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(CONFIG.AdminPassword) < 6 {
|
if len(CONFIG.AdminPassword) < 6 {
|
||||||
LogPanic(`超级管理员密码长度至少为6位`)
|
LOGGER.Panic(`超级管理员密码长度至少为6位`)
|
||||||
}
|
}
|
||||||
|
|
||||||
if CONFIG.AdminEmail == "" {
|
if CONFIG.AdminEmail == "" {
|
||||||
LogPanic("超级管理员邮箱必填!")
|
LOGGER.Panic("超级管理员邮箱必填!")
|
||||||
}
|
}
|
||||||
|
|
||||||
createUser := "CREATE TABLE `tank20_user` (`uuid` char(36) NOT NULL,`role` varchar(45) DEFAULT 'USER',`username` varchar(255) DEFAULT NULL COMMENT '昵称',`password` varchar(255) DEFAULT NULL COMMENT '密码',`email` varchar(45) DEFAULT NULL COMMENT '邮箱',`phone` varchar(45) DEFAULT NULL COMMENT '电话',`gender` varchar(45) DEFAULT 'UNKNOWN' COMMENT '性别,默认未知',`city` varchar(45) DEFAULT NULL COMMENT '城市',`avatar_url` varchar(255) DEFAULT NULL COMMENT '头像链接',`last_time` datetime DEFAULT NULL COMMENT '上次登录使劲按',`last_ip` varchar(45) DEFAULT NULL,`size_limit` int(11) DEFAULT '-1' COMMENT '该账号上传文件的大小限制,单位byte。<0 表示不设限制',`status` varchar(45) DEFAULT 'OK',`sort` bigint(20) DEFAULT NULL,`modify_time` timestamp NULL DEFAULT NULL,`create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',PRIMARY KEY (`uuid`),UNIQUE KEY `id_UNIQUE` (`uuid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户表描述';"
|
createUser := "CREATE TABLE `tank20_user` (`uuid` char(36) NOT NULL,`role` varchar(45) DEFAULT 'USER',`username` varchar(255) DEFAULT NULL COMMENT '昵称',`password` varchar(255) DEFAULT NULL COMMENT '密码',`email` varchar(45) DEFAULT NULL COMMENT '邮箱',`phone` varchar(45) DEFAULT NULL COMMENT '电话',`gender` varchar(45) DEFAULT 'UNKNOWN' COMMENT '性别,默认未知',`city` varchar(45) DEFAULT NULL COMMENT '城市',`avatar_url` varchar(255) DEFAULT NULL COMMENT '头像链接',`last_time` datetime DEFAULT NULL COMMENT '上次登录使劲按',`last_ip` varchar(45) DEFAULT NULL,`size_limit` int(11) DEFAULT '-1' COMMENT '该账号上传文件的大小限制,单位byte。<0 表示不设限制',`status` varchar(45) DEFAULT 'OK',`sort` bigint(20) DEFAULT NULL,`modify_time` timestamp NULL DEFAULT NULL,`create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',PRIMARY KEY (`uuid`),UNIQUE KEY `id_UNIQUE` (`uuid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户表描述';"
|
||||||
db = db.Exec(createUser)
|
db = db.Exec(createUser)
|
||||||
if db.Error != nil {
|
if db.Error != nil {
|
||||||
LogPanic(db.Error)
|
LOGGER.Panic(db.Error.Error())
|
||||||
}
|
}
|
||||||
LogInfo("创建User表")
|
LOGGER.Info("创建User表")
|
||||||
|
|
||||||
user := &User{}
|
user := &User{}
|
||||||
timeUUID, _ := uuid.NewV4()
|
timeUUID, _ := uuid.NewV4()
|
||||||
|
73
rest/logger.go
Normal file
73
rest/logger.go
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
package rest
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
//日志系统必须高保
|
||||||
|
//全局唯一的日志对象(在main函数中初始化)
|
||||||
|
var LOGGER *Logger = &Logger{}
|
||||||
|
|
||||||
|
//在Logger的基础上包装一个全新的Logger.
|
||||||
|
type Logger struct {
|
||||||
|
//继承logger
|
||||||
|
goLogger *log.Logger
|
||||||
|
//日志记录所在的文件
|
||||||
|
file *os.File
|
||||||
|
}
|
||||||
|
|
||||||
|
//处理日志的统一方法。
|
||||||
|
func (this *Logger) log(prefix string, format string, v ...interface{}) {
|
||||||
|
fmt.Printf(format+"\r\n", v...)
|
||||||
|
|
||||||
|
this.goLogger.SetPrefix(prefix)
|
||||||
|
this.goLogger.Printf(format, v...)
|
||||||
|
}
|
||||||
|
|
||||||
|
//处理日志的统一方法。
|
||||||
|
func (this *Logger) Debug(format string, v ...interface{}) {
|
||||||
|
this.log("[debug]", format, v...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Logger) Info(format string, v ...interface{}) {
|
||||||
|
this.log("[info]", format, v...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Logger) Warn(format string, v ...interface{}) {
|
||||||
|
this.log("[warn]", format, v...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Logger) Error(format string, v ...interface{}) {
|
||||||
|
this.log("[error]", format, v...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Logger) Panic(format string, v ...interface{}) {
|
||||||
|
this.log("[panic]", format, v...)
|
||||||
|
panic(fmt.Sprintf(format, v...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Logger) Init() {
|
||||||
|
|
||||||
|
//日志输出到文件中 文件打开后暂时不关闭
|
||||||
|
filePath := GetLogPath() + "/tank.log"
|
||||||
|
fmt.Printf("使用日志文件 %s\r\n", filePath)
|
||||||
|
f, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
||||||
|
if err != nil {
|
||||||
|
panic("日志文件无法正常打开: " + err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
this.goLogger = log.New(f, "", log.Ltime)
|
||||||
|
this.file = f
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Logger) Destroy() {
|
||||||
|
if this.file != nil {
|
||||||
|
err := this.file.Close()
|
||||||
|
if err != nil {
|
||||||
|
panic("尝试关闭日志时出错: " + err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -247,13 +247,13 @@ func (this *MatterDao) Delete(matter *Matter) {
|
|||||||
//删除文件
|
//删除文件
|
||||||
err := os.Remove(filePath)
|
err := os.Remove(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
LogError(fmt.Sprintf("删除磁盘上的文件出错,不做任何处理 %s", err.Error()))
|
this.logger.Error(fmt.Sprintf("删除磁盘上的文件出错,不做任何处理 %s", err.Error()))
|
||||||
}
|
}
|
||||||
|
|
||||||
//删除这一层文件夹
|
//删除这一层文件夹
|
||||||
err = os.Remove(dirPath)
|
err = os.Remove(dirPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
LogError(fmt.Sprintf("删除磁盘上的文件夹出错,不做任何处理 %s", err.Error()))
|
this.logger.Error(fmt.Sprintf("删除磁盘上的文件夹出错,不做任何处理 %s", err.Error()))
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -24,6 +24,7 @@ type MatterService struct {
|
|||||||
|
|
||||||
//初始化方法
|
//初始化方法
|
||||||
func (this *MatterService) Init() {
|
func (this *MatterService) Init() {
|
||||||
|
this.Bean.Init()
|
||||||
|
|
||||||
//手动装填本实例的Bean. 这里必须要用中间变量方可。
|
//手动装填本实例的Bean. 这里必须要用中间变量方可。
|
||||||
b := CONTEXT.GetBean(this.matterDao)
|
b := CONTEXT.GetBean(this.matterDao)
|
||||||
|
@ -8,6 +8,7 @@ type PreferenceService struct {
|
|||||||
|
|
||||||
//初始化方法
|
//初始化方法
|
||||||
func (this *PreferenceService) Init() {
|
func (this *PreferenceService) Init() {
|
||||||
|
this.Bean.Init()
|
||||||
|
|
||||||
//手动装填本实例的Bean. 这里必须要用中间变量方可。
|
//手动装填本实例的Bean. 这里必须要用中间变量方可。
|
||||||
b := CONTEXT.GetBean(this.preferenceDao)
|
b := CONTEXT.GetBean(this.preferenceDao)
|
||||||
|
@ -49,7 +49,7 @@ func NewRouter() *Router {
|
|||||||
func (this *Router) GlobalPanicHandler(writer http.ResponseWriter, request *http.Request) {
|
func (this *Router) GlobalPanicHandler(writer http.ResponseWriter, request *http.Request) {
|
||||||
if err := recover(); err != nil {
|
if err := recover(); err != nil {
|
||||||
|
|
||||||
LogError(fmt.Sprintf("全局异常: %v", err))
|
LOGGER.Error(fmt.Sprintf("全局异常: %v", err))
|
||||||
|
|
||||||
var webResult *WebResult = nil
|
var webResult *WebResult = nil
|
||||||
if value, ok := err.(string); ok {
|
if value, ok := err.(string); ok {
|
||||||
|
@ -215,7 +215,7 @@ func (this *UserController) Logout(writer http.ResponseWriter, request *http.Req
|
|||||||
//session置为过期
|
//session置为过期
|
||||||
sessionCookie, err := request.Cookie(COOKIE_AUTH_KEY)
|
sessionCookie, err := request.Cookie(COOKIE_AUTH_KEY)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
LogError("找不到任何登录信息")
|
this.logger.Error("找不到任何登录信息")
|
||||||
return this.Success("已经退出登录了!")
|
return this.Success("已经退出登录了!")
|
||||||
}
|
}
|
||||||
sessionId := sessionCookie.Value
|
sessionId := sessionCookie.Value
|
||||||
@ -230,7 +230,7 @@ func (this *UserController) Logout(writer http.ResponseWriter, request *http.Req
|
|||||||
//删掉session缓存
|
//删掉session缓存
|
||||||
_, err = CONTEXT.SessionCache.Delete(sessionId)
|
_, err = CONTEXT.SessionCache.Delete(sessionId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
LogError("删除用户session缓存时出错")
|
this.logger.Error("删除用户session缓存时出错")
|
||||||
}
|
}
|
||||||
|
|
||||||
//清空客户端的cookie.
|
//清空客户端的cookie.
|
||||||
|
@ -14,6 +14,7 @@ type UserService struct {
|
|||||||
|
|
||||||
//初始化方法
|
//初始化方法
|
||||||
func (this *UserService) Init() {
|
func (this *UserService) Init() {
|
||||||
|
this.Bean.Init()
|
||||||
|
|
||||||
//手动装填本实例的Bean. 这里必须要用中间变量方可。
|
//手动装填本实例的Bean. 这里必须要用中间变量方可。
|
||||||
b := CONTEXT.GetBean(this.userDao)
|
b := CONTEXT.GetBean(this.userDao)
|
||||||
@ -37,18 +38,18 @@ func (this *UserService) bootstrap(writer http.ResponseWriter, request *http.Req
|
|||||||
//验证用户是否已经登录。
|
//验证用户是否已经登录。
|
||||||
sessionCookie, err := request.Cookie(COOKIE_AUTH_KEY)
|
sessionCookie, err := request.Cookie(COOKIE_AUTH_KEY)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
LogError("找不到任何登录信息")
|
this.logger.Error("找不到任何登录信息")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
sessionId := sessionCookie.Value
|
sessionId := sessionCookie.Value
|
||||||
|
|
||||||
LogInfo("请求的sessionId = " + sessionId)
|
this.logger.Info("请求的sessionId = " + sessionId)
|
||||||
|
|
||||||
//去缓存中捞取
|
//去缓存中捞取
|
||||||
cacheItem, err := CONTEXT.SessionCache.Value(sessionId)
|
cacheItem, err := CONTEXT.SessionCache.Value(sessionId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
LogError("获取缓存时出错了" + err.Error())
|
this.logger.Error("获取缓存时出错了" + err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
//缓存中没有,尝试去数据库捞取
|
//缓存中没有,尝试去数据库捞取
|
||||||
@ -57,7 +58,7 @@ func (this *UserService) bootstrap(writer http.ResponseWriter, request *http.Req
|
|||||||
if session != nil {
|
if session != nil {
|
||||||
duration := session.ExpireTime.Sub(time.Now())
|
duration := session.ExpireTime.Sub(time.Now())
|
||||||
if duration <= 0 {
|
if duration <= 0 {
|
||||||
LogError("登录信息已过期")
|
this.logger.Error("登录信息已过期")
|
||||||
} else {
|
} else {
|
||||||
user := this.userDao.FindByUuid(session.UserUuid)
|
user := this.userDao.FindByUuid(session.UserUuid)
|
||||||
if user != nil {
|
if user != nil {
|
||||||
@ -65,7 +66,7 @@ func (this *UserService) bootstrap(writer http.ResponseWriter, request *http.Req
|
|||||||
CONTEXT.SessionCache.Add(sessionCookie.Value, duration, user)
|
CONTEXT.SessionCache.Add(sessionCookie.Value, duration, user)
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
LogError("没有找到对应的user " + session.UserUuid)
|
this.logger.Error("没有找到对应的user " + session.UserUuid)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,50 +0,0 @@
|
|||||||
package rest
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func Log(prefix string, content string) {
|
|
||||||
|
|
||||||
//日志输出到文件中
|
|
||||||
filePath := CONFIG.LogPath + "/tank-" + time.Now().Local().Format("2006-01-02") + ".log"
|
|
||||||
f, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Errorf("error opening file: %v", err)
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
log.SetOutput(f)
|
|
||||||
log.SetPrefix(prefix)
|
|
||||||
log.Println(content)
|
|
||||||
|
|
||||||
//如果需要输出到控制台。
|
|
||||||
if CONFIG.LogToConsole {
|
|
||||||
fmt.Println(content)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func LogDebug(content string) {
|
|
||||||
go Log("[Debug]", content)
|
|
||||||
}
|
|
||||||
|
|
||||||
func LogInfo(content string) {
|
|
||||||
go Log("[Info]", content)
|
|
||||||
}
|
|
||||||
|
|
||||||
func LogWarning(content string) {
|
|
||||||
go Log("[Warning]", content)
|
|
||||||
}
|
|
||||||
|
|
||||||
func LogError(content string) {
|
|
||||||
go Log("[Error]", content)
|
|
||||||
}
|
|
||||||
|
|
||||||
func LogPanic(content interface{}) {
|
|
||||||
Log("[Panic]", fmt.Sprintf("%v", content))
|
|
||||||
panic(content)
|
|
||||||
}
|
|
@ -87,6 +87,26 @@ func GetConfPath() string {
|
|||||||
return filePath
|
return filePath
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//获取日志的路径
|
||||||
|
//例如:默认存放于 home/log
|
||||||
|
func GetLogPath() string {
|
||||||
|
|
||||||
|
homePath := GetHomePath()
|
||||||
|
filePath := homePath + "/log"
|
||||||
|
exists, err := PathExists(filePath)
|
||||||
|
if err != nil {
|
||||||
|
panic("判断日志文件夹是否存在时出错!")
|
||||||
|
}
|
||||||
|
if !exists {
|
||||||
|
err = os.MkdirAll(filePath, 0777)
|
||||||
|
if err != nil {
|
||||||
|
panic("创建日志文件夹时出错!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return filePath
|
||||||
|
}
|
||||||
|
|
||||||
//获取某个用户文件应该存放的位置。这个是相对GetFilePath的路径
|
//获取某个用户文件应该存放的位置。这个是相对GetFilePath的路径
|
||||||
//例如:/zicla/2006-01-02/1510122428000
|
//例如:/zicla/2006-01-02/1510122428000
|
||||||
func GetUserFilePath(username string, cache bool) (string, string) {
|
func GetUserFilePath(username string, cache bool) (string, string) {
|
||||||
|
Loading…
Reference in New Issue
Block a user