feat: завершён этап 1 - Фундамент Core Service

- Удалены зависимости: grpc, redis, prometheus
- Упрощена конфигурация (Server, Database, JWT)
- Создан логгер на основе logrus
- Добавлено подключение к PostgreSQL
- Создана миграция с базовыми таблицами
- Обновлены модели с валидацией
- Создан базовый API сервер с health check
- Добавлен .env.example

Готово для этапа 2 - Аутентификация
This commit is contained in:
2025-08-27 14:40:48 +04:00
parent 725d4c4474
commit 9777114e16
10 changed files with 521 additions and 164 deletions

View File

@@ -0,0 +1,179 @@
package api
import (
"context"
"database/sql"
"net/http"
"erp-mvp/core-service/internal/config"
"erp-mvp/core-service/internal/logger"
"github.com/gin-gonic/gin"
)
type Server struct {
config *config.Config
db *sql.DB
logger logger.Logger
router *gin.Engine
}
func NewServer(cfg *config.Config, db *sql.DB, log logger.Logger) *Server {
server := &Server{
config: cfg,
db: db,
logger: log,
router: gin.Default(),
}
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.register)
auth.POST("/login", s.login)
}
// Protected routes
protected := api.Group("/")
protected.Use(s.authMiddleware())
{
// 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) register(c *gin.Context) {
c.JSON(http.StatusNotImplemented, gin.H{"error": "Not implemented yet"})
}
func (s *Server) login(c *gin.Context) {
c.JSON(http.StatusNotImplemented, gin.H{"error": "Not implemented yet"})
}
func (s *Server) authMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Auth not implemented yet"})
c.Abort()
}
}
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
}

View File

@@ -3,17 +3,15 @@ package config
import (
"os"
"strconv"
"time"
"github.com/joho/godotenv"
)
type Config struct {
Server ServerConfig
Database DatabaseConfig
Redis RedisConfig
JWT JWTConfig
DocumentService DocumentServiceConfig
Log LogConfig
Server ServerConfig
Database DatabaseConfig
JWT JWTConfig
}
type ServerConfig struct {
@@ -30,24 +28,9 @@ type DatabaseConfig struct {
SSLMode string
}
type RedisConfig struct {
Host string
Port string
Password string
DB int
}
type JWTConfig struct {
Secret string
Expiration int // в часах
}
type DocumentServiceConfig struct {
URL string
}
type LogConfig struct {
Level string
Secret string
TTL time.Duration
}
func Load() (*Config, error) {
@@ -67,21 +50,9 @@ func Load() (*Config, error) {
DBName: getEnv("DB_NAME", "erp_mvp"),
SSLMode: getEnv("DB_SSLMODE", "disable"),
},
Redis: RedisConfig{
Host: getEnv("REDIS_HOST", "localhost"),
Port: getEnv("REDIS_PORT", "6379"),
Password: getEnv("REDIS_PASSWORD", ""),
DB: getEnvAsInt("REDIS_DB", 0),
},
JWT: JWTConfig{
Secret: getEnv("JWT_SECRET", "your-secret-key"),
Expiration: getEnvAsInt("JWT_EXPIRATION", 24),
},
DocumentService: DocumentServiceConfig{
URL: getEnv("DOC_SERVICE_URL", "http://localhost:8000"),
},
Log: LogConfig{
Level: getEnv("LOG_LEVEL", "info"),
Secret: getEnv("JWT_SECRET", "your-secret-key"),
TTL: time.Duration(getEnvAsInt("JWT_TTL_HOURS", 24)) * time.Hour,
},
}, nil
}

View File

@@ -0,0 +1,25 @@
package database
import (
"database/sql"
"fmt"
"erp-mvp/core-service/internal/config"
_ "github.com/lib/pq"
)
func Connect(cfg config.DatabaseConfig) (*sql.DB, error) {
dsn := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s",
cfg.Host, cfg.Port, cfg.User, cfg.Password, cfg.DBName, cfg.SSLMode)
db, err := sql.Open("postgres", dsn)
if err != nil {
return nil, fmt.Errorf("failed to open database: %w", err)
}
if err := db.Ping(); err != nil {
return nil, fmt.Errorf("failed to ping database: %w", err)
}
return db, nil
}

View File

@@ -0,0 +1,28 @@
package logger
import (
"os"
"github.com/sirupsen/logrus"
)
type Logger interface {
Info(args ...interface{})
Error(args ...interface{})
Fatal(args ...interface{})
Debug(args ...interface{})
Warn(args ...interface{})
}
type logger struct {
*logrus.Logger
}
func New() Logger {
l := logrus.New()
l.SetOutput(os.Stdout)
l.SetLevel(logrus.InfoLevel)
l.SetFormatter(&logrus.JSONFormatter{})
return &logger{l}
}

View File

