fix:修复beego使用file储存session时存在的路径BUG

pull/425/head
lifei6671 2018-11-09 18:10:34 +08:00
parent 64edcf97ce
commit 7785fb270d
4 changed files with 44 additions and 2 deletions

View File

@ -31,7 +31,7 @@
[[constraint]] [[constraint]]
name = "github.com/astaxie/beego" name = "github.com/astaxie/beego"
version = "1.10.0" version = "1.10.1"
[[constraint]] [[constraint]]
name = "github.com/boombuler/barcode" name = "github.com/boombuler/barcode"

View File

@ -12,7 +12,6 @@ import (
"time" "time"
"encoding/json" "encoding/json"
"github.com/astaxie/beego" "github.com/astaxie/beego"
beegoCache "github.com/astaxie/beego/cache" beegoCache "github.com/astaxie/beego/cache"
_ "github.com/astaxie/beego/cache/memcache" _ "github.com/astaxie/beego/cache/memcache"
@ -26,6 +25,8 @@ import (
"github.com/lifei6671/mindoc/utils/filetil" "github.com/lifei6671/mindoc/utils/filetil"
"github.com/astaxie/beego/cache/redis" "github.com/astaxie/beego/cache/redis"
"github.com/howeyc/fsnotify" "github.com/howeyc/fsnotify"
"net/http"
"bytes"
) )
// RegisterDataBase 注册数据库 // RegisterDataBase 注册数据库
@ -448,6 +449,35 @@ func RegisterAutoLoadConfig() {
} }
} }
} }
//注册错误处理方法.
func RegisterError() {
beego.ErrorHandler("404", func(writer http.ResponseWriter, request *http.Request) {
var buf bytes.Buffer
data :=make(map[string]interface{})
data["ErrorCode"] = 404
data["ErrorMessage"] = "页面未找到或已删除"
if err := beego.ExecuteViewPathTemplate(&buf,"errors/error.tpl",beego.BConfig.WebConfig.ViewsPath,data);err == nil {
fmt.Fprint(writer,buf.String())
} else {
fmt.Fprint(writer,data["ErrorMessage"])
}
})
beego.ErrorHandler("401", func(writer http.ResponseWriter, request *http.Request) {
var buf bytes.Buffer
data :=make(map[string]interface{})
data["ErrorCode"] = 401
data["ErrorMessage"] = "请与 Web 服务器的管理员联系,以确认您是否具有访问所请求资源的权限。"
if err := beego.ExecuteViewPathTemplate(&buf,"errors/error.tpl",beego.BConfig.WebConfig.ViewsPath,data);err == nil {
fmt.Fprint(writer,buf.String())
} else {
fmt.Fprint(writer,data["ErrorMessage"])
}
})
}
func init() { func init() {

View File

@ -44,12 +44,15 @@ func (d *Daemon) Start(s service.Service) error {
func (d *Daemon) Run() { func (d *Daemon) Run() {
commands.ResolveCommand(d.config.Arguments) commands.ResolveCommand(d.config.Arguments)
commands.RegisterFunction() commands.RegisterFunction()
commands.RegisterAutoLoadConfig() commands.RegisterAutoLoadConfig()
commands.RegisterError()
beego.ErrorController(&controllers.ErrorController{}) beego.ErrorController(&controllers.ErrorController{})
f,err := filepath.Abs(os.Args[0]) f,err := filepath.Abs(os.Args[0])

View File

@ -7,6 +7,7 @@ import (
"github.com/lifei6671/mindoc/conf" "github.com/lifei6671/mindoc/conf"
"github.com/lifei6671/mindoc/models" "github.com/lifei6671/mindoc/models"
"net/url" "net/url"
"regexp"
) )
func init() { func init() {
@ -42,5 +43,13 @@ func init() {
ctx.ResponseWriter.Header().Add("MinDoc-Site", "https://www.iminho.me") ctx.ResponseWriter.Header().Add("MinDoc-Site", "https://www.iminho.me")
} }
var StartRouter = func(ctx *context.Context) {
sessionId := ctx.Input.Cookie(beego.AppConfig.String("sessionname"))
//sessionId必须是数字字母组成且最小32个字符最大1024字符
if ok, err := regexp.MatchString(`^[a-zA-z0-9]{32,1024}$`, sessionId); !ok || err != nil {
panic("401")
}
}
beego.InsertFilter("/*", beego.BeforeStatic, StartRouter, false)
beego.InsertFilter("/*", beego.BeforeRouter, FinishRouter, false) beego.InsertFilter("/*", beego.BeforeRouter, FinishRouter, false)
} }