- Реализована JWT аутентификация с organization-scope - Добавлено хеширование паролей через bcrypt - Созданы репозитории для организаций и пользователей - Реализован AuthService с бизнес-логикой - Добавлен AuthMiddleware для проверки токенов - Созданы handlers для регистрации и входа - Обновлён API сервер для использования аутентификации Готово для этапа 3 - API структура
198 lines
5.3 KiB
Go
198 lines
5.3 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"net/http"
|
|
|
|
"erp-mvp/core-service/internal/auth"
|
|
"erp-mvp/core-service/internal/config"
|
|
"erp-mvp/core-service/internal/logger"
|
|
"erp-mvp/core-service/internal/repository"
|
|
"erp-mvp/core-service/internal/service"
|
|
"erp-mvp/core-service/internal/api/handlers"
|
|
"erp-mvp/core-service/internal/api/middleware"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type Server struct {
|
|
config *config.Config
|
|
db *sql.DB
|
|
logger logger.Logger
|
|
router *gin.Engine
|
|
|
|
// Services
|
|
authService service.AuthService
|
|
|
|
// Handlers
|
|
authHandler *handlers.AuthHandler
|
|
|
|
// Middleware
|
|
authMiddleware *middleware.AuthMiddleware
|
|
}
|
|
|
|
func NewServer(cfg *config.Config, db *sql.DB, log logger.Logger) *Server {
|
|
// Инициализируем JWT сервис
|
|
jwtService := auth.NewJWTService(cfg.JWT.Secret, cfg.JWT.TTL)
|
|
|
|
// Инициализируем репозитории
|
|
orgRepo := repository.NewOrganizationRepository(db)
|
|
userRepo := repository.NewUserRepository(db)
|
|
|
|
// Инициализируем сервисы
|
|
authService := service.NewAuthService(orgRepo, userRepo, jwtService)
|
|
|
|
// Инициализируем handlers
|
|
authHandler := handlers.NewAuthHandler(authService)
|
|
|
|
// Инициализируем middleware
|
|
authMiddleware := middleware.NewAuthMiddleware(jwtService)
|
|
|
|
server := &Server{
|
|
config: cfg,
|
|
db: db,
|
|
logger: log,
|
|
router: gin.Default(),
|
|
authService: authService,
|
|
authHandler: authHandler,
|
|
authMiddleware: authMiddleware,
|
|
}
|
|
|
|
server.setupRoutes()
|
|
return server
|
|
}
|
|
|
|
func (s *Server) setupRoutes() {
|
|
// Health check
|
|
s.router.GET("/health", s.healthCheck)
|
|
|
|
// API routes
|
|
api := s.router.Group("/api")
|
|
{
|
|
// Auth routes
|
|
auth := api.Group("/auth")
|
|
{
|
|
auth.POST("/register", s.authHandler.Register)
|
|
auth.POST("/login", s.authHandler.Login)
|
|
}
|
|
|
|
// Protected routes
|
|
protected := api.Group("/")
|
|
protected.Use(s.authMiddleware.AuthRequired())
|
|
{
|
|
// Organizations
|
|
protected.GET("/organizations/:id", s.getOrganization)
|
|
protected.PUT("/organizations/:id", s.updateOrganization)
|
|
|
|
// Locations
|
|
protected.GET("/locations", s.getLocations)
|
|
protected.POST("/locations", s.createLocation)
|
|
protected.GET("/locations/:id", s.getLocation)
|
|
protected.PUT("/locations/:id", s.updateLocation)
|
|
protected.DELETE("/locations/:id", s.deleteLocation)
|
|
|
|
// Items
|
|
protected.GET("/items", s.getItems)
|
|
protected.POST("/items", s.createItem)
|
|
protected.GET("/items/:id", s.getItem)
|
|
protected.PUT("/items/:id", s.updateItem)
|
|
protected.DELETE("/items/:id", s.deleteItem)
|
|
|
|
// Operations
|
|
protected.POST("/operations/place-item", s.placeItem)
|
|
protected.POST("/operations/move-item", s.moveItem)
|
|
protected.GET("/operations/search", s.search)
|
|
|
|
// Templates
|
|
protected.GET("/templates", s.getTemplates)
|
|
protected.POST("/templates/:id/apply", s.applyTemplate)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (s *Server) healthCheck(c *gin.Context) {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"status": "ok",
|
|
"service": "erp-mvp-core",
|
|
})
|
|
}
|
|
|
|
// Placeholder handlers - will be implemented in next stages
|
|
func (s *Server) getOrganization(c *gin.Context) {
|
|
c.JSON(http.StatusNotImplemented, gin.H{"error": "Not implemented yet"})
|
|
}
|
|
|
|
func (s *Server) updateOrganization(c *gin.Context) {
|
|
c.JSON(http.StatusNotImplemented, gin.H{"error": "Not implemented yet"})
|
|
}
|
|
|
|
func (s *Server) getLocations(c *gin.Context) {
|
|
c.JSON(http.StatusNotImplemented, gin.H{"error": "Not implemented yet"})
|
|
}
|
|
|
|
func (s *Server) createLocation(c *gin.Context) {
|
|
c.JSON(http.StatusNotImplemented, gin.H{"error": "Not implemented yet"})
|
|
}
|
|
|
|
func (s *Server) getLocation(c *gin.Context) {
|
|
c.JSON(http.StatusNotImplemented, gin.H{"error": "Not implemented yet"})
|
|
}
|
|
|
|
func (s *Server) updateLocation(c *gin.Context) {
|
|
c.JSON(http.StatusNotImplemented, gin.H{"error": "Not implemented yet"})
|
|
}
|
|
|
|
func (s *Server) deleteLocation(c *gin.Context) {
|
|
c.JSON(http.StatusNotImplemented, gin.H{"error": "Not implemented yet"})
|
|
}
|
|
|
|
func (s *Server) getItems(c *gin.Context) {
|
|
c.JSON(http.StatusNotImplemented, gin.H{"error": "Not implemented yet"})
|
|
}
|
|
|
|
func (s *Server) createItem(c *gin.Context) {
|
|
c.JSON(http.StatusNotImplemented, gin.H{"error": "Not implemented yet"})
|
|
}
|
|
|
|
func (s *Server) getItem(c *gin.Context) {
|
|
c.JSON(http.StatusNotImplemented, gin.H{"error": "Not implemented yet"})
|
|
}
|
|
|
|
func (s *Server) updateItem(c *gin.Context) {
|
|
c.JSON(http.StatusNotImplemented, gin.H{"error": "Not implemented yet"})
|
|
}
|
|
|
|
func (s *Server) deleteItem(c *gin.Context) {
|
|
c.JSON(http.StatusNotImplemented, gin.H{"error": "Not implemented yet"})
|
|
}
|
|
|
|
func (s *Server) placeItem(c *gin.Context) {
|
|
c.JSON(http.StatusNotImplemented, gin.H{"error": "Not implemented yet"})
|
|
}
|
|
|
|
func (s *Server) moveItem(c *gin.Context) {
|
|
c.JSON(http.StatusNotImplemented, gin.H{"error": "Not implemented yet"})
|
|
}
|
|
|
|
func (s *Server) search(c *gin.Context) {
|
|
c.JSON(http.StatusNotImplemented, gin.H{"error": "Not implemented yet"})
|
|
}
|
|
|
|
func (s *Server) getTemplates(c *gin.Context) {
|
|
c.JSON(http.StatusNotImplemented, gin.H{"error": "Not implemented yet"})
|
|
}
|
|
|
|
func (s *Server) applyTemplate(c *gin.Context) {
|
|
c.JSON(http.StatusNotImplemented, gin.H{"error": "Not implemented yet"})
|
|
}
|
|
|
|
func (s *Server) Start() error {
|
|
return s.router.Run(s.config.Server.Host + ":" + s.config.Server.Port)
|
|
}
|
|
|
|
func (s *Server) Shutdown(ctx context.Context) error {
|
|
// Graceful shutdown logic will be added later
|
|
return nil
|
|
}
|