diff --git a/.gitignore b/.gitignore index b87d357..cd706c9 100644 --- a/.gitignore +++ b/.gitignore @@ -21,6 +21,5 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* -bin/ objs .idea diff --git a/.vscode/launch.json b/.vscode/launch.json index 69abab7..7880836 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -9,8 +9,8 @@ "type": "go", "request": "launch", "mode": "exec", - "program": "${workspaceFolder}/bin/srs-sip", - "cwd": "${workspaceFolder}/bin", + "program": "${workspaceFolder}/objs/srs-sip", + "cwd": "${workspaceFolder}/objs", "env": {}, "args": [] } diff --git a/Makefile b/Makefile index 5a95ff2..8f691c8 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ GOCMD=go GOBUILD=$(GOCMD) build -BINARY_NAME=bin/srs-sip +BINARY_NAME=objs/srs-sip MAIN_PATH=main/main.go VUE_DIR=html/NextGB @@ -20,7 +20,7 @@ run: install: $(GOBUILD) -o $(BINARY_NAME) $(MAIN_PATH) - mv $(BINARY_NAME) /usr/local/bin + mv $(BINARY_NAME) /usr/local/bin vue-install: cd $(VUE_DIR) && npm install diff --git a/README.md b/README.md index dfe36d5..ca8251b 100644 --- a/README.md +++ b/README.md @@ -21,12 +21,12 @@ If on Windows Run the program: ``` -./bin/srs-sip +./objs/srs-sip ``` Use docker ``` -docker run -id -p 1985:1985 -p 2025:2025 -p 5060:5060 -p 8025:8025 -p 9000:9000 -p 5060:5060/udp -p 8000:8000/udp --name srs-sip --env CANDIDATE=your_ip xiaoniu008/srs-sip:alpha +docker run -id -p 1985:1985 -p 5060:5060 -p 8025:8025 -p 9000:9000 -p 5060:5060/udp -p 8000:8000/udp --name srs-sip --env CANDIDATE=your_ip ossrs/srs-sip:alpha ``` ## Sequence diff --git a/build.bat b/build.bat index d428693..4ee7a64 100644 --- a/build.bat +++ b/build.bat @@ -1,7 +1,7 @@ @echo off setlocal -set BINARY_NAME=bin\srs-sip.exe +set BINARY_NAME=objs\srs-sip.exe set MAIN_PATH=main\main.go set VUE_DIR=html\NextGB set CONFIG_FILE=conf\config.yaml @@ -17,14 +17,14 @@ if "%1"=="all" goto all :build echo Building Go binary... -if not exist "bin" mkdir bin +if not exist "objs" mkdir objs go build -o %BINARY_NAME% %MAIN_PATH% echo Copying config file... if exist "%CONFIG_FILE%" ( - mkdir "bin\%~dp0%CONFIG_FILE%" 2>nul - xcopy /s /i /y "%CONFIG_FILE%" "bin\%~dp0%CONFIG_FILE%\" - echo Config file copied to bin\%~dp0%CONFIG_FILE% + mkdir "objs\%~dp0%CONFIG_FILE%" 2>nul + xcopy /s /i /y "%CONFIG_FILE%" "objs\%~dp0%CONFIG_FILE%\" + echo Config file copied to objs\%~dp0%CONFIG_FILE% ) else ( echo Warning: %CONFIG_FILE% not found ) @@ -35,8 +35,8 @@ echo Cleaning... if exist %BINARY_NAME% del /F /Q %BINARY_NAME% if exist %VUE_DIR%\dist rd /S /Q %VUE_DIR%\dist if exist %VUE_DIR%\node_modules rd /S /Q %VUE_DIR%\node_modules -if exist bin\html rd /S /Q bin\html -if exist bin\%CONFIG_FILE% del /F /Q bin\%CONFIG_FILE% +if exist objs\html rd /S /Q objs\html +if exist objs\%CONFIG_FILE% del /F /Q objs\%CONFIG_FILE% goto :eof :run @@ -105,18 +105,18 @@ if errorlevel 1 ( popd echo Vue build completed successfully -echo Copying dist files to bin directory... -if exist bin\html rd /S /Q bin\html -if not exist bin mkdir bin +echo Copying dist files to objs directory... +if exist objs\html rd /S /Q objs\html +if not exist objs mkdir objs if not exist "%VUE_DIR%\dist" ( echo Error: Vue dist directory not found at %VUE_DIR%\dist goto :eof ) -robocopy "%VUE_DIR%\dist" "bin\html" /E /NFL /NDL /NJH /NJS /nc /ns /np +robocopy "%VUE_DIR%\dist" "objs\html" /E /NFL /NDL /NJH /NJS /nc /ns /np if errorlevel 8 ( echo Error copying files ) else ( - echo Vue dist files successfully copied to bin\html + echo Vue dist files successfully copied to objs\html ) goto :eof diff --git a/build.sh b/build.sh index 088b5f4..dc89abc 100755 --- a/build.sh +++ b/build.sh @@ -1,6 +1,6 @@ #!/bin/bash -BINARY_NAME="bin/srs-sip" +BINARY_NAME="objs/srs-sip" MAIN_PATH="main/main.go" VUE_DIR="html/NextGB" CONFIG_FILE="conf/config.yaml" @@ -21,14 +21,14 @@ esac build() { echo "Building Go binary..." - mkdir -p bin + mkdir -p objs go build -o ${BINARY_NAME} ${MAIN_PATH} echo "Copying config file..." if [ -f "${CONFIG_FILE}" ]; then - mkdir -p "bin/$(dirname ${CONFIG_FILE})" - cp -a "${CONFIG_FILE}" "bin/$(dirname ${CONFIG_FILE})/" - echo "Config file copied to bin/$(dirname ${CONFIG_FILE})/" + mkdir -p "objs/$(dirname ${CONFIG_FILE})" + cp -a "${CONFIG_FILE}" "objs/$(dirname ${CONFIG_FILE})/" + echo "Config file copied to objs/$(dirname ${CONFIG_FILE})/" else echo "Warning: ${CONFIG_FILE} not found" fi @@ -39,8 +39,8 @@ clean() { rm -rf ${BINARY_NAME} rm -rf ${VUE_DIR}/dist rm -rf ${VUE_DIR}/node_modules - rm -rf bin/html - rm -rf bin/${CONFIG_FILE} + rm -rf objs/html + rm -rf objs/${CONFIG_FILE} } run() { @@ -107,16 +107,16 @@ vue_build() { popd > /dev/null echo "Vue build completed successfully" - echo "Copying dist files to bin directory..." - rm -rf bin/html - mkdir -p bin + echo "Copying dist files to objs directory..." + rm -rf objs/html + mkdir -p objs if [ ! -d "${VUE_DIR}/dist" ]; then echo "Error: Vue dist directory not found at ${VUE_DIR}/dist" return 1 fi - cp -r "${VUE_DIR}/dist" "bin/html" + cp -r "${VUE_DIR}/dist" "objs/html" if [ $? -eq 0 ]; then - echo "Vue dist files successfully copied to bin/html" + echo "Vue dist files successfully copied to objs/html" else echo "Error copying files" return 1 diff --git a/html/NextGB/index.html b/html/NextGB/index.html index 9e5fc8f..0d41463 100644 --- a/html/NextGB/index.html +++ b/html/NextGB/index.html @@ -4,7 +4,7 @@ - Vite App + NextGB
diff --git a/main/main.go b/main/main.go index a1258ee..9c40527 100644 --- a/main/main.go +++ b/main/main.go @@ -7,6 +7,7 @@ import ( "os/signal" "path" "strconv" + "strings" "syscall" "time" @@ -54,7 +55,10 @@ func main() { methods := handlers.AllowedMethods([]string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}) origins := handlers.AllowedOrigins([]string{"*"}) - // 设置API路由 - 需要在静态文件路由之前设置 + // 创建文件服务器 + fs := http.FileServer(http.Dir(conf.Http.Dir)) + + // 先注册API路由 apiSvr, err := api.NewHttpApiServer(conf, sipSvr) if err != nil { logger.Ef("create http service failed. err is %v", err.Error()) @@ -62,38 +66,23 @@ func main() { } apiSvr.Start(router) - // 使用配置中指定的目录,如果不存在则尝试备选目录 - targetDir := conf.Http.Dir - if _, err := os.Stat(path.Join(targetDir, "index.html")); err != nil { - backupDirs := []string{"./html", "../web/NextGB/dist"} - for _, dir := range backupDirs { - if _, err := os.Stat(path.Join(dir, "index.html")); err == nil { - targetDir = dir - break - } + // 添加静态文件处理 - 使用PathPrefix处理所有非API请求 + router.PathPrefix("/").Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // 如果是API路径,直接返回404 + if strings.HasPrefix(r.URL.Path, "/srs-sip/v1/") { + http.NotFound(w, r) + return } - } - if targetDir == "" { - logger.Ef(ctx, "index.html not found") - return - } - - // 创建文件服务器 - fs := http.FileServer(http.Dir(targetDir)) - - // 添加静态文件处理 - 使用NotFoundHandler来处理未匹配的路由 - router.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - logger.Tf(context.Background(), "Handling request: %s", r.URL.Path) // 检查请求的文件是否存在 - filePath := path.Join(targetDir, r.URL.Path) + filePath := path.Join(conf.Http.Dir, r.URL.Path) _, err := os.Stat(filePath) if os.IsNotExist(err) { // 如果文件不存在,返回 index.html r.URL.Path = "/" } fs.ServeHTTP(w, r) - }) + })) // 启动合并后的HTTP服务 go func() { @@ -107,7 +96,7 @@ func main() { IdleTimeout: 30 * time.Second, ReadHeaderTimeout: 5 * time.Second, } - logger.Tf(ctx, "http server listen on %s, home is %v", httpPort, targetDir) + logger.Tf(ctx, "http server listen on %s, home is %v", httpPort, conf.Http.Dir) if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed { logger.Ef(ctx, "listen on %s failed", httpPort) }