Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: hw3 #3

Merged
merged 3 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 5 additions & 62 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,78 +3,21 @@ package main
import (
"context"
"flag"
"fmt"
"log"
"net"

sq "github.com/Masterminds/squirrel"
_ "github.com/jackc/pgx/stdlib"
"github.com/jackc/pgx/v5"
"github.com/markgenuine/auth/internal/app/auth_v1"
"github.com/markgenuine/auth/internal/config"
"github.com/markgenuine/auth/internal/config/env"
desc "github.com/markgenuine/auth/pkg/auth_v1"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
"github.com/markgenuine/auth/internal/app"
)

var configPath string

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

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

fmt.Println(configPath)
err := config.Load(configPath)
if err != nil {
log.Fatalf("failed to load config: %v", err)
}

grpcConfig, err := env.NewGRPCConfig()
if err != nil {
log.Fatalf("failed to get grpc config: %s ", err.Error())
}

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

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

pool, err := pgx.Connect(ctx, pgConfig.DSN())
a, err := app.NewApp(ctx)
if err != nil {
log.Fatalf("failed to connect to db: %s", err.Error())
log.Fatalf("failed to init app: %s", err.Error())
}

defer func() {
err = pool.Close(ctx)
if err != nil {
log.Fatal(err)
}
}()

s := grpc.NewServer()
reflection.Register(s)
desc.RegisterUserV1Server(
s,
auth_v1.NewUserService(pool, sq.StatementBuilder.PlaceholderFormat(sq.Dollar)),
)

if err != nil {
log.Fatalf("failed start DB postgres: %v", err)
}

log.Printf("server listening at %v", list.Addr())

if err = s.Serve(list); err != nil {
log.Fatalf("failed to serve: %v", err)
if err := a.Run(); err != nil {
log.Fatalf("failed to run app: %s", err.Error())
}
}
16 changes: 11 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,33 @@ go 1.22.0
require (
github.com/Masterminds/squirrel v1.5.4
github.com/brianvoe/gofakeit v2.2.0+incompatible
github.com/georgysavva/scany v1.2.1
github.com/golang/protobuf v1.5.3
github.com/jackc/pgconn v1.14.3
github.com/jackc/pgx v3.6.2+incompatible
github.com/jackc/pgx/v4 v4.18.3
github.com/jackc/pgx/v5 v5.5.3
github.com/joho/godotenv v1.5.1
github.com/pkg/errors v0.9.1
google.golang.org/grpc v1.61.0
google.golang.org/protobuf v1.32.0
)

