This commit is contained in:
chenhaibo
2025-02-04 11:42:50 +08:00
parent c80247286e
commit 3940bc1992
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 # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid* hs_err_pid*
bin/
objs objs
.idea .idea

4
.vscode/launch.json vendored
View File

@ -9,8 +9,8 @@
"type": "go", "type": "go",
"request": "launch", "request": "launch",
"mode": "exec", "mode": "exec",
"program": "${workspaceFolder}/bin/srs-sip", "program": "${workspaceFolder}/objs/srs-sip",
"cwd": "${workspaceFolder}/bin", "cwd": "${workspaceFolder}/objs",
"env": {}, "env": {},
"args": [] "args": []
} }

View File

@ -1,6 +1,6 @@
GOCMD=go GOCMD=go
GOBUILD=$(GOCMD) build GOBUILD=$(GOCMD) build
BINARY_NAME=bin/srs-sip BINARY_NAME=objs/srs-sip
MAIN_PATH=main/main.go MAIN_PATH=main/main.go
VUE_DIR=html/NextGB VUE_DIR=html/NextGB

View File

@ -21,12 +21,12 @@ If on Windows
Run the program: Run the program:
``` ```
./bin/srs-sip ./objs/srs-sip
``` ```
Use docker 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 ## Sequence

View File

