-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4a1d6cd
commit da9718a
Showing
25 changed files
with
1,120 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
pull-rabbitmq: | ||
docker pull rabbitmq:3-management | ||
|
||
run-rabbitmq: | ||
docker run -d --name my-rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3-management | ||
|
||
run-postgres: | ||
docker run --name habr-pg-13.3 -p 5432:5432 -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=daee -d postgres:13.3 | ||
docker run --name habr-pg-13.3 -p 5432:5432 -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=daee -d postgres:13.3 | ||
|
||
generate: | ||
cd ./backend/internal/protos && protoc -I proto proto/daee/daee.proto \ | ||
--go_out=./gen/go --go_opt=paths=source_relative \ | ||
--go-grpc_out=./gen/go --go-grpc_opt=paths=source_relative |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package grpcapp | ||
|
||
import ( | ||
"fmt" | ||
"log/slog" | ||
"net" | ||
|
||
authgrpc "github.com/Prrromanssss/DAEE-fullstack/internal/grpc/auth" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
type App struct { | ||
log *slog.Logger | ||
gRPCServer *grpc.Server | ||
port int | ||
} | ||
|
||
// MustRun runs gRPC server and panics if any error occurs. | ||
func (a *App) MustRun() { | ||
if err := a.Run(); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
// New creates new gRPC server app. | ||
func New( | ||
log *slog.Logger, | ||
authService authgrpc.Auth, | ||
port int, | ||
) *App { | ||
gRPCServer := grpc.NewServer() | ||
|
||
authgrpc.Register(gRPCServer, authService) | ||
|
||
return &App{ | ||
log: log, | ||
gRPCServer: gRPCServer, | ||
port: port, | ||
} | ||
} | ||
|
||
func (a *App) Run() error { | ||
const op = "grpcapp.Run" | ||
|
||
log := a.log.With( | ||
slog.String("op", op), | ||
slog.Int("port", a.port), | ||
) | ||
|
||
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", a.port)) | ||
if err != nil { | ||
return fmt.Errorf("%s: %w", op, err) | ||
} | ||
log.Info("gRPC server is running", slog.String("addr", lis.Addr().String())) | ||
|
||
if err := a.gRPCServer.Serve(lis); err != nil { | ||
return fmt.Errorf("%s: %w", op, err) | ||
} | ||
return nil | ||
} | ||
|
||
// Stop stops gRPC server. | ||
func (a *App) Stop() { | ||
const op = "grpcapp.Stop" | ||
|
||
a.log.With(slog.String("op", op)). | ||
Info("stopping gRPC server", slog.Int("port", a.port)) | ||
|
||
a.gRPCServer.GracefulStop() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package auth | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
daeev1 "github.com/Prrromanssss/DAEE-fullstack/internal/protos/gen/go/daee" | ||
"github.com/Prrromanssss/DAEE-fullstack/internal/services/auth" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
type Auth interface { | ||
Login( | ||
ctx context.Context, | ||
email string, | ||
password string, | ||
) (token string, err error) | ||
RegisterNewUser( | ||
ctx context.Context, | ||
email string, | ||
password string, | ||
) (userID int64, err error) | ||
} | ||
|
||
type serverAPI struct { | ||
daeev1.UnimplementedAuthServer | ||
auth Auth | ||
} | ||
|
||
func Register(gRPC *grpc.Server, auth Auth) { | ||
daeev1.RegisterAuthServer(gRPC, &serverAPI{auth: auth}) | ||
} | ||
|
||
func (s *serverAPI) Login( | ||
ctx context.Context, | ||
req *daeev1.LoginRequest, | ||
) (*daeev1.LoginResponse, error) { | ||
if err := validateLogin(req); err != nil { | ||
return nil, err | ||
} | ||
|
||
token, err := s.auth.Login(ctx, req.GetEmail(), req.GetPassword()) | ||
if err != nil { | ||
if errors.Is(err, auth.ErrInvalidCredentials) { | ||
return nil, status.Error(codes.InvalidArgument, "invalid email or password") | ||
} | ||
return nil, status.Error(codes.Internal, "internal error") | ||
} | ||
|
||
return &daeev1.LoginResponse{ | ||
Token: token, | ||
}, nil | ||
} | ||
|
||
func (s *serverAPI) Register( | ||
ctx context.Context, | ||
req *daeev1.RegisterRequest, | ||
) (*daeev1.RegisterResponse, error) { | ||
if err := validateRegister(req); err != nil { | ||
return nil, err | ||
} | ||
|
||
userID, err := s.auth.RegisterNewUser(ctx, req.GetEmail(), req.GetPassword()) | ||
if err != nil { | ||
if errors.Is(err, auth.ErrUserExists) { | ||
return nil, status.Error(codes.AlreadyExists, "user already exists") | ||
} | ||
return nil, status.Error(codes.Internal, "internal error") | ||
} | ||
|
||
return &daeev1.RegisterResponse{ | ||
UserId: userID, | ||
}, nil | ||
} | ||
|
||
func validateLogin(req *daeev1.LoginRequest) error { | ||
if req.GetEmail() == "" { | ||
return status.Error(codes.InvalidArgument, "email is required") | ||
} | ||
|
||
if req.GetPassword() == "" { | ||
return status.Error(codes.InvalidArgument, "password is required") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func validateRegister(req *daeev1.RegisterRequest) error { | ||
if req.GetEmail() == "" { | ||
return status.Error(codes.InvalidArgument, "email is required") | ||
} | ||
|
||
if req.GetPassword() == "" { | ||
return status.Error(codes.InvalidArgument, "password is required") | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package jwt | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"time" | ||
|
||
"github.com/Prrromanssss/DAEE-fullstack/internal/storage/postgres" | ||
"github.com/golang-jwt/jwt/v5" | ||
"github.com/joho/godotenv" | ||
) | ||
|
||
func NewToken(user postgres.User, duration time.Duration) (string, error) { | ||
err := godotenv.Load("local.env") | ||
if err != nil { | ||
log.Fatalf("can't parse env file: %v", err) | ||
} | ||
|
||
jwtSecret := os.Getenv("JWT_SECRET") | ||
if jwtSecret == "" { | ||
log.Fatal("JWT_SECRET is not set") | ||
} | ||
|
||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ | ||
"uid": user.UserID, | ||
"email": user.Email, | ||
"exp": time.Now().Add(duration).Unix(), | ||
}) | ||
|
||
tokenString, err := token.SignedString([]byte(jwtSecret)) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return tokenString, nil | ||
} |
Oops, something went wrong.