fix(all): don't use adapter in FuncMap and Filter etc.

pull/662/head
roberChen 2021-03-26 15:37:19 +08:00
parent 967ef37ce9
commit debc3aa5c9
7 changed files with 32 additions and 51 deletions

View File

@ -15,7 +15,6 @@ import (
"encoding/json"
"net/http"
"github.com/beego/beego/v2/adapter"
beegoCache "github.com/beego/beego/v2/client/cache"
_ "github.com/beego/beego/v2/client/cache/memcache"
"github.com/beego/beego/v2/client/cache/redis"
@ -163,28 +162,20 @@ func RegisterLogger(log string) {
switch level {
case "Emergency":
config["level"] = logs.LevelEmergency
break
case "Alert":
config["level"] = logs.LevelAlert
break
case "Critical":
config["level"] = logs.LevelCritical
break
case "Error":
config["level"] = logs.LevelError
break
case "Warning":
config["level"] = logs.LevelWarning
break
case "Notice":
config["level"] = logs.LevelNotice
break
case "Informational":
config["level"] = logs.LevelInformational
break
case "Debug":
config["level"] = logs.LevelDebug
break
}
}
b, err := json.Marshal(config)
@ -213,13 +204,13 @@ func RegisterCommand() {
//注册模板函数
func RegisterFunction() {
err := adapter.AddFuncMap("config", models.GetOptionValue)
err := web.AddFuncMap("config", models.GetOptionValue)
if err != nil {
logs.Error("注册函数 config 出错 ->", err)
os.Exit(-1)
}
err = adapter.AddFuncMap("cdn", func(p string) string {
err = web.AddFuncMap("cdn", func(p string) string {
cdn := web.AppConfig.DefaultString("cdn", "")
if strings.HasPrefix(p, "http://") || strings.HasPrefix(p, "https://") {
return p
@ -249,28 +240,28 @@ func RegisterFunction() {
os.Exit(-1)
}
err = adapter.AddFuncMap("cdnjs", conf.URLForWithCdnJs)
err = web.AddFuncMap("cdnjs", conf.URLForWithCdnJs)
if err != nil {
logs.Error("注册函数 cdnjs 出错 ->", err)
os.Exit(-1)
}
err = adapter.AddFuncMap("cdncss", conf.URLForWithCdnCss)
err = web.AddFuncMap("cdncss", conf.URLForWithCdnCss)
if err != nil {
logs.Error("注册函数 cdncss 出错 ->", err)
os.Exit(-1)
}
err = adapter.AddFuncMap("cdnimg", conf.URLForWithCdnImage)
err = web.AddFuncMap("cdnimg", conf.URLForWithCdnImage)
if err != nil {
logs.Error("注册函数 cdnimg 出错 ->", err)
os.Exit(-1)
}
//重写url生成支持配置域名以及域名前缀
err = adapter.AddFuncMap("urlfor", conf.URLFor)
err = web.AddFuncMap("urlfor", conf.URLFor)
if err != nil {
logs.Error("注册函数 urlfor 出错 ->", err)
os.Exit(-1)
}
err = adapter.AddFuncMap("date_format", func(t time.Time, format string) string {
err = web.AddFuncMap("date_format", func(t time.Time, format string) string {
return t.Local().Format(format)
})
if err != nil {
@ -307,7 +298,7 @@ func ResolveCommand(args []string) {
log.Fatal("读取字体文件时出错 -> ", err)
}
if err := adapter.LoadAppConfig("ini", conf.ConfigurationFile); err != nil {
if err := web.LoadAppConfig("ini", conf.ConfigurationFile); err != nil {
log.Fatal("An error occurred:", err)
}
if conf.LogFile == "" {
@ -461,7 +452,7 @@ func RegisterAutoLoadConfig() {
case ev := <-watcher.Event:
//如果是修改了配置文件
if ev.IsModify() {
if err := adapter.LoadAppConfig("ini", conf.ConfigurationFile); err != nil {
if err := web.LoadAppConfig("ini", conf.ConfigurationFile); err != nil {
logs.Error("An error occurred ->", err)
continue
}
@ -496,7 +487,7 @@ func RegisterError() {
data["ErrorCode"] = 404
data["ErrorMessage"] = "页面未找到或已删除"
if err := adapter.ExecuteViewPathTemplate(&buf, "errors/error.tpl", web.BConfig.WebConfig.ViewsPath, data); err == nil {
if err := web.ExecuteViewPathTemplate(&buf, "errors/error.tpl", web.BConfig.WebConfig.ViewsPath, data); err == nil {
_, _ = fmt.Fprint(writer, buf.String())
} else {
_, _ = fmt.Fprint(writer, data["ErrorMessage"])
@ -509,7 +500,7 @@ func RegisterError() {
data["ErrorCode"] = 401
data["ErrorMessage"] = "请与 Web 服务器的管理员联系,以确认您是否具有访问所请求资源的权限。"
if err := adapter.ExecuteViewPathTemplate(&buf, "errors/error.tpl", web.BConfig.WebConfig.ViewsPath, data); err == nil {
if err := web.ExecuteViewPathTemplate(&buf, "errors/error.tpl", web.BConfig.WebConfig.ViewsPath, data); err == nil {
_, _ = fmt.Fprint(writer, buf.String())
} else {
_, _ = fmt.Fprint(writer, data["ErrorMessage"])

View File

@ -6,7 +6,6 @@ import (
"path/filepath"
"github.com/beego/beego/v2/adapter"
"github.com/beego/beego/v2/core/logs"
"github.com/beego/beego/v2/server/web"
"github.com/kardianos/service"
@ -55,11 +54,6 @@ func (d *Daemon) Run() {
commands.RegisterError()
/* TODO: which ErrorController to use? currently there are
* two ErrorController avalable, one is beego/v2/server/web.ErrorController,
* and another is beego/v2/adapter.ErrorController, which is actually a calling to the first one
* here the first one is used, which is the original implementation.
*/
web.ErrorController(&controllers.ErrorController{})
f, err := filepath.Abs(os.Args[0])
@ -70,7 +64,7 @@ func (d *Daemon) Run() {
fmt.Printf("MinDoc version => %s\nbuild time => %s\nstart directory => %s\n%s\n", conf.VERSION, conf.BUILD_TIME, f, conf.GO_VERSION)
adapter.Run()
web.Run()
}
func (d *Daemon) Stop(s service.Service) error {

View File

@ -9,7 +9,6 @@ import (
"path/filepath"
"strconv"
"github.com/beego/beego/v2/adapter"
"github.com/beego/beego/v2/server/web"
)
@ -212,7 +211,7 @@ func IsAllowUploadFileExt(ext string) bool {
//重写生成URL的方法加上完整的域名
func URLFor(endpoint string, values ...interface{}) string {
baseUrl := web.AppConfig.DefaultString("baseurl", "")
pathUrl := adapter.URLFor(endpoint, values...)
pathUrl := web.URLFor(endpoint, values...)
if baseUrl == "" {
baseUrl = BaseUrl
@ -226,12 +225,12 @@ func URLFor(endpoint string, values ...interface{}) string {
if !strings.HasPrefix(pathUrl, "/") && !strings.HasSuffix(baseUrl, "/") {
return baseUrl + "/" + pathUrl
}
return baseUrl + adapter.URLFor(endpoint, values...)
return baseUrl + web.URLFor(endpoint, values...)
}
func URLForNotHost(endpoint string, values ...interface{}) string {
baseUrl := web.AppConfig.DefaultString("baseurl", "")
pathUrl := adapter.URLFor(endpoint, values...)
pathUrl := web.URLFor(endpoint, values...)
if baseUrl == "" {
baseUrl = "/"
@ -245,7 +244,7 @@ func URLForNotHost(endpoint string, values ...interface{}) string {
if !strings.HasPrefix(pathUrl, "/") && !strings.HasSuffix(baseUrl, "/") {
return baseUrl + "/" + pathUrl
}
return baseUrl + adapter.URLFor(endpoint, values...)
return baseUrl + web.URLFor(endpoint, values...)
}
func URLForWithCdnImage(p string) string {

View File

@ -12,7 +12,6 @@ import (
"io/ioutil"
"path/filepath"
"github.com/beego/beego/v2/adapter"
"github.com/beego/beego/v2/core/logs"
"github.com/beego/beego/v2/server/web"
"github.com/mindoc-org/mindoc/conf"
@ -160,7 +159,7 @@ func (c *BaseController) ExecuteViewPathTemplate(tplName string, data interface{
}
if err := adapter.ExecuteViewPathTemplate(&buf, tplName, viewPath, data); err != nil {
if err := web.ExecuteViewPathTemplate(&buf, tplName, viewPath, data); err != nil {
return "", err
}
return buf.String(), nil
@ -187,7 +186,7 @@ func (c *BaseController) ShowErrorPage(errCode int, errMsg string) {
var buf bytes.Buffer
if err := adapter.ExecuteViewPathTemplate(&buf, "errors/error.tpl", web.BConfig.WebConfig.ViewsPath, map[string]interface{}{"ErrorMessage": errMsg, "ErrorCode": errCode, "BaseUrl": conf.BaseUrl}); err != nil {
if err := web.ExecuteViewPathTemplate(&buf, "errors/error.tpl", web.BConfig.WebConfig.ViewsPath, map[string]interface{}{"ErrorMessage": errMsg, "ErrorCode": errCode, "BaseUrl": conf.BaseUrl}); err != nil {
c.Abort("500")
}
if errCode >= 200 && errCode <= 510 {

View File

@ -13,9 +13,9 @@ import (
"io/ioutil"
"os"
"github.com/beego/beego/v2/adapter"
"github.com/beego/beego/v2/client/orm"
"github.com/beego/beego/v2/core/logs"
"github.com/beego/beego/v2/server/web"
"github.com/mindoc-org/mindoc/conf"
"github.com/mindoc-org/mindoc/models"
"github.com/mindoc-org/mindoc/utils"
@ -751,7 +751,7 @@ func (c *ManagerController) Config() {
tf.WriteString(content)
err = adapter.LoadAppConfig("ini", tf.Name())
err = web.LoadAppConfig("ini", tf.Name())
if err != nil {
logs.Error("加载配置文件失败 ->", err)

View File

@ -14,7 +14,6 @@ import (
"regexp"
"github.com/PuerkitoBio/goquery"
"github.com/beego/beego/v2/adapter"
"github.com/beego/beego/v2/client/orm"
"github.com/beego/beego/v2/core/logs"
"github.com/beego/beego/v2/server/web"
@ -374,7 +373,7 @@ func (m *BookResult) Converter(sessionId string) (ConvertBookResult, error) {
}
var buf bytes.Buffer
if err := adapter.ExecuteViewPathTemplate(&buf, "document/export.tpl", viewPath, map[string]interface{}{"Model": m, "Lists": item, "BaseUrl": conf.BaseUrl}); err != nil {
if err := web.ExecuteViewPathTemplate(&buf, "document/export.tpl", viewPath, map[string]interface{}{"Model": m, "Lists": item, "BaseUrl": conf.BaseUrl}); err != nil {
return convertBookResult, err
}
html := buf.String()

View File

@ -5,9 +5,8 @@ import (
"net/url"
"regexp"
"github.com/beego/beego/v2/adapter"
"github.com/beego/beego/v2/adapter/context"
"github.com/beego/beego/v2/server/web"
"github.com/beego/beego/v2/server/web/context"
"github.com/mindoc-org/mindoc/conf"
"github.com/mindoc-org/mindoc/models"
)
@ -31,14 +30,14 @@ func init() {
}
}
}
adapter.InsertFilter("/manager", web.BeforeRouter, FilterUser)
adapter.InsertFilter("/manager/*", web.BeforeRouter, FilterUser)
adapter.InsertFilter("/setting", web.BeforeRouter, FilterUser)
adapter.InsertFilter("/setting/*", web.BeforeRouter, FilterUser)
adapter.InsertFilter("/book", web.BeforeRouter, FilterUser)
adapter.InsertFilter("/book/*", web.BeforeRouter, FilterUser)
adapter.InsertFilter("/api/*", web.BeforeRouter, FilterUser)
adapter.InsertFilter("/manage/*", web.BeforeRouter, FilterUser)
web.InsertFilter("/manager", web.BeforeRouter, FilterUser)
web.InsertFilter("/manager/*", web.BeforeRouter, FilterUser)
web.InsertFilter("/setting", web.BeforeRouter, FilterUser)
web.InsertFilter("/setting/*", web.BeforeRouter, FilterUser)
web.InsertFilter("/book", web.BeforeRouter, FilterUser)
web.InsertFilter("/book/*", web.BeforeRouter, FilterUser)
web.InsertFilter("/api/*", web.BeforeRouter, FilterUser)
web.InsertFilter("/manage/*", web.BeforeRouter, FilterUser)
var FinishRouter = func(ctx *context.Context) {
ctx.ResponseWriter.Header().Add("MinDoc-Version", conf.VERSION)
@ -56,6 +55,6 @@ func init() {
}
}
}
adapter.InsertFilter("/*", web.BeforeStatic, StartRouter, false)
adapter.InsertFilter("/*", web.BeforeRouter, FinishRouter, false)
web.InsertFilter("/*", web.BeforeStatic, StartRouter, web.WithReturnOnOutput(false))
web.InsertFilter("/*", web.BeforeRouter, FinishRouter, web.WithReturnOnOutput(false))
}