From 4b51086841ebd0bf425480ac8a43111c65062c73 Mon Sep 17 00:00:00 2001 From: udieudie Date: Sat, 10 May 2025 17:29:05 +0800 Subject: [PATCH] =?UTF-8?q?david/fea:=E5=A2=9E=E5=8A=A0=E4=BA=86=E7=94=9F?= =?UTF-8?q?=E4=BA=A7=E7=8E=AF=E5=A2=83=E7=9A=84=E5=A4=84=E7=90=86=EF=BC=8C?= =?UTF-8?q?=E4=BB=A5=E5=8F=8A=E5=89=8D=E7=AB=AF=E5=AF=B9=E5=BA=94=E7=9A=84?= =?UTF-8?q?=E7=94=9F=E4=BA=A7=E7=8E=AF=E5=A2=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/middleware/RecoveryMiddleware.go | 19 ++++++++++++++++ configs/config.prod.yaml | 11 +++++++--- main.go | 17 +++++++++++--- templates/index.html | 33 ++++++++++++++++------------ 4 files changed, 60 insertions(+), 20 deletions(-) create mode 100644 app/middleware/RecoveryMiddleware.go diff --git a/app/middleware/RecoveryMiddleware.go b/app/middleware/RecoveryMiddleware.go new file mode 100644 index 0000000..485979f --- /dev/null +++ b/app/middleware/RecoveryMiddleware.go @@ -0,0 +1,19 @@ +package middleware + +import ( + "github.com/gin-gonic/gin" + "log" + "net/http" +) + +func RecoveryMiddleware() gin.HandlerFunc { + return func(c *gin.Context) { + defer func() { + if err := recover(); err != nil { + log.Printf("Panic recovered: %v", err) + c.AbortWithStatus(http.StatusInternalServerError) + } + }() + c.Next() + } +} diff --git a/configs/config.prod.yaml b/configs/config.prod.yaml index 842b7e1..80e6e3e 100644 --- a/configs/config.prod.yaml +++ b/configs/config.prod.yaml @@ -1,10 +1,15 @@ app: env: "prod" - port: 80 + port: 8011 database: enabled: true - host: "prod-db-cluster.example.com" - user: "prod_user" + host: "82.157.179.66" + port: 3306 + user: "remotexinghuo" + password: "xinghuo@RMote&514" + database_name: "blazerider" + max_open_conns: 100 + max_idle_conns: 20 douyin: diff --git a/main.go b/main.go index d15fd3c..ca10cef 100644 --- a/main.go +++ b/main.go @@ -5,22 +5,33 @@ import ( "BlazeRider-backend/app/controller" "BlazeRider-backend/app/db" "BlazeRider-backend/app/manager" + "BlazeRider-backend/app/middleware" "BlazeRider-backend/config" "BlazeRider-backend/routes" + "flag" "fmt" "github.com/gin-gonic/gin" "log" "net/http" + "strconv" + "strings" ) func main() { + // 1. 定义并解析命令行参数 + var env string + flag.StringVar(&env, "env", "prod", "运行环境(dev/prod)") + flag.Parse() + env = strings.ToLower(env) // 统一转为小写 + r := gin.Default() + r.Use(middleware.RecoveryMiddleware()) // 配置CORS中间件 r.Use(CORSMiddleware()) // 初始化配置(自动检测环境) - if err := config.Init("dev"); err != nil { + if err := config.Init(env); err != nil { log.Fatalf("Failed to init config: %v", err) } @@ -29,7 +40,7 @@ func main() { log.Printf("Running in %s mode on port %d", cfg.App.Env, cfg.App.Port) if cfg.App.Env == "prod" { - gin.SetMode(gin.ReleaseMode) // 设置为 release 模式 + gin.SetMode(gin.ReleaseMode) // 设置为 rease 模式 } // 初始化所有Manager @@ -75,7 +86,7 @@ func main() { //app.RegistRoute(r, controllers) r.StaticFS("/static", http.Dir("./public")) - r.Run(":8001") + r.Run(":" + strconv.Itoa(cfg.App.Port)) } // CORSMiddleware 允许跨域请求 diff --git a/templates/index.html b/templates/index.html index e0ba5be..3675bac 100644 --- a/templates/index.html +++ b/templates/index.html @@ -114,7 +114,7 @@ var liveRoomID = "11111" var userId = "" var consoleMessageArray = [] - var ip = "82.157.179.66" + var ip = "82.157.179.66:8001" var moneyRemaining = 3000 // 直播间登录给的token var liveRoomToken = "" @@ -153,7 +153,12 @@ var queryParams = getQueryParams(); var local = queryParams.local === '1'; // 检查是否有local=1参数 if(local){ - ip = "localhost" + ip = "localhost:8001" + } + + var prod = queryParams.prod === '1'; // 检查是否有local=1参数 + if(prod){ + ip = ip.replaceAll(":8001", ":8011") } var giftList = [ @@ -229,7 +234,7 @@ liveRoomToken = encodeURIComponent(liveRoomToken) // var url = "ws://" + ip + ":8001/index/regist?liveRoomID=" + liveRoomID + "&liveRoomToken=" + liveRoomToken; - var url = "ws://" + ip + ":8001/index/regist?" + "liveRoomToken=" + liveRoomToken; + var url = "ws://" + ip + "/index/regist?" + "liveRoomToken=" + liveRoomToken; console.log("connect ws url : ", url) useConnection(url) if (ws.readyState === WebSocket.OPEN) { @@ -362,7 +367,7 @@ } giftNum = parseInt(giftNum) - const url = `http://${ip}:8001/api/v1/getLikeRequest`; + const url = `http://${ip}/api/v1/getLikeRequest`; let likeArr = [ { @@ -413,7 +418,7 @@ return } - const url = `http://${ip}:8001/api/v1/getGiftRequest`; + const url = `http://${ip}/api/v1/getGiftRequest`; let giftData = getGiftData(giftId) @@ -534,7 +539,7 @@ joinedTrack = num - const url = `http://${ip}:8001/api/v1/getPingLun`; + const url = `http://${ip}/api/v1/getPingLun`; console.log("sendJoinMsg function sendJoinMsg joinedTrack : ", joinedTrack) console.log("sendJoinMsg function sendJoinMsg headImg : ", headImg) @@ -804,7 +809,7 @@ } function sendTokenRequest(){ - const url = `http://${ip}:8001/api/v1/getToken`; + const url = `http://${ip}/api/v1/getToken`; const params = { userId: 'david' }; const headers = { 'Authorization': 'Bearer your-token' }; @@ -818,7 +823,7 @@ } function sendAnchorLoginRequst(){ - const url = `http://${ip}:8001/api/v1/auth/anchor/login`; + const url = `http://${ip}/api/v1/auth/anchor/login`; const params = { token: 'david-token-test' }; // const headers = { 'Authorization': 'Bearer your-token' }; @@ -846,7 +851,7 @@ function sendPingLunRequest(){ - const url = `http://${ip}:8001/api/v1/getPingLun`; + const url = `http://${ip}/api/v1/getPingLun`; // "http://82.157.179.66:8001/static/16e1041e5e02db1c77678084102a9b6b.jpeg", // "http://82.157.179.66:8001/static/8944face68cc71bc452ea8a4fd0abc5b.jpeg", @@ -939,7 +944,7 @@ } function sendGiftRequest(){ - const url = `http://${ip}:8001/api/v1/getGift`; + const url = `http://${ip}/api/v1/getGift`; let giftArr = [ { @@ -1038,7 +1043,7 @@ } function sendFinishRequest(){ - const url = `http://${ip}:8001/api/v1/auth/finishGame`; + const url = `http://${ip}/api/v1/auth/finishGame`; let message = { "msg_id": "1029378912340134", // string类型id @@ -1095,7 +1100,7 @@ } function sendGetRanksRequest(){ - const url = `http://${ip}:8001/api/v1/auth/getRanks`; + const url = `http://${ip}/api/v1/auth/getRanks`; liveRoomID = document.getElementById('liveRoomId').value; if(liveRoomID == ''){ @@ -1133,7 +1138,7 @@ } function sendGetWorldRecordsRequest(){ - const url = `http://${ip}:8001/api/v1/auth/getWorldRecords`; + const url = `http://${ip}/api/v1/auth/getWorldRecords`; liveRoomID = document.getElementById('liveRoomId').value; if(liveRoomID == ''){ @@ -1163,7 +1168,7 @@ } function sendClientErrorRequest(){ - const url = `http://${ip}:8001/api/v1/recordClientError`; + const url = `http://${ip}/api/v1/recordClientError`; const data = {"live_room_id": "111", "desc": "this is desc", -- Gitee