Skip to content

Commit

Permalink
Implemented lgic
Browse files Browse the repository at this point in the history
  • Loading branch information
artemzi committed Sep 17, 2024
1 parent d26e447 commit 6536a30
Show file tree
Hide file tree
Showing 18 changed files with 719 additions and 145 deletions.
7 changes: 6 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@ PG_DATABASE_NAME=auth
PG_USER=auth
PG_PASSWORD=secret
PG_PORT=54321
MIGRATION_DIR=./migrations
MIGRATION_DIR=./migrations

PG_DSN="host=localhost port=54321 dbname=auth user=auth password=secret sslmode=disable"

GRPC_HOST=localhost
GRPC_PORT=50051
146 changes: 5 additions & 141 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,157 +2,21 @@ package main

import (
"context"
"flag"
"net"
"strconv"
"time"

"github.com/artemzi/auth/internal/config"
desc "github.com/artemzi/auth/pkg/user_v1"
"github.com/fatih/color"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/artemzi/auth/internal/app"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
"google.golang.org/protobuf/types/known/emptypb"
"google.golang.org/protobuf/types/known/timestamppb"
)

var configPath string

func init() {
flag.StringVar(&configPath, "config-path", ".env", "path to config file")
}

type server struct {
desc.UnimplementedUserAPIV1Server
pool *pgxpool.Pool
}

func (s *server) Create(ctx context.Context, req *desc.CreateRequest) (*desc.CreateResponse, error) {
query := "INSERT INTO \"user\" (name, email, password, password_confirm, role) VALUES ($1, $2, $3, $4, $5) RETURNING id;"

var userID int64
err := s.pool.QueryRow(
ctx,
query,
req.GetInfo().Name, req.GetInfo().Email, req.GetInfo().Password, req.GetInfo().PasswordConfirm, req.GetInfo().Role).
Scan(&userID)
if err != nil {
log.Errorf("failed to INSERT user: %v", err)
return nil, err
}

log.WithContext(ctx).Infof("inserted user with id: %d", userID)

return &desc.CreateResponse{
Id: userID,
}, nil

}

func (s *server) Get(ctx context.Context, req *desc.GetRequest) (*desc.GetResponse, error) {
query := "SELECT id, name, email, role, created_at, updated_at FROM \"user\" WHERE id = $1;"

var id int64
var name, email, role string
var createdAt, updatedAt time.Time

err := s.pool.QueryRow(ctx, query, req.GetId()).Scan(&id, &name, &email, &role, &createdAt, &updatedAt)
if err != nil {
log.Errorf("failed to GET user: %v", err)
return nil, err
}

log.WithContext(ctx).Info(color.GreenString("Got User id: "), req.GetId())

roleVal, err := strconv.Atoi(role)
if err != nil {
log.Errorf("failed to parse role: %v", err)
return nil, err
}

return &desc.GetResponse{
User: &desc.User{
Id: req.GetId(),
Info: &desc.UserInfo{
Name: name,
Email: email,
Role: desc.Role(roleVal),
},
CreatedAt: timestamppb.New(createdAt),
UpdatedAt: timestamppb.New(updatedAt),
},
}, nil
}

func (s *server) Update(ctx context.Context, req *desc.UpdateRequest) (*emptypb.Empty, error) {
query := "UPDATE \"user\" SET name = $1, email = $2 WHERE id = $3;"

_, err := s.pool.Exec(ctx, query, req.GetInfo().GetName().Value, req.GetInfo().GetEmail().Value, req.GetId())
if err != nil {
log.Errorf("failed to UPDATE user: %v", err)
return nil, err
}

log.WithContext(ctx).Info(color.GreenString("Updated User id: "), req.GetId())
out := new(emptypb.Empty)

return out, nil
}

func (s *server) Delete(ctx context.Context, req *desc.DeleteRequest) (*emptypb.Empty, error) {
query := "DELETE FROM \"user\" WHERE id = $1;"

_, err := s.pool.Exec(ctx, query, req.GetId())
if err != nil {
log.Errorf("failed to DELETE user: %v", err)
return nil, err
}

log.WithContext(ctx).Info(color.GreenString("Deleted User id: "), req.GetId())
out := new(emptypb.Empty)

return out, nil
}

func main() {
flag.Parse()
ctx := context.Background()

err := config.Load(configPath)
a, err := app.NewApp(ctx)
if err != nil {
log.Fatalf("Failed to load config: %v", err)
log.Fatalf("failed to init app: %s", err.Error())
}

grpcConfig, err := config.NewGRPCConfig()
err = a.Run()
if err != nil {
log.Fatalf("Failed to load config: %v", err)
}

pgConfig, err := config.NewPGConfig()
if err != nil {
log.Fatalf("failed to get pg config: %v", err)
}

lis, err := net.Listen("tcp", grpcConfig.Address())
if err != nil {
log.Fatalf("failed to listen: %v", err)
}

pool, err := pgxpool.Connect(ctx, pgConfig.DSN())
if err != nil {
log.Fatalf("failed to connect to database: %v", err)
}
defer pool.Close()

s := grpc.NewServer()
reflection.Register(s)
desc.RegisterUserAPIV1Server(s, &server{pool: pool})

log.Info(color.GreenString("server listening at "), lis.Addr())

if err = s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
log.Fatalf("failed to run app: %s", err.Error())
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.23
require (
github.com/Masterminds/squirrel v1.5.4
github.com/fatih/color v1.17.0
github.com/georgysavva/scany v1.2.2
github.com/jackc/pgconn v1.14.3
github.com/jackc/pgx/v4 v4.18.3
github.com/joho/godotenv v1.5.1
Expand Down
Loading

0 comments on commit 6536a30

Please sign in to comment.