Files
backend/cmd/server/app.go
lafay b2b55ea52d
All checks were successful
Build Backend / build (push) Successful in 1m56s
Build Backend / build-docker (push) Successful in 1m15s
feat: enhance security with IP banning, ownership checks, and SSRF protection
Add comprehensive security improvements across the application:

- **IP-based login protection**: Implement IP ban system tracking login failures, auto-banning after threshold exceeded
- **Ownership verification**: Add userID parameter to Delete/Update operations for posts and comments to prevent unauthorized modifications
- **SSRF protection**: Add URL and resolved host validation for image URLs in chat and OpenAI client
- **SQL injection prevention**: Add EscapeLikeWildcard utility to escape special characters in LIKE queries
- **HTTP security**: Configure server timeouts and add security headers middleware
- **Rate limiting**: Refactor to support configurable duration and per-endpoint rate limits for auth routes
- **Error handling**: Standardize error responses using HandleError and proper error types
2026-04-30 12:26:25 +08:00

135 lines
2.8 KiB
Go

package main
import (
"context"
"fmt"
"net/http"
"time"
"with_you/internal/config"
"with_you/internal/grpc/runner"
"with_you/internal/router"
"with_you/internal/service"
"go.uber.org/zap"
"gorm.io/gorm"
)
// App 应用程序结构体
type App struct {
Config *config.Config
DB *gorm.DB
Router *router.Router
PushService service.PushService
HotRankWorker *service.HotRankWorker
GRPCServer *runner.Server
Server *http.Server
Logger *zap.Logger
}
// NewApp 创建应用程序
func NewApp(
cfg *config.Config,
db *gorm.DB,
r *router.Router,
pushService service.PushService,
hotRankWorker *service.HotRankWorker,
grpcServer *runner.Server,
logger *zap.Logger,
) *App {
return &App{
Config: cfg,
DB: db,
Router: r,
PushService: pushService,
HotRankWorker: hotRankWorker,
GRPCServer: grpcServer,
Logger: logger,
}
}
// Start 启动应用程序
func (a *App) Start(ctx context.Context) error {
// 1. 启动 gRPC 服务器(在 HTTP 服务器之前)
if a.GRPCServer != nil {
a.Logger.Info("Starting gRPC server")
if err := a.GRPCServer.Start(); err != nil {
return fmt.Errorf("failed to start gRPC server: %w", err)
}
}
// 2. 启动推送 Worker
a.PushService.StartPushWorker(ctx)
// 2.1 热门榜定时重算
if a.HotRankWorker != nil {
a.HotRankWorker.Start(ctx)
}
// 3. 创建 HTTP 服务器
addr := fmt.Sprintf("%s:%d", a.Config.Server.Host, a.Config.Server.Port)
a.Logger.Info("Starting HTTP server",
zap.String("address", addr),
)
a.Server = &http.Server{
Addr: addr,
Handler: a.Router.Engine(),
ReadHeaderTimeout: 10 * time.Second,
ReadTimeout: 30 * time.Second,
WriteTimeout: 60 * time.Second,
IdleTimeout: 120 * time.Second,
}
// 4. 启动 HTTP 服务器
go func() {
if err := a.Server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
a.Logger.Fatal("HTTP server listen error",
zap.Error(err),
)
}
}()
return nil
}
// Stop 停止应用程序
func (a *App) Stop(ctx context.Context) error {
a.Logger.Info("Shutting down server")
// 1. 停止 HTTP 服务器
if a.Server != nil {
a.Logger.Info("Stopping HTTP server")
if err := a.Server.Shutdown(ctx); err != nil {
a.Logger.Error("HTTP server forced to shutdown",
zap.Error(err),
)
}
}
// 2. 停止 gRPC 服务器
if a.GRPCServer != nil {
a.Logger.Info("Stopping gRPC server")
a.GRPCServer.Stop()
}
// 3. 停止推送 Worker
a.Logger.Info("Stopping push worker")
a.PushService.StopPushWorker()
if a.HotRankWorker != nil {
a.Logger.Info("Stopping hot rank worker")
a.HotRankWorker.Stop()
}
// 4. 关闭数据库连接
a.Logger.Info("Closing database connection")
sqlDB, err := a.DB.DB()
if err == nil {
sqlDB.Close()
}
a.Logger.Info("Server exited")
return nil
}