Feat: use history router mode
This commit is contained in:
parent
5b44606276
commit
5d406f1c6a
6 changed files with 74 additions and 9 deletions
69
middleware/frontend.go
Normal file
69
middleware/frontend.go
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
package middleware
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/cloudreve/Cloudreve/v3/bootstrap"
|
||||||
|
model "github.com/cloudreve/Cloudreve/v3/models"
|
||||||
|
"github.com/cloudreve/Cloudreve/v3/pkg/util"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// FrontendFileHandler 前端静态文件处理
|
||||||
|
func FrontendFileHandler() gin.HandlerFunc {
|
||||||
|
ignoreFunc := func(c *gin.Context) {
|
||||||
|
c.Next()
|
||||||
|
}
|
||||||
|
|
||||||
|
if bootstrap.StaticFS == nil {
|
||||||
|
return ignoreFunc
|
||||||
|
}
|
||||||
|
|
||||||
|
// 读取index.html
|
||||||
|
file, err := bootstrap.StaticFS.Open("/index.html")
|
||||||
|
if err != nil {
|
||||||
|
util.Log().Warning("静态文件[index.html]不存在,可能会影响首页展示")
|
||||||
|
return ignoreFunc
|
||||||
|
}
|
||||||
|
|
||||||
|
fileContentBytes, err := ioutil.ReadAll(file)
|
||||||
|
if err != nil {
|
||||||
|
util.Log().Warning("静态文件[index.html]读取失败,可能会影响首页展示")
|
||||||
|
return ignoreFunc
|
||||||
|
}
|
||||||
|
fileContent := string(fileContentBytes)
|
||||||
|
|
||||||
|
fileServer := http.FileServer(bootstrap.StaticFS)
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
path := c.Request.URL.Path
|
||||||
|
|
||||||
|
// API 跳过
|
||||||
|
if strings.HasPrefix(path, "/api") || strings.HasPrefix(path, "/custom") || strings.HasPrefix(path, "/custom") || path == "manifest.json" {
|
||||||
|
c.Next()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 不存在的路径和index.html均返回index.html
|
||||||
|
if !bootstrap.StaticFS.Exists("/", path) || (path == "/index.html") || (path == "/") {
|
||||||
|
// 读取、替换站点设置
|
||||||
|
options := model.GetSettingByNames("siteName", "siteKeywords", "siteScript",
|
||||||
|
"pwa_small_icon")
|
||||||
|
finalHTML := util.Replace(map[string]string{
|
||||||
|
"{siteName}": options["siteName"],
|
||||||
|
"{siteDes}": options["siteDes"],
|
||||||
|
"{siteScript}": options["siteScript"],
|
||||||
|
"{pwa_small_icon}": options["pwa_small_icon"],
|
||||||
|
}, fileContent)
|
||||||
|
|
||||||
|
c.Header("Content-Type", "text/html")
|
||||||
|
c.String(200, finalHTML)
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 存在的静态文件
|
||||||
|
fileServer.ServeHTTP(c.Writer, c.Request)
|
||||||
|
c.Abort()
|
||||||
|
}
|
||||||
|
}
|
|
@ -91,7 +91,7 @@ func OneDriveOAuth(c *gin.Context) {
|
||||||
queries.Add("msg", res.Msg)
|
queries.Add("msg", res.Msg)
|
||||||
queries.Add("err", res.Error)
|
queries.Add("err", res.Error)
|
||||||
redirect.RawQuery = queries.Encode()
|
redirect.RawQuery = queries.Encode()
|
||||||
c.Redirect(301, "/#"+redirect.String())
|
c.Redirect(301, "/"+redirect.String())
|
||||||
} else {
|
} else {
|
||||||
c.JSON(200, ErrorResponse(err))
|
c.JSON(200, ErrorResponse(err))
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package routers
|
package routers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/cloudreve/Cloudreve/v3/bootstrap"
|
|
||||||
"github.com/cloudreve/Cloudreve/v3/middleware"
|
"github.com/cloudreve/Cloudreve/v3/middleware"
|
||||||
"github.com/cloudreve/Cloudreve/v3/pkg/conf"
|
"github.com/cloudreve/Cloudreve/v3/pkg/conf"
|
||||||
"github.com/cloudreve/Cloudreve/v3/pkg/hashid"
|
"github.com/cloudreve/Cloudreve/v3/pkg/hashid"
|
||||||
|
@ -9,7 +8,6 @@ import (
|
||||||
"github.com/cloudreve/Cloudreve/v3/routers/controllers"
|
"github.com/cloudreve/Cloudreve/v3/routers/controllers"
|
||||||
"github.com/gin-contrib/cors"
|
"github.com/gin-contrib/cors"
|
||||||
"github.com/gin-contrib/gzip"
|
"github.com/gin-contrib/gzip"
|
||||||
"github.com/gin-contrib/static"
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -82,8 +80,7 @@ func InitMasterRouter() *gin.Engine {
|
||||||
静态资源
|
静态资源
|
||||||
*/
|
*/
|
||||||
r.Use(gzip.Gzip(gzip.DefaultCompression, gzip.WithExcludedPaths([]string{"/api/"})))
|
r.Use(gzip.Gzip(gzip.DefaultCompression, gzip.WithExcludedPaths([]string{"/api/"})))
|
||||||
r.Use(middleware.InjectSiteInfo())
|
r.Use(middleware.FrontendFileHandler())
|
||||||
r.Use(static.Serve("/", bootstrap.StaticFS))
|
|
||||||
r.GET("manifest.json", controllers.Manifest)
|
r.GET("manifest.json", controllers.Manifest)
|
||||||
|
|
||||||
v3 := r.Group("/api/v3")
|
v3 := r.Group("/api/v3")
|
||||||
|
|
|
@ -139,7 +139,7 @@ func (service *ShareCreateService) Create(c *gin.Context) serializer.Response {
|
||||||
uid := hashid.HashID(id, hashid.ShareID)
|
uid := hashid.HashID(id, hashid.ShareID)
|
||||||
// 最终得到分享链接
|
// 最终得到分享链接
|
||||||
siteURL := model.GetSiteURL()
|
siteURL := model.GetSiteURL()
|
||||||
sharePath, _ := url.Parse("/#/s/" + uid)
|
sharePath, _ := url.Parse("/s/" + uid)
|
||||||
shareURL := siteURL.ResolveReference(sharePath)
|
shareURL := siteURL.ResolveReference(sharePath)
|
||||||
|
|
||||||
return serializer.Response{
|
return serializer.Response{
|
||||||
|
|
|
@ -3,7 +3,6 @@ package user
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
model "github.com/cloudreve/Cloudreve/v3/models"
|
model "github.com/cloudreve/Cloudreve/v3/models"
|
||||||
|
@ -108,7 +107,7 @@ func (service *UserResetEmailService) Reset(c *gin.Context) serializer.Response
|
||||||
finalURL.RawQuery = queries.Encode()
|
finalURL.RawQuery = queries.Encode()
|
||||||
|
|
||||||
// 发送密码重设邮件
|
// 发送密码重设邮件
|
||||||
title, body := email.NewResetEmail(user.Nick, strings.ReplaceAll(finalURL.String(), "/reset", "/#/reset"))
|
title, body := email.NewResetEmail(user.Nick, finalURL.String())
|
||||||
if err := email.Send(user.Email, title, body); err != nil {
|
if err := email.Send(user.Email, title, body); err != nil {
|
||||||
return serializer.Err(serializer.CodeInternalSetting, "无法发送密码重设邮件", err)
|
return serializer.Err(serializer.CodeInternalSetting, "无法发送密码重设邮件", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -95,7 +95,7 @@ func (service *UserRegisterService) Register(c *gin.Context) serializer.Response
|
||||||
|
|
||||||
// 返送激活邮件
|
// 返送激活邮件
|
||||||
title, body := email.NewActivationEmail(user.Email,
|
title, body := email.NewActivationEmail(user.Email,
|
||||||
strings.ReplaceAll(finalURL.String(), "/activate", "/#/activate"),
|
finalURL.String(),
|
||||||
)
|
)
|
||||||
if err := email.Send(user.Email, title, body); err != nil {
|
if err := email.Send(user.Email, title, body); err != nil {
|
||||||
return serializer.Err(serializer.CodeInternalSetting, "无法发送激活邮件", err)
|
return serializer.Err(serializer.CodeInternalSetting, "无法发送激活邮件", err)
|
||||||
|
|
Loading…
Add table
Reference in a new issue