add
This commit is contained in:
204
deploy/api/api_update_account/main.go
Normal file
204
deploy/api/api_update_account/main.go
Normal file
@@ -0,0 +1,204 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
// UpdateAccountRequest 更新账号请求参数
|
||||
type UpdateAccountRequest struct {
|
||||
UserID string `json:"user_id" binding:"required"`
|
||||
Account string `json:"account" binding:"required"`
|
||||
}
|
||||
|
||||
// UpdateAccountResponse 更新账号响应结构
|
||||
type UpdateAccountResponse struct {
|
||||
Success bool `json:"success"`
|
||||
Message string `json:"message"`
|
||||
Data struct {
|
||||
UserID string `json:"user_id,omitempty"`
|
||||
Account string `json:"account,omitempty"`
|
||||
} `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
var db *sql.DB
|
||||
|
||||
func main() {
|
||||
// 记录程序启动时间
|
||||
startTime := time.Now()
|
||||
fmt.Printf("[%s] 程序开始启动\n", startTime.Format(time.RFC3339))
|
||||
|
||||
// 初始化Gin引擎
|
||||
r := gin.Default()
|
||||
fmt.Println("[INFO] Gin引擎初始化完成")
|
||||
|
||||
// 从环境变量获取数据库配置
|
||||
fmt.Println("[INFO] 开始读取数据库配置")
|
||||
dbHost := os.Getenv("DB_HOST")
|
||||
dbPort := os.Getenv("DB_PORT")
|
||||
dbUser := os.Getenv("DB_USER")
|
||||
dbPassword := os.Getenv("DB_PASSWORD")
|
||||
dbName := os.Getenv("DB_NAME")
|
||||
|
||||
fmt.Printf("[INFO] 数据库配置: host=%s, port=%s, user=%s, dbname=%s\n",
|
||||
dbHost, dbPort, dbUser, dbName)
|
||||
|
||||
// 构建数据库连接字符串
|
||||
connStr := fmt.Sprintf(
|
||||
"host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
|
||||
dbHost, dbPort, dbUser, dbPassword, dbName,
|
||||
)
|
||||
fmt.Println("[INFO] 数据库连接字符串构建完成")
|
||||
|
||||
var err error
|
||||
db, err = sql.Open("postgres", connStr)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("[ERROR] 无法连接数据库: %v", err))
|
||||
}
|
||||
defer func() {
|
||||
// 关闭数据库连接时记录日志
|
||||
if err := db.Close(); err != nil {
|
||||
fmt.Printf("[ERROR] 关闭数据库连接失败: %v\n", err)
|
||||
} else {
|
||||
fmt.Println("[INFO] 数据库连接已关闭")
|
||||
}
|
||||
}()
|
||||
|
||||
// 验证数据库连接
|
||||
fmt.Println("[INFO] 开始验证数据库连接")
|
||||
if err := db.Ping(); err != nil {
|
||||
panic(fmt.Sprintf("[ERROR] 数据库连接失败: %v", err))
|
||||
}
|
||||
fmt.Println("[INFO] 数据库连接验证成功")
|
||||
|
||||
// 注册更新账号接口
|
||||
r.POST("/user/update/account", updateAccountHandler)
|
||||
fmt.Println("[INFO] 已注册接口: POST /user/update/account")
|
||||
|
||||
// 启动服务,监听80端口
|
||||
startMsg := "服务启动在80端口"
|
||||
fmt.Printf("[%s] %s\n", time.Now().Format(time.RFC3339), startMsg)
|
||||
fmt.Printf("[INFO] 启动耗时: %v\n", time.Since(startTime))
|
||||
|
||||
if err := r.Run(":80"); err != nil {
|
||||
fmt.Printf("[ERROR] 服务启动失败: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
// updateAccountHandler 处理账号更新逻辑
|
||||
func updateAccountHandler(c *gin.Context) {
|
||||
// 记录请求开始时间和请求ID
|
||||
startTime := time.Now()
|
||||
reqID := c.Request.Header.Get("X-Request-ID")
|
||||
if reqID == "" {
|
||||
reqID = fmt.Sprintf("req-%d", time.Now().UnixNano())
|
||||
}
|
||||
fmt.Printf("[%s] [REQUEST] %s - 收到请求: %s %s, RequestID: %s\n",
|
||||
startTime.Format(time.RFC3339),
|
||||
c.ClientIP(),
|
||||
c.Request.Method,
|
||||
c.Request.URL.Path,
|
||||
reqID,
|
||||
)
|
||||
|
||||
var req UpdateAccountRequest
|
||||
// 绑定并验证请求参数
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
errMsg := fmt.Sprintf("请求参数错误: %v", err)
|
||||
fmt.Printf("[%s] [ERROR] %s, RequestID: %s, 错误详情: %v\n",
|
||||
time.Now().Format(time.RFC3339),
|
||||
errMsg,
|
||||
reqID,
|
||||
err,
|
||||
)
|
||||
c.JSON(http.StatusBadRequest, UpdateAccountResponse{
|
||||
Success: false,
|
||||
Message: errMsg,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 打印接收到的请求参数
|
||||
fmt.Printf("[%s] [INFO] 接收到更新请求, RequestID: %s, UserID: %s, 新账号: %s\n",
|
||||
time.Now().Format(time.RFC3339),
|
||||
reqID,
|
||||
req.UserID,
|
||||
req.Account,
|
||||
)
|
||||
|
||||
// 更新账号信息
|
||||
updateQuery := `
|
||||
UPDATE user_account
|
||||
SET account = $1
|
||||
WHERE user_id = $2
|
||||
RETURNING id, user_id, account
|
||||
`
|
||||
fmt.Printf("[%s] [INFO] 执行SQL: %q, 参数: [%s, %s], RequestID: %s\n",
|
||||
time.Now().Format(time.RFC3339),
|
||||
updateQuery,
|
||||
req.Account,
|
||||
req.UserID,
|
||||
reqID,
|
||||
)
|
||||
|
||||
var (
|
||||
id string
|
||||
userID string
|
||||
account string
|
||||
)
|
||||
err := db.QueryRow(updateQuery, req.Account, req.UserID).Scan(&id, &userID, &account)
|
||||
switch {
|
||||
case err == sql.ErrNoRows:
|
||||
errMsg := "未找到该用户的账号信息"
|
||||
fmt.Printf("[%s] [WARN] %s, UserID: %s, RequestID: %s\n",
|
||||
time.Now().Format(time.RFC3339),
|
||||
errMsg,
|
||||
req.UserID,
|
||||
reqID,
|
||||
)
|
||||
c.JSON(http.StatusOK, UpdateAccountResponse{
|
||||
Success: false,
|
||||
Message: errMsg,
|
||||
})
|
||||
return
|
||||
case err != nil:
|
||||
errMsg := fmt.Sprintf("更新账号失败: %v", err)
|
||||
fmt.Printf("[%s] [ERROR] %s, UserID: %s, RequestID: %s, 错误详情: %v\n",
|
||||
time.Now().Format(time.RFC3339),
|
||||
errMsg,
|
||||
req.UserID,
|
||||
reqID,
|
||||
err,
|
||||
)
|
||||
c.JSON(http.StatusInternalServerError, UpdateAccountResponse{
|
||||
Success: false,
|
||||
Message: errMsg,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 更新成功
|
||||
response := UpdateAccountResponse{
|
||||
Success: true,
|
||||
Message: "账号更新成功",
|
||||
}
|
||||
response.Data.UserID = userID
|
||||
response.Data.Account = account
|
||||
|
||||
// 记录成功日志
|
||||
fmt.Printf("[%s] [INFO] 账号更新成功, RequestID: %s, UserID: %s, 新账号: %s, 耗时: %v\n",
|
||||
time.Now().Format(time.RFC3339),
|
||||
reqID,
|
||||
userID,
|
||||
account,
|
||||
time.Since(startTime),
|
||||
)
|
||||
|
||||
c.JSON(http.StatusOK, response)
|
||||
}
|
Reference in New Issue
Block a user