@@ -8,47 +8,43 @@ import (
// Organization представляет организацию/компанию
type Organization struct {
ID uuid.UUID `json:"id" db:"id"`
Name string `json:"name" db:"name"`
Type string `json:"type" db:"type"`
Settings map[string]any `json:"settings" db:"settings"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
ID uuid.UUID `json:"id" db:"id"`
Name string `json:"name" validate:"required"`
Type string `json:"type"`
Settings JSON `json:"settings"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
}
// User представляет пользователя системы
type User struct {
ID uuid.UUID `json:"id" db:"id"`
OrganizationID uuid.UUID `json:"organization_id" db:"organization_id"`
Email string `json:"email" db:"email"`
Role string `json:"role" db:"role"`
Email string `json:"email" validate:"required,email"`
PasswordHash string `json:"-" db:"password_hash"`
Role string `json:"role"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
}
// StorageLocation представляет место хранения
type StorageLocation struct {
ID uuid.UUID `json:"id" db:"id"`
OrganizationID uuid.UUID `json:"organization_id" db:"organization_id"`
ParentID *uuid.UUID `json:"parent_id" db:"parent_id"`
Name string `json:"name" db:"name"`
Address string `json:"address" db:"address"`
Type string `json:"type" db:"type"`
Coordinates map[string]any `json:"coordinates" db:"coordinates"`
QRCode string `json:"qr_code" db:"qr_code"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
ID uuid.UUID `json:"id" db:"id"`
OrganizationID uuid.UUID `json:"organization_id" db:"organization_id"`
ParentID *uuid.UUID `json:"parent_id,omitempty" db:"parent_id"`
Name string `json:"name" validate:"required"`
Address string `json:"address" validate:"required"`
Type string `json:"type" validate:"required"`
Coordinates JSON `json:"coordinates"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
}
// Item представляет товар/материал
type Item struct {
ID uuid.UUID `json:"id" db:"id"`
OrganizationID uuid.UUID `json:"organization_id" db:"organization_id"`
Name string `json:"name" db:"name"`
Description string `json:"description" db:"description"`
Category string `json:"category" db:"category"`
Name string `json:"name" validate:"required"`
Description string `json:"description"`
Category string `json:"category"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
}
// ItemPlacement представляет размещение товара в месте хранения
@@ -57,53 +53,64 @@ type ItemPlacement struct {
OrganizationID uuid.UUID `json:"organization_id" db:"organization_id"`
ItemID uuid.UUID `json:"item_id" db:"item_id"`
LocationID uuid.UUID `json:"location_id" db:"location_id"`
Quantity int `json:"quantity" db:"quantity"`
Quantity int `json:"quantity" validate:"min=1"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
}
// JSON тип для JSON полей
type JSON map[string]interface{}
// LoginRequest запрос на аутентификацию
type LoginRequest struct {
Email string `json:"email" binding:"required,email"`
Password string `json:"password" binding:"required"`
Email string `json:"email" validate:"required,email"`
Password string `json:"password" validate:"required"`
}
// RegisterRequest запрос на регистрацию
type RegisterRequest struct {
OrganizationName string `json:"organization_name" validate:"required"`
UserEmail string `json:"user_email" validate:"required,email"`
UserPassword string `json:"user_password" validate:"required,min=8"`
OrganizationType string `json:"organization_type"`
}
// LoginResponse ответ на аутентификацию
type LoginResponse struct {
Token string `json:"token"`
RefreshToken string `json:"refresh_token"`
User User `json:"user"`
ExpiresAt time.Time `json:"expires_at"`
Token string `json:"token"`
User User `json:"user"`
ExpiresAt time.Time `json:"expires_at"`
}
// CreateLocationRequest запрос на создание места хранения
type CreateLocationRequest struct {
Name string `json:"name" binding:"required"`
Address string `json:"address" binding:"required"`
Type string `json:"type" binding:"required"`
ParentID *uuid.UUID `json:"parent_id"`
Coordinates map[string]any `json:"coordinates"`
Name string `json:"name" validate:"required"`
Address string `json:"address" validate:"required"`
Type string `json:"type" validate:"required"`
ParentID *uuid.UUID `json:"parent_id"`
Coordinates JSON `json:"coordinates"`
}
// CreateItemRequest запрос на создание товара
type CreateItemRequest struct {
Name string `json:"name" binding:"required"`
Name string `json:"name" validate:"required"`
Description string `json:"description"`
Category string `json:"category" binding:"required"`
Category string `json:"category"`
}
// PlaceItemRequest запрос на размещение товара
type PlaceItemRequest struct {
ItemID uuid.UUID `json:"item_id" binding:"required"`
LocationID uuid.UUID `json:"location_id" binding:"required"`
Quantity int `json:"quantity" binding:"required,min=1"`
ItemID uuid.UUID `json:"item_id" validate:"required"`
LocationID uuid.UUID `json:"location_id" validate:"required"`
Quantity int `json:"quantity" validate:"required,min=1"`
}
// SearchRequest запрос на поиск
type SearchRequest struct {
Query string `json:"query" binding:"required"`
Category string `json:"category"`
LocationID *uuid.UUID `json:"location_id"`
Query string `form:"q"`
Category string `form:"category"`
Address string `form:"address"`
Page int `form:"page,default=1"`
PageSize int `form:"page_size,default=20"`
}
// SearchResponse результат поиска
@@ -114,7 +121,7 @@ type SearchResponse struct {
// ItemWithLocation товар с информацией о месте размещения
type ItemWithLocation struct {
Item Item `json:"item"`
Item Item `json:"item"`
Location StorageLocation `json:"location"`
Quantity int `json:"quantity"`
Quantity int `json:"quantity"`
}