Finish the Abstract of Config.
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"github.com/eyebluecn/tank/code/config"
|
||||
"github.com/eyebluecn/tank/code/core"
|
||||
"math"
|
||||
"reflect"
|
||||
"time"
|
||||
@ -40,7 +40,7 @@ func (this *Base) Map() map[string]interface{} {
|
||||
}
|
||||
|
||||
func (this *Base) TableName() string {
|
||||
return config.TABLE_PREFIX + "base"
|
||||
return core.TABLE_PREFIX + "base"
|
||||
}
|
||||
|
||||
//分页类
|
||||
|
@ -1,7 +1,6 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"github.com/eyebluecn/tank/code/config"
|
||||
"github.com/eyebluecn/tank/code/core"
|
||||
"github.com/eyebluecn/tank/code/tool/result"
|
||||
"github.com/eyebluecn/tank/code/tool/util"
|
||||
@ -35,7 +34,7 @@ func (this *Bean) findUser(writer http.ResponseWriter, request *http.Request) *U
|
||||
|
||||
//验证用户是否已经登录。
|
||||
//登录身份有效期以数据库中记录的为准
|
||||
sessionId := util.GetSessionUuidFromRequest(request, config.COOKIE_AUTH_KEY)
|
||||
sessionId := util.GetSessionUuidFromRequest(request, core.COOKIE_AUTH_KEY)
|
||||
if sessionId == "" {
|
||||
return nil
|
||||
}
|
||||
|
@ -1,220 +0,0 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/eyebluecn/tank/code/config"
|
||||
"github.com/eyebluecn/tank/code/core"
|
||||
"github.com/eyebluecn/tank/code/tool/cache"
|
||||
"github.com/jinzhu/gorm"
|
||||
"net/http"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
//上下文,管理数据库连接,管理所有路由请求,管理所有的单例component.
|
||||
type Context struct {
|
||||
//数据库连接
|
||||
db *gorm.DB
|
||||
//session缓存
|
||||
SessionCache *cache.Table
|
||||
//各类的Bean Map。这里面是包含ControllerMap中所有元素
|
||||
BeanMap map[string]core.IBean
|
||||
//只包含了Controller的map
|
||||
ControllerMap map[string]core.IController
|
||||
//处理所有路由请求
|
||||
Router *Router
|
||||
}
|
||||
|
||||
//初始化上下文
|
||||
func (this *Context) Init() {
|
||||
|
||||
//创建一个用于存储session的缓存。
|
||||
this.SessionCache = cache.NewTable()
|
||||
|
||||
//初始化Map
|
||||
this.BeanMap = make(map[string]core.IBean)
|
||||
this.ControllerMap = make(map[string]core.IController)
|
||||
|
||||
//注册各类Beans.在这个方法里面顺便把Controller装入ControllerMap中去。
|
||||
this.registerBeans()
|
||||
|
||||
//初始化每个bean.
|
||||
this.initBeans()
|
||||
|
||||
//初始化Router. 这个方法要在Bean注册好了之后才能。
|
||||
this.Router = NewRouter()
|
||||
|
||||
//如果数据库信息配置好了,就直接打开数据库连接 同时执行Bean的ConfigPost方法
|
||||
this.InstallOk()
|
||||
|
||||
}
|
||||
|
||||
//获取数据库对象
|
||||
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
|
||||
this.db, err = gorm.Open("mysql", config.CONFIG.MysqlUrl)
|
||||
|
||||
if err != nil {
|
||||
core.LOGGER.Panic("failed to connect mysql database")
|
||||
}
|
||||
|
||||
//是否打开sql日志(在调试阶段可以打开,以方便查看执行的SQL)
|
||||
this.db.LogMode(false)
|
||||
}
|
||||
|
||||
func (this *Context) CloseDb() {
|
||||
|
||||
if this.db != nil {
|
||||
err := this.db.Close()
|
||||
if err != nil {
|
||||
core.LOGGER.Error("关闭数据库连接出错 %s", err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//注册一个Bean
|
||||
func (this *Context) registerBean(bean core.IBean) {
|
||||
|
||||
typeOf := reflect.TypeOf(bean)
|
||||
typeName := typeOf.String()
|
||||
|
||||
if element, ok := bean.(core.IBean); ok {
|
||||
|
||||
err := fmt.Sprintf("【%s】已经被注册了,跳过。", typeName)
|
||||
if _, ok := this.BeanMap[typeName]; ok {
|
||||
core.LOGGER.Error(fmt.Sprintf(err))
|
||||
} else {
|
||||
this.BeanMap[typeName] = element
|
||||
|
||||
//看看是不是controller类型,如果是,那么单独放在ControllerMap中。
|
||||
if controller, ok1 := bean.(core.IController); ok1 {
|
||||
this.ControllerMap[typeName] = controller
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
core.LOGGER.Panic("注册的【%s】不是Bean类型。", typeName)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//注册各个Beans
|
||||
func (this *Context) registerBeans() {
|
||||
|
||||
//alien
|
||||
this.registerBean(new(AlienController))
|
||||
this.registerBean(new(AlienService))
|
||||
|
||||
//dashboard
|
||||
this.registerBean(new(DashboardController))
|
||||
this.registerBean(new(DashboardDao))
|
||||
this.registerBean(new(DashboardService))
|
||||
|
||||
//downloadToken
|
||||
this.registerBean(new(DownloadTokenDao))
|
||||
|
||||
//imageCache
|
||||
this.registerBean(new(ImageCacheController))
|
||||
this.registerBean(new(ImageCacheDao))
|
||||
this.registerBean(new(ImageCacheService))
|
||||
|
||||
//install
|
||||
this.registerBean(new(InstallController))
|
||||
|
||||
//matter
|
||||
this.registerBean(new(MatterController))
|
||||
this.registerBean(new(MatterDao))
|
||||
this.registerBean(new(MatterService))
|
||||
|
||||
//preference
|
||||
this.registerBean(new(PreferenceController))
|
||||
this.registerBean(new(PreferenceDao))
|
||||
this.registerBean(new(PreferenceService))
|
||||
|
||||
//footprint
|
||||
this.registerBean(new(FootprintController))
|
||||
this.registerBean(new(FootprintDao))
|
||||
this.registerBean(new(FootprintService))
|
||||
|
||||
//session
|
||||
this.registerBean(new(SessionDao))
|
||||
this.registerBean(new(SessionService))
|
||||
|
||||
//uploadToken
|
||||
this.registerBean(new(UploadTokenDao))
|
||||
|
||||
//user
|
||||
this.registerBean(new(UserController))
|
||||
this.registerBean(new(UserDao))
|
||||
this.registerBean(new(UserService))
|
||||
|
||||
//webdav
|
||||
this.registerBean(new(DavController))
|
||||
this.registerBean(new(DavService))
|
||||
|
||||
}
|
||||
|
||||
//从Map中获取某个Bean.
|
||||
func (this *Context) GetBean(bean core.IBean) core.IBean {
|
||||
|
||||
typeOf := reflect.TypeOf(bean)
|
||||
typeName := typeOf.String()
|
||||
|
||||
if val, ok := this.BeanMap[typeName]; ok {
|
||||
return val
|
||||
} else {
|
||||
core.LOGGER.Panic("【%s】没有注册。", typeName)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
//初始化每个Bean
|
||||
func (this *Context) initBeans() {
|
||||
|
||||
for key, bean := range this.BeanMap {
|
||||
core.LOGGER.Info("init %s", key)
|
||||
bean.Init()
|
||||
}
|
||||
}
|
||||
|
||||
//系统如果安装好了就调用这个方法。
|
||||
func (this *Context) InstallOk() {
|
||||
|
||||
if config.CONFIG.Installed {
|
||||
this.OpenDb()
|
||||
|
||||
for _, bean := range this.BeanMap {
|
||||
bean.Bootstrap()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//销毁的方法
|
||||
func (this *Context) Destroy() {
|
||||
this.CloseDb()
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
package rest
|
||||
|
||||
import "github.com/eyebluecn/tank/code/config"
|
||||
import "github.com/eyebluecn/tank/code/core"
|
||||
|
||||
/**
|
||||
* 系统的所有访问记录均记录在此
|
||||
@ -21,7 +21,7 @@ type Dashboard struct {
|
||||
|
||||
// set File's table name to be `profiles`
|
||||
func (this *Dashboard) TableName() string {
|
||||
return config.TABLE_PREFIX + "dashboard"
|
||||
return core.TABLE_PREFIX + "dashboard"
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,7 +1,7 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"github.com/eyebluecn/tank/code/config"
|
||||
"github.com/eyebluecn/tank/code/core"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -14,5 +14,5 @@ type DownloadToken struct {
|
||||
}
|
||||
|
||||
func (this *DownloadToken) TableName() string {
|
||||
return config.TABLE_PREFIX + "download_token"
|
||||
return core.TABLE_PREFIX + "download_token"
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
package rest
|
||||
|
||||
import "github.com/eyebluecn/tank/code/config"
|
||||
import "github.com/eyebluecn/tank/code/core"
|
||||
|
||||
/**
|
||||
* 系统的所有访问记录均记录在此
|
||||
@ -18,5 +18,5 @@ type Footprint struct {
|
||||
|
||||
// set File's table name to be `profiles`
|
||||
func (this *Footprint) TableName() string {
|
||||
return config.TABLE_PREFIX + "footprint"
|
||||
return core.TABLE_PREFIX + "footprint"
|
||||
}
|
||||
|
@ -2,7 +2,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"
|
||||
@ -76,7 +76,7 @@ func (this *FootprintService) Trace(writer http.ResponseWriter, request *http.Re
|
||||
}
|
||||
|
||||
//有可能DB尚且没有配置 直接打印出内容,并且退出
|
||||
if config.CONFIG.Installed {
|
||||
if core.CONFIG.IsInstalled() {
|
||||
user := this.findUser(writer, request)
|
||||
userUuid := ""
|
||||
if user != nil {
|
||||
|
@ -1,7 +1,7 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"github.com/eyebluecn/tank/code/config"
|
||||
"github.com/eyebluecn/tank/code/core"
|
||||
)
|
||||
|
||||
/**
|
||||
@ -23,7 +23,7 @@ type ImageCache struct {
|
||||
|
||||
// set File's table name to be `profiles`
|
||||
func (this *ImageCache) TableName() string {
|
||||
return config.TABLE_PREFIX + "image_cache"
|
||||
return core.TABLE_PREFIX + "image_cache"
|
||||
}
|
||||
|
||||
// 获取该ImageCache的绝对路径。path代表的是相对路径。
|
||||
|
@ -2,18 +2,16 @@ 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"
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/json-iterator/go"
|
||||
"github.com/nu7hatch/gouuid"
|
||||
"go/build"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"time"
|
||||
@ -427,38 +425,8 @@ func (this *InstallController) Finish(writer http.ResponseWriter, request *http.
|
||||
panic(result.BadRequest(`请至少配置一名管理员`))
|
||||
}
|
||||
|
||||
var configItem = &config.ConfigItem{
|
||||
//默认监听端口号
|
||||
ServerPort: config.CONFIG.ServerPort,
|
||||
//上传的文件路径,要求不以/结尾。如果没有指定,默认在根目录下的matter文件夹中。eg: /var/www/matter
|
||||
MatterPath: config.CONFIG.MatterPath,
|
||||
//mysql相关配置。
|
||||
//数据库端口
|
||||
MysqlPort: mysqlPort,
|
||||
//数据库Host
|
||||
MysqlHost: mysqlHost,
|
||||
//数据库名字
|
||||
MysqlSchema: mysqlSchema,
|
||||
//用户名
|
||||
MysqlUsername: mysqlUsername,
|
||||
//密码
|
||||
MysqlPassword: mysqlPassword,
|
||||
}
|
||||
|
||||
//用json的方式输出返回值。为了让格式更好看。
|
||||
jsonStr, _ := jsoniter.ConfigCompatibleWithStandardLibrary.MarshalIndent(configItem, "", " ")
|
||||
|
||||
//写入到配置文件中(不能使用os.O_APPEND 否则会追加)
|
||||
filePath := util.GetConfPath() + "/tank.json"
|
||||
f, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE, 0777)
|
||||
this.PanicError(err)
|
||||
_, err = f.Write(jsonStr)
|
||||
this.PanicError(err)
|
||||
err = f.Close()
|
||||
this.PanicError(err)
|
||||
|
||||
//通知配置文件安装完毕。
|
||||
config.CONFIG.InstallOk()
|
||||
core.CONFIG.FinishInstall(mysqlPort, mysqlHost, mysqlSchema, mysqlUsername, mysqlPassword)
|
||||
|
||||
//通知全局上下文,说系统安装好了
|
||||
core.CONTEXT.InstallOk()
|
||||
|
@ -1,7 +1,6 @@
|
||||
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"
|
||||
@ -367,7 +366,7 @@ func (this *MatterDao) Cleanup() {
|
||||
db := core.CONTEXT.GetDB().Where("uuid is not null").Delete(Matter{})
|
||||
this.PanicError(db.Error)
|
||||
|
||||
err := os.RemoveAll(config.CONFIG.MatterPath)
|
||||
err := os.RemoveAll(core.CONFIG.GetMatterPath())
|
||||
this.PanicError(err)
|
||||
|
||||
}
|
||||
|
@ -2,7 +2,8 @@ package rest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/eyebluecn/tank/code/config"
|
||||
"github.com/eyebluecn/tank/code/core"
|
||||
|
||||
"github.com/eyebluecn/tank/code/tool/util"
|
||||
)
|
||||
|
||||
@ -37,7 +38,7 @@ type Matter struct {
|
||||
|
||||
// set File's table name to be `profiles`
|
||||
func (Matter) TableName() string {
|
||||
return config.TABLE_PREFIX + "matter"
|
||||
return core.TABLE_PREFIX + "matter"
|
||||
}
|
||||
|
||||
// 获取该Matter的绝对路径。path代表的是相对路径。
|
||||
@ -67,7 +68,7 @@ func NewRootMatter(user *User) *Matter {
|
||||
//获取到用户文件的根目录。
|
||||
func GetUserFileRootDir(username string) (rootDirPath string) {
|
||||
|
||||
rootDirPath = fmt.Sprintf("%s/%s/%s", config.CONFIG.MatterPath, username, MATTER_ROOT)
|
||||
rootDirPath = fmt.Sprintf("%s/%s/%s", core.CONFIG.GetMatterPath(), username, MATTER_ROOT)
|
||||
|
||||
return rootDirPath
|
||||
}
|
||||
@ -75,7 +76,7 @@ func GetUserFileRootDir(username string) (rootDirPath string) {
|
||||
//获取到用户缓存的根目录。
|
||||
func GetUserCacheRootDir(username string) (rootDirPath string) {
|
||||
|
||||
rootDirPath = fmt.Sprintf("%s/%s/%s", config.CONFIG.MatterPath, username, MATTER_CACHE)
|
||||
rootDirPath = fmt.Sprintf("%s/%s/%s", core.CONFIG.GetMatterPath(), username, MATTER_CACHE)
|
||||
|
||||
return rootDirPath
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
package rest
|
||||
|
||||
import "github.com/eyebluecn/tank/code/config"
|
||||
import "github.com/eyebluecn/tank/code/core"
|
||||
|
||||
type Preference struct {
|
||||
Base
|
||||
@ -14,5 +14,5 @@ type Preference struct {
|
||||
|
||||
// set File's table name to be `profiles`
|
||||
func (this *Preference) TableName() string {
|
||||
return config.TABLE_PREFIX + "preference"
|
||||
return core.TABLE_PREFIX + "preference"
|
||||
}
|
||||
|
@ -1,210 +0,0 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/eyebluecn/tank/code/config"
|
||||
"github.com/eyebluecn/tank/code/core"
|
||||
"github.com/eyebluecn/tank/code/tool/result"
|
||||
"github.com/eyebluecn/tank/code/tool/util"
|
||||
"github.com/json-iterator/go"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
//用于处理所有前来的请求
|
||||
type Router struct {
|
||||
installController *InstallController
|
||||
footprintService *FootprintService
|
||||
userService *UserService
|
||||
routeMap map[string]func(writer http.ResponseWriter, request *http.Request)
|
||||
installRouteMap map[string]func(writer http.ResponseWriter, request *http.Request)
|
||||
}
|
||||
|
||||
//构造方法
|
||||
func NewRouter() *Router {
|
||||
router := &Router{
|
||||
routeMap: make(map[string]func(writer http.ResponseWriter, request *http.Request)),
|
||||
installRouteMap: make(map[string]func(writer http.ResponseWriter, request *http.Request)),
|
||||
}
|
||||
|
||||
//installController.
|
||||
b := core.CONTEXT.GetBean(router.installController)
|
||||
if b, ok := b.(*InstallController); ok {
|
||||
router.installController = b
|
||||
}
|
||||
|
||||
//装载userService.
|
||||
b = core.CONTEXT.GetBean(router.userService)
|
||||
if b, ok := b.(*UserService); ok {
|
||||
router.userService = b
|
||||
}
|
||||
|
||||
//装载footprintService
|
||||
b = core.CONTEXT.GetBean(router.footprintService)
|
||||
if b, ok := b.(*FootprintService); ok {
|
||||
router.footprintService = b
|
||||
}
|
||||
|
||||
//将Controller中的路由规则装载进来,InstallController中的除外
|
||||
for _, controller := range core.CONTEXT.GetControllerMap() {
|
||||
|
||||
if controller == router.installController {
|
||||
routes := controller.RegisterRoutes()
|
||||
for k, v := range routes {
|
||||
router.installRouteMap[k] = v
|
||||
}
|
||||
} else {
|
||||
routes := controller.RegisterRoutes()
|
||||
for k, v := range routes {
|
||||
router.routeMap[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return router
|
||||
|
||||
}
|
||||
|
||||
//全局的异常捕获
|
||||
func (this *Router) GlobalPanicHandler(writer http.ResponseWriter, request *http.Request, startTime time.Time) {
|
||||
if err := recover(); err != nil {
|
||||
|
||||
core.LOGGER.Error("错误: %v", err)
|
||||
|
||||
var webResult *result.WebResult = nil
|
||||
if value, ok := err.(string); ok {
|
||||
//一个字符串,默认是请求错误。
|
||||
webResult = result.CustomWebResult(result.CODE_WRAPPER_BAD_REQUEST, value)
|
||||
} else if value, ok := err.(*result.WebResult); ok {
|
||||
//一个WebResult对象
|
||||
webResult = value
|
||||
} else if value, ok := err.(*result.CodeWrapper); ok {
|
||||
//一个WebResult对象
|
||||
webResult = result.ConstWebResult(value)
|
||||
} else if value, ok := err.(error); ok {
|
||||
//一个普通的错误对象
|
||||
webResult = result.CustomWebResult(result.CODE_WRAPPER_UNKNOWN, value.Error())
|
||||
} else {
|
||||
//其他不能识别的内容
|
||||
webResult = result.ConstWebResult(result.CODE_WRAPPER_UNKNOWN)
|
||||
}
|
||||
|
||||
//修改http code码
|
||||
writer.WriteHeader(result.FetchHttpStatus(webResult.Code))
|
||||
|
||||
//输出的是json格式 返回的内容申明是json,utf-8
|
||||
writer.Header().Set("Content-Type", "application/json;charset=UTF-8")
|
||||
|
||||
//用json的方式输出返回值。
|
||||
b, _ := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(webResult)
|
||||
|
||||
//写到输出流中
|
||||
_, err := fmt.Fprintf(writer, string(b))
|
||||
if err != nil {
|
||||
fmt.Printf("输出结果时出错了\n")
|
||||
}
|
||||
|
||||
//错误情况记录。
|
||||
go util.RunWithRecovery(func() {
|
||||
this.footprintService.Trace(writer, request, time.Now().Sub(startTime), false)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
//让Router具有处理请求的功能。
|
||||
func (this *Router) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
||||
|
||||
startTime := time.Now()
|
||||
|
||||
//每个请求的入口在这里
|
||||
//全局异常处理。
|
||||
defer this.GlobalPanicHandler(writer, request, startTime)
|
||||
|
||||
path := request.URL.Path
|
||||
if strings.HasPrefix(path, "/api") {
|
||||
|
||||
//对于IE浏览器,会自动缓存,因此设置不缓存Header.
|
||||
writer.Header().Set("Pragma", "No-cache")
|
||||
writer.Header().Set("Cache-Control", "no-cache")
|
||||
writer.Header().Set("Expires", "0")
|
||||
|
||||
if config.CONFIG.Installed {
|
||||
//已安装的模式
|
||||
|
||||
//统一处理用户的身份信息。
|
||||
this.userService.preHandle(writer, request)
|
||||
|
||||
if handler, ok := this.routeMap[path]; ok {
|
||||
handler(writer, request)
|
||||
} else {
|
||||
//直接将请求扔给每个controller,看看他们能不能处理,如果都不能处理,那就抛出找不到的错误
|
||||
canHandle := false
|
||||
for _, controller := range core.CONTEXT.GetControllerMap() {
|
||||
if handler, exist := controller.HandleRoutes(writer, request); exist {
|
||||
canHandle = true
|
||||
handler(writer, request)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !canHandle {
|
||||
panic(result.CustomWebResult(result.CODE_WRAPPER_NOT_FOUND, fmt.Sprintf("没有找到能够处理%s的方法", path)))
|
||||
}
|
||||
}
|
||||
|
||||
//正常的访问记录会落到这里。
|
||||
go util.RunWithRecovery(func() {
|
||||
this.footprintService.Trace(writer, request, time.Now().Sub(startTime), true)
|
||||
})
|
||||
|
||||
} else {
|
||||
//未安装模式
|
||||
if handler, ok := this.installRouteMap[path]; ok {
|
||||
handler(writer, request)
|
||||
} else {
|
||||
panic(result.ConstWebResult(result.CODE_WRAPPER_NOT_INSTALLED))
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
//当作静态资源处理。默认从当前文件下面的static文件夹中取东西。
|
||||
dir := util.GetHtmlPath()
|
||||
|
||||
requestURI := request.RequestURI
|
||||
if requestURI == "" || request.RequestURI == "/" {
|
||||
requestURI = "index.html"
|
||||
}
|
||||
|
||||
filePath := dir + requestURI
|
||||
exists, _ := util.PathExists(filePath)
|
||||
if !exists {
|
||||
filePath = dir + "/index.html"
|
||||
exists, _ = util.PathExists(filePath)
|
||||
if !exists {
|
||||
panic(fmt.Sprintf("404 not found:%s", filePath))
|
||||
}
|
||||
}
|
||||
|
||||
writer.Header().Set("Content-Type", util.GetMimeType(util.GetExtension(filePath)))
|
||||
|
||||
diskFile, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
panic("cannot get file.")
|
||||
}
|
||||
defer func() {
|
||||
err := diskFile.Close()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}()
|
||||
_, err = io.Copy(writer, diskFile)
|
||||
if err != nil {
|
||||
panic("cannot get file.")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"github.com/eyebluecn/tank/code/config"
|
||||
"github.com/eyebluecn/tank/code/core"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -14,5 +14,5 @@ type Session struct {
|
||||
|
||||
// set User's table name to be `profiles`
|
||||
func (this *Session) TableName() string {
|
||||
return config.TABLE_PREFIX + "session"
|
||||
return core.TABLE_PREFIX + "session"
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"github.com/eyebluecn/tank/code/config"
|
||||
"github.com/eyebluecn/tank/code/core"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -18,5 +18,5 @@ type UploadToken struct {
|
||||
}
|
||||
|
||||
func (this *UploadToken) TableName() string {
|
||||
return config.TABLE_PREFIX + "upload_token"
|
||||
return core.TABLE_PREFIX + "upload_token"
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
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"
|
||||
@ -83,7 +82,7 @@ func (this *UserController) Login(writer http.ResponseWriter, request *http.Requ
|
||||
|
||||
//设置用户的cookie.
|
||||
cookie := http.Cookie{
|
||||
Name: config.COOKIE_AUTH_KEY,
|
||||
Name: core.COOKIE_AUTH_KEY,
|
||||
Path: "/",
|
||||
Value: session.Uuid,
|
||||
Expires: expiration}
|
||||
@ -218,7 +217,7 @@ func (this *UserController) Detail(writer http.ResponseWriter, request *http.Req
|
||||
func (this *UserController) Logout(writer http.ResponseWriter, request *http.Request) *result.WebResult {
|
||||
|
||||
//session置为过期
|
||||
sessionCookie, err := request.Cookie(config.COOKIE_AUTH_KEY)
|
||||
sessionCookie, err := request.Cookie(core.COOKIE_AUTH_KEY)
|
||||
if err != nil {
|
||||
return this.Success("已经退出登录了!")
|
||||
}
|
||||
@ -241,7 +240,7 @@ func (this *UserController) Logout(writer http.ResponseWriter, request *http.Req
|
||||
expiration := time.Now()
|
||||
expiration = expiration.AddDate(-1, 0, 0)
|
||||
cookie := http.Cookie{
|
||||
Name: config.COOKIE_AUTH_KEY,
|
||||
Name: core.COOKIE_AUTH_KEY,
|
||||
Path: "/",
|
||||
Value: sessionId,
|
||||
Expires: expiration}
|
||||
|
@ -1,7 +1,7 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"github.com/eyebluecn/tank/code/config"
|
||||
"github.com/eyebluecn/tank/code/core"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -45,7 +45,7 @@ type User struct {
|
||||
|
||||
// set User's table name to be `profiles`
|
||||
func (this *User) TableName() string {
|
||||
return config.TABLE_PREFIX + "user"
|
||||
return core.TABLE_PREFIX + "user"
|
||||
}
|
||||
|
||||
//通过一个字符串获取性别
|
||||
|
@ -1,7 +1,6 @@
|
||||
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"
|
||||
@ -72,12 +71,12 @@ func (this *UserService) MatterUnlock(userUuid string) {
|
||||
|
||||
//装载session信息,如果session没有了根据cookie去装填用户信息。
|
||||
//在所有的路由最初会调用这个方法
|
||||
func (this *UserService) preHandle(writer http.ResponseWriter, request *http.Request) {
|
||||
func (this *UserService) PreHandle(writer http.ResponseWriter, request *http.Request) {
|
||||
|
||||
//登录身份有效期以数据库中记录的为准
|
||||
|
||||
//验证用户是否已经登录。
|
||||
sessionCookie, err := request.Cookie(config.COOKIE_AUTH_KEY)
|
||||
sessionCookie, err := request.Cookie(core.COOKIE_AUTH_KEY)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
Reference in New Issue
Block a user