require (
github.com/cockroachdb/apd v1.1.0 // indirect
github.com/gofrs/uuid v4.4.0+incompatible // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/fake v0.0.0-20150926172116-812a484cc733 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.3 // indirect
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect
github.com/jackc/pgtype v1.14.0 // indirect
github.com/jackc/puddle v1.3.0 // indirect
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect
github.com/lib/pq v1.2.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
golang.org/x/crypto v0.19.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/crypto v0.20.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231106174013-bbf56f31fb17 // indirect
Expand Down
205 changes: 200 additions & 5 deletions go.sum

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions internal/api/auth/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package auth

import (
"context"

"github.com/markgenuine/auth/internal/converter"
desc "github.com/markgenuine/auth/pkg/auth_v1"
)

// Create user in auth-service
func (s *Implementation) Create(ctx context.Context, request *desc.CreateRequest) (*desc.CreateResponse, error) {
id, err := s.authService.Create(ctx, converter.CreateUserToServiceFromUser(request))
if err != nil {
return nil, err
}

return converter.CreateUserToUserFromService(id), nil
}
18 changes: 18 additions & 0 deletions internal/api/auth/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package auth

import (
"context"

"github.com/golang/protobuf/ptypes/empty"
desc "github.com/markgenuine/auth/pkg/auth_v1"
)

// Delete user from auth-service
func (s *Implementation) Delete(ctx context.Context, request *desc.DeleteRequest) (*empty.Empty, error) {
err := s.authService.Delete(ctx, request.GetId())
if err != nil {
return nil, err
}

return &empty.Empty{}, nil
}
18 changes: 18 additions & 0 deletions internal/api/auth/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package auth

import (
"context"

"github.com/markgenuine/auth/internal/converter"
desc "github.com/markgenuine/auth/pkg/auth_v1"
)

// Get user of ID
func (s *Implementation) Get(ctx context.Context, request *desc.GetRequest) (*desc.GetResponse, error) {
user, err := s.authService.Get(ctx, request.GetId())
if err != nil {
return nil, err
}

return converter.GetToUserFromService(user), nil
}
19 changes: 19 additions & 0 deletions internal/api/auth/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package auth

import (
"github.com/markgenuine/auth/internal/service"
desc "github.com/markgenuine/auth/pkg/auth_v1"
)

// Implementation - type for proto implementation
type Implementation struct {
desc.UnimplementedUserV1Server
authService service.AuthService
}

// NewImplementation create proto interface implementation
func NewImplementation(authService service.AuthService) *Implementation {
return &Implementation{
authService: authService,
}
}
19 changes: 19 additions & 0 deletions internal/api/auth/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package auth

import (
"context"

"github.com/golang/protobuf/ptypes/empty"
"github.com/markgenuine/auth/internal/converter"
desc "github.com/markgenuine/auth/pkg/auth_v1"
)

// Update user of ID
func (s *Implementation) Update(ctx context.Context, request *desc.UpdateRequest) (*empty.Empty, error) {
err := s.authService.Update(ctx, converter.UpdateToServiceFromUser(request))
if err != nil {
return nil, err
}

return &empty.Empty{}, nil
}
107 changes: 107 additions & 0 deletions internal/app/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package app

import (
"context"
"flag"
"log"
"net"

"github.com/markgenuine/auth/internal/closer"
"github.com/markgenuine/auth/internal/config"
desc "github.com/markgenuine/auth/pkg/auth_v1"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/reflection"
)

// App ...
type App struct {
serviceProvider *serviceProvider
grpcServer *grpc.Server
}

var configPath string

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

// NewApp ...
func NewApp(ctx context.Context) (*App, error) {
a := &App{}

err := a.initDeps(ctx)
if err != nil {
return nil, err
}

return a, nil
}

// Run ...
func (a *App) Run() error {
defer func() {
closer.CloseAll()
closer.Wait()
}()

return a.runGRPCServer()
}

func (a *App) initDeps(ctx context.Context) error {
inits := []func(context.Context) error{
a.initConfig,
a.initServiceProvider,
a.initGRPCServer,
}

for _, f := range inits {
err := f(ctx)
if err != nil {
return err
}
}

return nil
}

func (a *App) initConfig(_ context.Context) error {
flag.Parse()
err := config.Load(configPath)

if err != nil {
return err
}

return nil
}

func (a *App) initServiceProvider(_ context.Context) error {
a.serviceProvider = newServiceProvider()

return nil
}

func (a *App) initGRPCServer(ctx context.Context) error {
a.grpcServer = grpc.NewServer(grpc.Creds(insecure.NewCredentials()))

reflection.Register(a.grpcServer)
desc.RegisterUserV1Server(a.grpcServer, a.serviceProvider.UserImpl(ctx))

return nil
}

func (a *App) runGRPCServer() error {
log.Printf("GRPC server start: %s", a.serviceProvider.GRPCConfig().Address())

list, err := net.Listen("tcp", a.serviceProvider.GRPCConfig().Address())
if err != nil {
return err
}

if err = a.grpcServer.Serve(list); err != nil {
return err
}

return nil
}
38 changes: 0 additions & 38 deletions internal/app/auth_v1/create.go

This file was deleted.

Loading
Loading