Merge pull request #10 from duiniuluantanqin/main

fix bug
This commit is contained in:
Haibo Chen
2025-02-04 11:50:40 +08:00
committed by GitHub
8 changed files with 45 additions and 57 deletions

1
.gitignore vendored
View File

@ -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

4
.vscode/launch.json vendored
View File

@ -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": []
}

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -4,7 +4,7 @@
<meta charset="UTF-8">
<link rel="icon" href="/favicon.ico">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vite App</title>
<title>NextGB</title>
</head>
<body>
<div id="app"></div>

View File

@ -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
}
}
}
if targetDir == "" {
logger.Ef(ctx, "index.html not found")
// 添加静态文件处理 - 使用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
}
// 创建文件服务器
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)
}