This commit is contained in:
Liang Ding 2022-07-17 12:22:32 +08:00
parent c8ea858976
commit 505b973c2d
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
70 changed files with 671 additions and 942 deletions

View file

@ -33,6 +33,7 @@ import (
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin"
"github.com/mssola/user_agent"
"github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/api"
"github.com/siyuan-note/siyuan/kernel/cmd"
"github.com/siyuan-note/siyuan/kernel/model"
@ -75,11 +76,11 @@ func Serve(fastMode bool) {
} else {
addr = "127.0.0.1:" + util.ServerPort
}
util.LogInfof("kernel is booting [%s]", "http://"+addr)
logging.LogInfof("kernel is booting [%s]", "http://"+addr)
util.HttpServing = true
if err := ginServer.Run(addr); nil != err {
if !fastMode {
util.LogErrorf("boot kernel failed: %s", err)
logging.LogErrorf("boot kernel failed: %s", err)
os.Exit(util.ExitCodeUnavailablePort)
}
}
@ -137,12 +138,12 @@ func serveAppearance(ginServer *gin.Engine) {
enUSFilePath := filepath.Join(appearancePath, "langs", "en_US.json")
enUSData, err := os.ReadFile(enUSFilePath)
if nil != err {
util.LogFatalf("read en_US.json [%s] failed: %s", enUSFilePath, err)
logging.LogFatalf("read en_US.json [%s] failed: %s", enUSFilePath, err)
return
}
enUSMap := map[string]interface{}{}
if err = gulu.JSON.UnmarshalJSON(enUSData, &enUSMap); nil != err {
util.LogFatalf("unmarshal en_US.json [%s] failed: %s", enUSFilePath, err)
logging.LogFatalf("unmarshal en_US.json [%s] failed: %s", enUSFilePath, err)
return
}
@ -155,7 +156,7 @@ func serveAppearance(ginServer *gin.Engine) {
langMap := map[string]interface{}{}
if err = gulu.JSON.UnmarshalJSON(data, &langMap); nil != err {
util.LogErrorf("unmarshal json [%s] failed: %s", filePath, err)
logging.LogErrorf("unmarshal json [%s] failed: %s", filePath, err)
c.JSON(200, enUSMap)
return
}
@ -183,7 +184,7 @@ func serveAppearance(ginServer *gin.Engine) {
func serveCheckAuth(c *gin.Context) {
data, err := os.ReadFile(filepath.Join(util.WorkingDir, "stage/auth.html"))
if nil != err {
util.LogErrorf("load auth page failed: %s", err)
logging.LogErrorf("load auth page failed: %s", err)
c.Status(500)
return
}
@ -237,7 +238,7 @@ func serveWebSocket(ginServer *gin.Engine) {
ginServer.GET("/ws", func(c *gin.Context) {
if err := util.WebSocketServer.HandleRequest(c.Writer, c.Request); nil != err {
util.LogErrorf("handle command failed: %s", err)
logging.LogErrorf("handle command failed: %s", err)
}
})
@ -246,14 +247,14 @@ func serveWebSocket(ginServer *gin.Engine) {
})
util.WebSocketServer.HandleConnect(func(s *melody.Session) {
//util.LogInfof("ws check auth for [%s]", s.Request.RequestURI)
//logging.LogInfof("ws check auth for [%s]", s.Request.RequestURI)
authOk := true
if "" != model.Conf.AccessAuthCode {
session, err := cookieStore.Get(s.Request, "siyuan")
if nil != err {
authOk = false
util.LogErrorf("get cookie failed: %s", err)
logging.LogErrorf("get cookie failed: %s", err)
} else {
val := session.Values["data"]
if nil == val {
@ -263,7 +264,7 @@ func serveWebSocket(ginServer *gin.Engine) {
err = gulu.JSON.UnmarshalJSON([]byte(val.(string)), &sess)
if nil != err {
authOk = false
util.LogErrorf("unmarshal cookie failed: %s", err)
logging.LogErrorf("unmarshal cookie failed: %s", err)
} else {
authOk = sess["AccessAuthCode"].(string) == model.Conf.AccessAuthCode
}
@ -273,13 +274,13 @@ func serveWebSocket(ginServer *gin.Engine) {
if !authOk {
s.CloseWithMsg([]byte(" unauthenticated"))
//util.LogWarnf("closed a unauthenticated session [%s]", util.GetRemoteAddr(s))
//logging.LogWarnf("closed a unauthenticated session [%s]", util.GetRemoteAddr(s))
return
}
util.AddPushChan(s)
//sessionId, _ := s.Get("id")
//util.LogInfof("ws [%s] connected", sessionId)
//logging.LogInfof("ws [%s] connected", sessionId)
})
util.WebSocketServer.HandleDisconnect(func(s *melody.Session) {
@ -290,18 +291,18 @@ func serveWebSocket(ginServer *gin.Engine) {
util.WebSocketServer.HandleError(func(s *melody.Session, err error) {
//sessionId, _ := s.Get("id")
//util.LogDebugf("ws [%s] failed: %s", sessionId, err)
//logging.LogDebugf("ws [%s] failed: %s", sessionId, err)
})
util.WebSocketServer.HandleClose(func(s *melody.Session, i int, str string) error {
//sessionId, _ := s.Get("id")
//util.LogDebugf("ws [%s] closed: %v, %v", sessionId, i, str)
//logging.LogDebugf("ws [%s] closed: %v, %v", sessionId, i, str)
return nil
})
util.WebSocketServer.HandleMessage(func(s *melody.Session, msg []byte) {
start := time.Now()
util.LogTracef("request [%s]", shortReqMsg(msg))
logging.LogTracef("request [%s]", shortReqMsg(msg))
request := map[string]interface{}{}
if err := gulu.JSON.UnmarshalJSON(msg, &request); nil != err {
result := util.NewResult()
@ -340,7 +341,7 @@ func serveWebSocket(ginServer *gin.Engine) {
}
end := time.Now()
util.LogTracef("parse cmd [%s] consumed [%d]ms", command.Name(), end.Sub(start).Milliseconds())
logging.LogTracef("parse cmd [%s] consumed [%d]ms", command.Name(), end.Sub(start).Milliseconds())
cmd.Exec(command)
})