diff --git a/build.sh b/build.sh index e8cb5a7..6376e38 100755 --- a/build.sh +++ b/build.sh @@ -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 \ No newline at end of file diff --git a/services/static.go b/services/static.go index 101fa89..6843b8b 100644 --- a/services/static.go +++ b/services/static.go @@ -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) + } } }) } diff --git a/src/components/player.tsx b/src/components/player.tsx index 9de0cba..a9671c7 100644 --- a/src/components/player.tsx +++ b/src/components/player.tsx @@ -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 } diff --git a/src/pages/index.tsx b/src/pages/index.tsx index 67cf465..89d4989 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -32,7 +32,7 @@ export default function Home() {