cleanup some shit in auth service
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
# Go parameters
|
# Go parameters
|
||||||
GOCMD=go
|
GOCMD=go
|
||||||
GOBUILD=$(GOCMD) build -trimpath -ldflags="-s -w"
|
GOBUILD=$(GOCMD) build -trimpath -ldflags="-s -w"
|
||||||
GOTEST=$(GOCMD) test
|
GOTEST=$(GOCMD) test
|
||||||
GODOWNLOAD=$(GOCMD) mod download
|
GODOWNLOAD=$(GOCMD) mod download
|
||||||
BINARY_NAME=datarush
|
BINARY_NAME=datarush
|
||||||
@@ -74,23 +74,6 @@ codegen:
|
|||||||
$(GOBUILD) -o ./$(BINARY_DIR)/codegen ./cmd/codegen
|
$(GOBUILD) -o ./$(BINARY_DIR)/codegen ./cmd/codegen
|
||||||
./$(BINARY_DIR)/codegen $(SERVICE) ./
|
./$(BINARY_DIR)/codegen $(SERVICE) ./
|
||||||
|
|
||||||
examples:
|
|
||||||
@echo "📖 Datarush-Go Integration Examples"
|
|
||||||
@echo ""
|
|
||||||
@echo "Files:"
|
|
||||||
@echo " - SQL_BUILDER_ADVANCED.md - Детальные примеры SQL Builder"
|
|
||||||
@echo " - SQL_BUILDER_CODEGEN.md - Документация Codegen"
|
|
||||||
@echo " - SQL_EXAMPLES.sql - SQL схемы и примеры"
|
|
||||||
@echo " - INTEGRATION_EXAMPLES.sh - Интеграционные примеры"
|
|
||||||
@echo ""
|
|
||||||
@echo "Быстрый старт:"
|
|
||||||
@echo " 1. make codegen SERVICE=payment"
|
|
||||||
@echo " 2. Edit internal/payment/repository/postgres/repo.go"
|
|
||||||
@echo " 3. Edit internal/payment/service/service.go"
|
|
||||||
@echo " 4. go test ./internal/payment/..."
|
|
||||||
@echo ""
|
|
||||||
@cat INTEGRATION_EXAMPLES.sh
|
|
||||||
|
|
||||||
|
|
||||||
help:
|
help:
|
||||||
@echo "Available commands:"
|
@echo "Available commands:"
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
}
|
||||||
+2
-2
@@ -4,7 +4,7 @@ services:
|
|||||||
auth:
|
auth:
|
||||||
build:
|
build:
|
||||||
context: .
|
context: .
|
||||||
dockerfile: Containerfile.auth
|
dockerfile: Containerfile
|
||||||
depends_on:
|
depends_on:
|
||||||
postgres:
|
postgres:
|
||||||
restart: false
|
restart: false
|
||||||
@@ -79,7 +79,7 @@ services:
|
|||||||
migrate:
|
migrate:
|
||||||
build:
|
build:
|
||||||
context: .
|
context: .
|
||||||
dockerfile: Containerfile.migrate
|
dockerfile: Containerfile
|
||||||
depends_on:
|
depends_on:
|
||||||
postgres:
|
postgres:
|
||||||
restart: false
|
restart: false
|
||||||
|
|||||||
@@ -3,14 +3,12 @@ package grpc
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"log"
|
"log"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"datarush/internal/auth/domain"
|
"datarush/internal/auth/domain"
|
||||||
"datarush/internal/auth/service"
|
"datarush/internal/auth/service"
|
||||||
pb "datarush/pkg/api/auth"
|
pb "datarush/pkg/api/auth"
|
||||||
|
|
||||||
"google.golang.org/grpc/codes"
|
"google.golang.org/grpc/codes"
|
||||||
"google.golang.org/grpc/metadata"
|
|
||||||
"google.golang.org/grpc/status"
|
"google.golang.org/grpc/status"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -58,18 +56,8 @@ func (h *AuthHandler) SignIn(ctx context.Context, req *pb.SignInRequest) (*pb.Si
|
|||||||
func (h *AuthHandler) ValidateToken(ctx context.Context, req *pb.ValidateTokenRequest) (*pb.ValidateTokenResponse, error) {
|
func (h *AuthHandler) ValidateToken(ctx context.Context, req *pb.ValidateTokenRequest) (*pb.ValidateTokenResponse, error) {
|
||||||
token := req.Token
|
token := req.Token
|
||||||
|
|
||||||
// If token is empty, try to get it from Authorization header (for gRPC-Gateway)
|
|
||||||
if token == "" {
|
if token == "" {
|
||||||
md, ok := metadata.FromIncomingContext(ctx)
|
return nil, status.Error(codes.Unauthenticated, "missing token")
|
||||||
if ok {
|
|
||||||
authHeaders := md.Get("authorization")
|
|
||||||
if len(authHeaders) > 0 {
|
|
||||||
parts := strings.Split(authHeaders[0], " ")
|
|
||||||
if len(parts) == 2 && parts[0] == "Bearer" {
|
|
||||||
token = parts[1]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if token == "" {
|
if token == "" {
|
||||||
|
|||||||
@@ -1,24 +0,0 @@
|
|||||||
package interceptor
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"log"
|
|
||||||
|
|
||||||
"google.golang.org/grpc"
|
|
||||||
)
|
|
||||||
|
|
||||||
type LoggerInterceptor struct{}
|
|
||||||
|
|
||||||
func NewLoggerInterceptor() *LoggerInterceptor {
|
|
||||||
return &LoggerInterceptor{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (li *LoggerInterceptor) UnaryServerInterceptor(
|
|
||||||
ctx context.Context,
|
|
||||||
req interface{},
|
|
||||||
info *grpc.UnaryServerInfo,
|
|
||||||
handler grpc.UnaryHandler,
|
|
||||||
) (interface{}, error) {
|
|
||||||
log.Printf("gRPC call: %s", info.FullMethod)
|
|
||||||
return handler(ctx, req)
|
|
||||||
}
|
|
||||||
@@ -6,7 +6,6 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"datarush/internal/auth/config"
|
"datarush/internal/auth/config"
|
||||||
@@ -43,24 +42,16 @@ func New(cfg *config.Config) *Server {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Start() error {
|
func (s *Server) Start() error {
|
||||||
// Connect to database
|
|
||||||
db, err := sqlx.Connect("postgres", s.config.BuildPostgresConnStr())
|
db, err := sqlx.Connect("postgres", s.config.BuildPostgresConnStr())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to connect to postgres: %w", err)
|
return fmt.Errorf("failed to connect to postgres: %w", err)
|
||||||
}
|
}
|
||||||
s.db = db
|
s.db = db
|
||||||
|
|
||||||
// Create tables
|
|
||||||
if err := s.createTables(); err != nil {
|
|
||||||
return fmt.Errorf("failed to create tables: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Register gRPC services
|
|
||||||
if err := s.registerGRPCServices(); err != nil {
|
if err := s.registerGRPCServices(); err != nil {
|
||||||
return fmt.Errorf("failed to register gRPC services: %w", err)
|
return fmt.Errorf("failed to register gRPC services: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start gRPC server in a goroutine
|
|
||||||
go func() {
|
go func() {
|
||||||
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", s.config.GRPCPort))
|
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", s.config.GRPCPort))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -73,7 +64,6 @@ func (s *Server) Start() error {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// Start HTTP server
|
|
||||||
if err := s.startHTTPServer(); err != nil {
|
if err := s.startHTTPServer(); err != nil {
|
||||||
return fmt.Errorf("failed to start HTTP server: %w", err)
|
return fmt.Errorf("failed to start HTTP server: %w", err)
|
||||||
}
|
}
|
||||||
@@ -84,17 +74,13 @@ func (s *Server) Start() error {
|
|||||||
func (s *Server) registerGRPCServices() error {
|
func (s *Server) registerGRPCServices() error {
|
||||||
s.grpcServer = grpc.NewServer()
|
s.grpcServer = grpc.NewServer()
|
||||||
|
|
||||||
// Create repositories
|
|
||||||
userRepo := authPostgresRepo.NewUserRepository(s.db)
|
userRepo := authPostgresRepo.NewUserRepository(s.db)
|
||||||
|
|
||||||
// Create services
|
|
||||||
authService := service.NewAuthService(userRepo, s.config.JWTSecret)
|
authService := service.NewAuthService(userRepo, s.config.JWTSecret)
|
||||||
|
|
||||||
// Create and register handlers
|
|
||||||
authHandler := grpcHandlers.NewAuthHandler(authService)
|
authHandler := grpcHandlers.NewAuthHandler(authService)
|
||||||
pb.RegisterAuthServiceServer(s.grpcServer, authHandler)
|
pb.RegisterAuthServiceServer(s.grpcServer, authHandler)
|
||||||
|
|
||||||
// Enable reflection if configured
|
|
||||||
if s.config.GRPCEnableReflection {
|
if s.config.GRPCEnableReflection {
|
||||||
reflection.Register(s.grpcServer)
|
reflection.Register(s.grpcServer)
|
||||||
}
|
}
|
||||||
@@ -103,20 +89,14 @@ func (s *Server) registerGRPCServices() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) startHTTPServer() error {
|
func (s *Server) startHTTPServer() error {
|
||||||
// Create repositories
|
|
||||||
userRepo := authPostgresRepo.NewUserRepository(s.db)
|
userRepo := authPostgresRepo.NewUserRepository(s.db)
|
||||||
|
|
||||||
// Create services
|
|
||||||
authService := service.NewAuthService(userRepo, s.config.JWTSecret)
|
authService := service.NewAuthService(userRepo, s.config.JWTSecret)
|
||||||
|
|
||||||
// Create HTTP handlers
|
|
||||||
authHandler := httpHandlers.NewAuthHandler(authService)
|
authHandler := httpHandlers.NewAuthHandler(authService)
|
||||||
userHandler := httpHandlers.NewUserHandler(authService)
|
|
||||||
|
|
||||||
// Create router
|
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
|
|
||||||
// Register routes with middleware wrapper
|
|
||||||
mux.HandleFunc("/api/v1/sign-up", func(w http.ResponseWriter, r *http.Request) {
|
mux.HandleFunc("/api/v1/sign-up", func(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != http.MethodPost {
|
if r.Method != http.MethodPost {
|
||||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
@@ -133,41 +113,6 @@ func (s *Server) startHTTPServer() error {
|
|||||||
authHandler.SignIn(w, r)
|
authHandler.SignIn(w, r)
|
||||||
})
|
})
|
||||||
|
|
||||||
mux.HandleFunc("/api/v1/me", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
if r.Method != http.MethodGet {
|
|
||||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
userHandler.GetMe(w, r)
|
|
||||||
})
|
|
||||||
|
|
||||||
mux.HandleFunc("/api/v1/users/", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
if r.Method != http.MethodGet {
|
|
||||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// Extract user_id from path /api/v1/users/:user_id
|
|
||||||
userID := strings.TrimPrefix(r.URL.Path, "/api/v1/users/")
|
|
||||||
r.Header.Set("X-User-ID", userID)
|
|
||||||
userHandler.GetUser(w, r)
|
|
||||||
})
|
|
||||||
|
|
||||||
mux.HandleFunc("/api/v1/me/stat", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
if r.Method != http.MethodGet {
|
|
||||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
userHandler.GetMyStat(w, r)
|
|
||||||
})
|
|
||||||
|
|
||||||
mux.HandleFunc("/api/v1/leaderboard", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
if r.Method != http.MethodGet {
|
|
||||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
userHandler.GetLeaderboard(w, r)
|
|
||||||
})
|
|
||||||
|
|
||||||
s.httpServer = &http.Server{
|
s.httpServer = &http.Server{
|
||||||
Addr: fmt.Sprintf(":%d", s.config.HTTPPort),
|
Addr: fmt.Sprintf(":%d", s.config.HTTPPort),
|
||||||
Handler: mux,
|
Handler: mux,
|
||||||
@@ -209,21 +154,3 @@ func (s *Server) Stop() {
|
|||||||
|
|
||||||
log.Println("auth server stopped")
|
log.Println("auth server stopped")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) createTables() error {
|
|
||||||
schema := `
|
|
||||||
CREATE TABLE IF NOT EXISTS users (
|
|
||||||
id UUID PRIMARY KEY,
|
|
||||||
email VARCHAR(255) UNIQUE NOT NULL,
|
|
||||||
username VARCHAR(255) NOT NULL,
|
|
||||||
password VARCHAR(255) NOT NULL,
|
|
||||||
created_at TIMESTAMP NOT NULL,
|
|
||||||
updated_at TIMESTAMP NOT NULL
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_users_email ON users(email);
|
|
||||||
`
|
|
||||||
|
|
||||||
_, err := s.db.Exec(schema)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -36,7 +36,6 @@ func NewAuthService(repo UserRepository, jwtSecret string) *AuthService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *AuthService) SignUp(ctx context.Context, email, username, password string) (string, error) {
|
func (s *AuthService) SignUp(ctx context.Context, email, username, password string) (string, error) {
|
||||||
// Check if user already exists
|
|
||||||
_, err := s.repo.GetByEmail(ctx, email)
|
_, err := s.repo.GetByEmail(ctx, email)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return "", domain.ErrUserAlreadyExists
|
return "", domain.ErrUserAlreadyExists
|
||||||
@@ -45,13 +44,11 @@ func (s *AuthService) SignUp(ctx context.Context, email, username, password stri
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hash password
|
|
||||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcryptCost)
|
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcryptCost)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create user
|
|
||||||
user := &domain.User{
|
user := &domain.User{
|
||||||
ID: domain.NewID(),
|
ID: domain.NewID(),
|
||||||
Email: email,
|
Email: email,
|
||||||
@@ -63,7 +60,6 @@ func (s *AuthService) SignUp(ctx context.Context, email, username, password stri
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate token
|
|
||||||
token, err := s.generateToken(user)
|
token, err := s.generateToken(user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
@@ -73,7 +69,6 @@ func (s *AuthService) SignUp(ctx context.Context, email, username, password stri
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *AuthService) SignIn(ctx context.Context, email, password string) (string, error) {
|
func (s *AuthService) SignIn(ctx context.Context, email, password string) (string, error) {
|
||||||
// Get user by email
|
|
||||||
user, err := s.repo.GetByEmail(ctx, email)
|
user, err := s.repo.GetByEmail(ctx, email)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == domain.ErrUserNotFound {
|
if err == domain.ErrUserNotFound {
|
||||||
@@ -82,12 +77,10 @@ func (s *AuthService) SignIn(ctx context.Context, email, password string) (strin
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check password
|
|
||||||
if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password)); err != nil {
|
if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password)); err != nil {
|
||||||
return "", domain.ErrInvalidPassword
|
return "", domain.ErrInvalidPassword
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate token
|
|
||||||
token, err := s.generateToken(user)
|
token, err := s.generateToken(user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
|
|||||||
Reference in New Issue
Block a user