98f581ec97
- Updated cursor rules to emphasize the importance of dependency injection and logging practices. - Introduced a new user management domain, including entities, services, handlers, and validation. - Implemented user authentication features such as login, registration, and role-based access control. - Added Redis integration for token management and caching. - Enhanced API documentation with Swagger for user-related endpoints. - Updated configuration to support new JWT settings and Redis connection parameters.
149 lines
3.9 KiB
Go
149 lines
3.9 KiB
Go
// Package main Tender Management API
|
|
//
|
|
// This is the API documentation for the Tender Management System.
|
|
//
|
|
// Schemes: http, https
|
|
// Host: localhost:8081
|
|
// BasePath: /api/v1
|
|
// Version: 1.0.0
|
|
//
|
|
// Consumes:
|
|
// - application/json
|
|
//
|
|
// Produces:
|
|
// - application/json
|
|
//
|
|
// Security:
|
|
// - bearer
|
|
//
|
|
// swagger:meta
|
|
package main
|
|
|
|
// @title Tender Management API
|
|
// @version 1.0.0
|
|
// @description This is the API documentation for the Tender Management System.
|
|
// @termsOfService http://swagger.io/terms/
|
|
|
|
// @contact.name API Support
|
|
// @contact.url http://www.swagger.io/support
|
|
// @contact.email support@swagger.io
|
|
|
|
// @license.name Apache 2.0
|
|
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
|
|
|
|
// @host localhost:8081
|
|
// @BasePath /api/v1
|
|
|
|
// @securityDefinitions.apikey BearerAuth
|
|
// @in header
|
|
// @name Authorization
|
|
// @description Type "Bearer" followed by a space and JWT token.
|
|
|
|
// @tag.name customers
|
|
// @tag.description Customer management operations
|
|
|
|
// @tag.name users
|
|
// @tag.description User management operations
|
|
|
|
// @tag.name health
|
|
// @tag.description Health check operations
|
|
|
|
// @tag.name auth
|
|
// @tag.description Authentication operations
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
"tm/internal/customer"
|
|
"tm/internal/user"
|
|
|
|
_ "tm/cmd/web/docs" // This is generated by swag
|
|
)
|
|
|
|
func main() {
|
|
conf := initConfig()
|
|
|
|
logger := initLogger(conf.Logging)
|
|
defer logger.Sync()
|
|
|
|
// Initialize MongoDB connection manager
|
|
mongoManager := initMongoDB(conf.Database.MongoDB, logger)
|
|
defer func() {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
if err := mongoManager.Close(ctx); err != nil {
|
|
logger.Error("Failed to close MongoDB connection", map[string]interface{}{
|
|
"error": err.Error(),
|
|
})
|
|
}
|
|
}()
|
|
|
|
// Initialize Redis connection manager
|
|
redisClient := initRedis(conf.Cache.Redis, logger)
|
|
defer func() {
|
|
if err := redisClient.Close(); err != nil {
|
|
logger.Error("Failed to close Redis connection", map[string]interface{}{
|
|
"error": err.Error(),
|
|
})
|
|
}
|
|
}()
|
|
|
|
// Initialize authorization service
|
|
authService := initAuthorizationService(conf.Auth.JWT, redisClient, logger)
|
|
// Initialize repositories with MongoDB connection manager
|
|
customerRepository := customer.NewCustomerRepository(mongoManager, logger)
|
|
userRepository := user.NewUserRepository(mongoManager, logger)
|
|
|
|
logger.Info("Repositories initialized successfully", map[string]interface{}{
|
|
"repositories": []string{"customer", "user"},
|
|
})
|
|
|
|
// Initialize validation service
|
|
userValidator := user.NewValidationService()
|
|
|
|
// Initialize services with repositories
|
|
customerService := customer.NewCustomerService(customerRepository, logger)
|
|
userService := user.NewUserService(userRepository, logger, authService, userValidator)
|
|
|
|
logger.Info("Services initialized successfully", map[string]interface{}{
|
|
"services": []string{"customer", "user"},
|
|
})
|
|
|
|
// Initialize handlers with services
|
|
customerHandler := customer.NewHandler(customerService)
|
|
userHandler := user.NewUserHandler(userService, logger, userValidator, authService)
|
|
|
|
logger.Info("Handlers initialized successfully", map[string]interface{}{
|
|
"handlers": []string{"customer", "user"},
|
|
})
|
|
|
|
// Initialize HTTP server
|
|
e := initHTTPServer(conf, logger)
|
|
|
|
// Register routes
|
|
customerHandler.RegisterRoutes(e)
|
|
userHandler.RegisterRoutes(e)
|
|
|
|
// Start server
|
|
serverAddr := fmt.Sprintf("%s:%d", conf.Server.Host, conf.Server.Port)
|
|
logger.Info("HTTP server starting", map[string]interface{}{
|
|
"address": serverAddr,
|
|
"version": "1.0.0",
|
|
"host": conf.Server.Host,
|
|
"port": conf.Server.Port,
|
|
})
|
|
|
|
if err := e.Start(serverAddr); err != nil && err != http.ErrServerClosed {
|
|
logger.Error("Failed to start HTTP server", map[string]interface{}{
|
|
"error": err.Error(),
|
|
"address": serverAddr,
|
|
"version": "1.0.0",
|
|
"host": conf.Server.Host,
|
|
"port": conf.Server.Port,
|
|
})
|
|
panic(fmt.Sprintf("Failed to start HTTP server: %v", err))
|
|
}
|
|
}
|