go服务端增加next路由支持

main
落雨楓 5 months ago
parent d0a954abce
commit 4b203902d4

@ -2,4 +2,6 @@ CGO_ENABLED=0 GOOS=linux GOARCH=mipsle go build -o dist/movie-sync-server-linux-
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o dist/movie-sync-server-linux-amd64
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o dist/movie-sync-server-linux-arm64
CGO_ENABLED=0 GOOS=linux GOARCH=mips go build -o dist/movie-sync-server-linux-mips
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o dist/movie-sync-server-darwin-amd64
CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -o dist/movie-sync-server-darwin-arm64
GOOS=windows GOARCH=amd64 go build -o dist/movie-sync-server-win-amd64.exe

@ -2,12 +2,14 @@ package services
import (
"embed"
"io"
"io/fs"
"net/http"
"strings"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)
type embedFileSystem struct {
@ -28,6 +30,44 @@ func EmbedFolder(fsEmbed embed.FS, targetPath string) static.ServeFileSystem {
}
}
func FindNextIndexFile(currentPath string, fsys static.ServeFileSystem) string {
findPath := strings.TrimSuffix(currentPath, "/")
logrus.Infoln("Finding index file for path:", findPath)
testFile := findPath + ".html"
logrus.Infoln("Checking for HTML file:", testFile)
if fsys.Exists("", testFile) {
return testFile
}
testFile = findPath + "/index.html"
logrus.Infoln("Checking for index file:", testFile)
if fsys.Exists("", testFile) {
return testFile
}
lastSlash := strings.LastIndex(findPath, "/")
if lastSlash < 0 {
return ""
}
findPath = findPath[:lastSlash]
lastSlash = strings.LastIndex(findPath, "/")
if lastSlash < 0 {
return ""
}
endfix := findPath[lastSlash+1:]
testFile = findPath + "/[" + endfix + "].html"
logrus.Infoln("Checking for dynamic route file:", testFile)
if fsys.Exists("", testFile) {
return testFile
}
return ""
}
func HandleStaticFile(f embed.FS, router *gin.Engine) {
root := EmbedFolder(f, "out")
router.Use(static.Serve("/", root))
@ -36,8 +76,38 @@ func HandleStaticFile(f embed.FS, router *gin.Engine) {
if c.Request.Method == http.MethodGet &&
!strings.ContainsRune(c.Request.URL.Path, '.') &&
!strings.HasPrefix(c.Request.URL.Path, "/socket.io") {
c.Request.URL.Path = "/"
staticServer(c)
logrus.Debugln("No route found for path:", c.Request.URL.Path)
// 尝试查找Next.js的index.html
indexFile := FindNextIndexFile(c.Request.URL.Path, root)
if indexFile != "" {
logrus.Infoln("Serving index file:", indexFile)
c.Request.URL.Path = indexFile
staticServer(c)
} else {
// 先读取404.html文件内容
file, err := root.Open("/404.html")
if err != nil {
logrus.Error("Failed to open 404.html:", err)
c.String(http.StatusNotFound, "404 Not Found")
return
}
defer file.Close()
// 读取文件内容
content, err := io.ReadAll(file)
if err != nil {
logrus.Error("Failed to read 404.html:", err)
c.String(http.StatusNotFound, "404 Not Found")
return
}
// 设置状态码和内容类型,然后返回内容
c.Header("Content-Type", "text/html; charset=utf-8")
c.Data(http.StatusNotFound, "text/html; charset=utf-8", content)
}
}
})
}

@ -188,11 +188,10 @@ export const Player = ({ roomName }: { roomName: string }) => {
}
function onPlay() {
console.log('play', { isAdmin: $userInfo.value?.isAdmin, isUserStartPlay: isUserStartPlay.current, playerReady: player.current?.readyState})
console.log('play')
if (!$userInfo.value?.isAdmin &&
isUserStartPlay.current && // 仅在用户已经开始播放的情况下触发
player.current && player.current.readyState >= 2) {
console.log('play video')
try {
player.current.play().then(() => {
handleUserStateUpdated(true);
@ -207,8 +206,6 @@ export const Player = ({ roomName }: { roomName: string }) => {
function onSetTime(d: any) {
const msg = d as SetTimeMessage
console.log('$userInfo', $userInfo.value)
console.log('set time condition', !msg.playTime, $userInfo.value?.isAdmin, !player.current, !isUserStartPlay.current)
if (!msg.playTime || $userInfo.value?.isAdmin || !player.current || !isUserStartPlay.current) {
return
}

@ -32,7 +32,7 @@ export default function Home() {
<div className='flex justify-center items-center h-screen'>
<form className='flex flex-col gap-3' onSubmit={onSubmit}>
<Label className='font-bold text-center'></Label>
<Input placeholder='请输入房间名' onChange={(e) => setRoomName(e.target.value)} name="roomId" />
<Input placeholder='请输入房间名' onChange={(e) => setRoomName(e.target.value)} name="roomId" autoCapitalize="none" />
<Button type='submit'></Button>
</form>
</div>

@ -83,12 +83,6 @@ export default function Page() {
...$userInfo.value,
isAdmin: msg.userInfo.isAdmin,
})
console.log('$userInfo', $userInfo.value)
}
function onSetUrl(d: any) {
}
socket.on('connect', onConnect)

Loading…
Cancel
Save