@ -1,7 +1,7 @@
@echo off @echo off
setlocal setlocal
set BINARY_NAME=bin\srs-sip.exe set BINARY_NAME=objs\srs-sip.exe
set MAIN_PATH=main\main.go set MAIN_PATH=main\main.go
set VUE_DIR=html\NextGB set VUE_DIR=html\NextGB
set CONFIG_FILE=conf\config.yaml set CONFIG_FILE=conf\config.yaml
@ -17,14 +17,14 @@ if "%1"=="all" goto all
:build :build
echo Building Go binary... echo Building Go binary...
if not exist "bin" mkdir bin if not exist "objs" mkdir objs
go build -o %BINARY_NAME% %MAIN_PATH% go build -o %BINARY_NAME% %MAIN_PATH%
echo Copying config file... echo Copying config file...
if exist "%CONFIG_FILE%" ( if exist "%CONFIG_FILE%" (
mkdir "bin\%~dp0%CONFIG_FILE%" 2>nul mkdir "objs\%~dp0%CONFIG_FILE%" 2>nul
xcopy /s /i /y "%CONFIG_FILE%" "bin\%~dp0%CONFIG_FILE%\" xcopy /s /i /y "%CONFIG_FILE%" "objs\%~dp0%CONFIG_FILE%\"
echo Config file copied to bin\%~dp0%CONFIG_FILE% echo Config file copied to objs\%~dp0%CONFIG_FILE%
) else ( ) else (
echo Warning: %CONFIG_FILE% not found echo Warning: %CONFIG_FILE% not found
) )
@ -35,8 +35,8 @@ echo Cleaning...
if exist %BINARY_NAME% del /F /Q %BINARY_NAME% if exist %BINARY_NAME% del /F /Q %BINARY_NAME%
if exist %VUE_DIR%\dist rd /S /Q %VUE_DIR%\dist 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 %VUE_DIR%\node_modules rd /S /Q %VUE_DIR%\node_modules
if exist bin\html rd /S /Q bin\html if exist objs\html rd /S /Q objs\html
if exist bin\%CONFIG_FILE% del /F /Q bin\%CONFIG_FILE% if exist objs\%CONFIG_FILE% del /F /Q objs\%CONFIG_FILE%
goto :eof goto :eof
:run :run
@ -105,18 +105,18 @@ if errorlevel 1 (
popd popd
echo Vue build completed successfully echo Vue build completed successfully
echo Copying dist files to bin directory... echo Copying dist files to objs directory...
if exist bin\html rd /S /Q bin\html if exist objs\html rd /S /Q objs\html
if not exist bin mkdir bin if not exist objs mkdir objs
if not exist "%VUE_DIR%\dist" ( if not exist "%VUE_DIR%\dist" (
echo Error: Vue dist directory not found at %VUE_DIR%\dist echo Error: Vue dist directory not found at %VUE_DIR%\dist
goto :eof 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 ( if errorlevel 8 (
echo Error copying files echo Error copying files
) else ( ) else (
echo Vue dist files successfully copied to bin\html echo Vue dist files successfully copied to objs\html
) )
goto :eof goto :eof

View File

@ -1,6 +1,6 @@
#!/bin/bash #!/bin/bash
BINARY_NAME="bin/srs-sip" BINARY_NAME="objs/srs-sip"
MAIN_PATH="main/main.go" MAIN_PATH="main/main.go"
VUE_DIR="html/NextGB" VUE_DIR="html/NextGB"
CONFIG_FILE="conf/config.yaml" CONFIG_FILE="conf/config.yaml"
@ -21,14 +21,14 @@ esac
build() { build() {
echo "Building Go binary..." echo "Building Go binary..."
mkdir -p bin mkdir -p objs
go build -o ${BINARY_NAME} ${MAIN_PATH} go build -o ${BINARY_NAME} ${MAIN_PATH}
echo "Copying config file..." echo "Copying config file..."
if [ -f "${CONFIG_FILE}" ]; then if [ -f "${CONFIG_FILE}" ]; then
mkdir -p "bin/$(dirname ${CONFIG_FILE})" mkdir -p "objs/$(dirname ${CONFIG_FILE})"
cp -a "${CONFIG_FILE}" "bin/$(dirname ${CONFIG_FILE})/" cp -a "${CONFIG_FILE}" "objs/$(dirname ${CONFIG_FILE})/"
echo "Config file copied to bin/$(dirname ${CONFIG_FILE})/" echo "Config file copied to objs/$(dirname ${CONFIG_FILE})/"
else else
echo "Warning: ${CONFIG_FILE} not found" echo "Warning: ${CONFIG_FILE} not found"
fi fi
@ -39,8 +39,8 @@ clean() {
rm -rf ${BINARY_NAME} rm -rf ${BINARY_NAME}
rm -rf ${VUE_DIR}/dist rm -rf ${VUE_DIR}/dist
rm -rf ${VUE_DIR}/node_modules rm -rf ${VUE_DIR}/node_modules
rm -rf bin/html rm -rf objs/html
rm -rf bin/${CONFIG_FILE} rm -rf objs/${CONFIG_FILE}
} }
run() { run() {
@ -107,16 +107,16 @@ vue_build() {
popd > /dev/null popd > /dev/null
echo "Vue build completed successfully" echo "Vue build completed successfully"
echo "Copying dist files to bin directory..." echo "Copying dist files to objs directory..."
rm -rf bin/html rm -rf objs/html
mkdir -p bin mkdir -p objs
if [ ! -d "${VUE_DIR}/dist" ]; then if [ ! -d "${VUE_DIR}/dist" ]; then
echo "Error: Vue dist directory not found at ${VUE_DIR}/dist" echo "Error: Vue dist directory not found at ${VUE_DIR}/dist"
return 1 return 1
fi fi
cp -r "${VUE_DIR}/dist" "bin/html" cp -r "${VUE_DIR}/dist" "objs/html"
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
echo "Vue dist files successfully copied to bin/html" echo "Vue dist files successfully copied to objs/html"
else else
echo "Error copying files" echo "Error copying files"
return 1 return 1

View File

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

View File

@ -7,6 +7,7 @@ import (
"os/signal" "os/signal"
"path" "path"
"strconv" "strconv"
"strings"
"syscall" "syscall"
"time" "time"
@ -54,7 +55,10 @@ func main() {
methods := handlers.AllowedMethods([]string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}) methods := handlers.AllowedMethods([]string{"GET", "POST", "PUT", "DELETE", "OPTIONS"})
origins := handlers.AllowedOrigins([]string{"*"}) origins := handlers.AllowedOrigins([]string{"*"})
// 设置API路由 - 需要在静态文件路由之前设置 // 创建文件服务器
fs := http.FileServer(http.Dir(conf.Http.Dir))
// 先注册API路由
apiSvr, err := api.NewHttpApiServer(conf, sipSvr) apiSvr, err := api.NewHttpApiServer(conf, sipSvr)
if err != nil { if err != nil {
logger.Ef("create http service failed. err is %v", err.Error()) logger.Ef("create http service failed. err is %v", err.Error())
@ -62,38 +66,23 @@ func main() {
} }
apiSvr.Start(router) apiSvr.Start(router)
// 使用配置中指定的目录,如果不存在则尝试备选目录 // 添加静态文件处理 - 使用PathPrefix处理所有非API请求
targetDir := conf.Http.Dir router.PathPrefix("/").Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if _, err := os.Stat(path.Join(targetDir, "index.html")); err != nil { // 如果是API路径直接返回404
backupDirs := []string{"./html", "../web/NextGB/dist"} if strings.HasPrefix(r.URL.Path, "/srs-sip/v1/") {
for _, dir := range backupDirs { http.NotFound(w, r)
if _, err := os.Stat(path.Join(dir, "index.html")); err == nil { return
targetDir = dir
break
}
} }
}
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) _, err := os.Stat(filePath)
if os.IsNotExist(err) { if os.IsNotExist(err) {
// 如果文件不存在,返回 index.html // 如果文件不存在,返回 index.html
r.URL.Path = "/" r.URL.Path = "/"
} }
fs.ServeHTTP(w, r) fs.ServeHTTP(w, r)
}) }))
// 启动合并后的HTTP服务 // 启动合并后的HTTP服务
go func() { go func() {
@ -107,7 +96,7 @@ func main() {
IdleTimeout: 30 * time.Second, IdleTimeout: 30 * time.Second,
ReadHeaderTimeout: 5 * 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 { if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.Ef(ctx, "listen on %s failed", httpPort) logger.Ef(ctx, "listen on %s failed", httpPort)
} }