Skip to content
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
4 changes: 2 additions & 2 deletions .golangci.pipeline.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ linters:
- goprintffuncname # checks that printf-like functions are named with f at the end
# - gosec # inspects source code for security problems
- govet # reports suspicious constructs, such as Printf calls whose arguments do not align with the format string
- iface # checks the incorrect use of interfaces, helping developers avoid interface pollution
# - iface # checks the incorrect use of interfaces, helping developers avoid interface pollution
- ineffassign # detects when assignments to existing variables are not used
- intrange # finds places where for loops could make use of an integer range
- loggercheck # checks key value pairs for common logger libraries (kitlog,klog,logr,zap)
Expand All @@ -89,7 +89,7 @@ linters:
- nestif # reports deeply nested if statements
- nilerr # finds the code that returns nil even if it checks that the error is not nil
- nilnesserr # reports that it checks for err != nil, but it returns a different nil value error (powered by nilness and nilerr)
# - nilnil # checks that there is no simultaneous return of nil error and an invalid value
- nilnil # checks that there is no simultaneous return of nil error and an invalid value
- noctx # finds sending http request without context.Context
- nolintlint # reports ill-formed or insufficient nolint directives
- nonamedreturns # reports all named returns
Expand Down
4 changes: 1 addition & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ lint:
install-deps:
GOBIN=$(LOCAL_BIN) go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.36.6
GOBIN=$(LOCAL_BIN) go install -mod=mod google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.71.1
GOBIN=$(LOCAL_BIN) go install github.com/pressly/goose/v3/cmd/goose@latest

generate:
make generate-auth-api
Expand All @@ -28,9 +29,6 @@ generate-auth-api:
LOCAL_MIGRATION_DIR=$(MIGRATION_DIR)
LOCAL_MIGRATION_DSN="host=localhost port=$(POSTGRES_PORT) dbname=$(POSTGRES_DB) user=$(POSTGRES_USER) password=$(POSTGRES_PASSWORD) sslmode=disable"

install-goose:
GOBIN=$(LOCAL_BIN) go install github.com/pressly/goose/v3/cmd/goose@latest

local-migration-status:
$(LOCAL_BIN)/goose -dir ${LOCAL_MIGRATION_DIR} postgres ${LOCAL_MIGRATION_DSN} status -v

Expand Down
31 changes: 19 additions & 12 deletions api/auth_v1/auth.proto
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,32 @@ service AuthV1{
rpc Delete(DeleteRequest) returns (google.protobuf.Empty);
}

enum Role {
ROLE_UNSPECIFIED = 0;
USER = 1;
ADMIN = 2;
message User {
int64 id = 1;
string name = 2;
string email = 3;
Role role = 4;
google.protobuf.Timestamp created_at = 5;
google.protobuf.Timestamp updated_at = 6;
}

message CreateRequest {
message UserInfo {
string name = 1;
string email = 2;
string password = 3;
string password_confirm = 4;
Role role = 5;
}

enum Role {
USER = 0;
ADMIN = 1;
}

message CreateRequest {
UserInfo userInfo = 1;
}

message CreateResponse {
int64 id = 1;
}
Expand All @@ -38,17 +50,12 @@ message GetRequest{
}

message GetResponse {
int64 id = 1;
string name = 2;
string email = 3;
Role role = 4;
google.protobuf.Timestamp created_at = 5;
google.protobuf.Timestamp updated_at = 6;
User user = 1;
}

message UpdateRequest {
int64 id = 1;
google.protobuf.StringValue name = 2;
google.protobuf.StringValue name = 2;
google.protobuf.StringValue email = 3;
}

Expand Down
91 changes: 8 additions & 83 deletions cmd/auth/main.go
Original file line number Diff line number Diff line change
@@ -1,97 +1,22 @@
package main

import (
"flag"
"log/slog"
"net"
"os"
"context"
"log"

"github.com/Str1m/auth/internal/config"
"github.com/Str1m/auth/internal/config/env"
"github.com/Str1m/auth/internal/grpc/auth"
"github.com/Str1m/auth/internal/lib/logger/handlers/slogpretty"
"github.com/Str1m/auth/internal/lib/logger/sl"
"github.com/Str1m/auth/internal/storage/postgres"
desc "github.com/Str1m/auth/pkg/auth_v1"

"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)

const (
envLocal = "local"
envDev = "dev"
envProd = "prod"
"github.com/Str1m/auth/internal/app"
)

func main() {
var cfgPath string
flag.StringVar(&cfgPath, "config-path", ".env", "path to config file")
flag.Parse()

config.MustLoad(cfgPath)
ctx := context.Background()

log := setupLogger()

grpcConfig, err := env.NewGRPCConfig()
a, err := app.NewApp(ctx)
if err != nil {
log.Error("failed to get grpc config", sl.Err(err))
log.Fatalf("failed to init app %s", err.Error())
}

pgConfig, err := env.NewPGConfig()
err = a.Run()
if err != nil {
log.Error("failed to get postgres config", sl.Err(err))
log.Fatalf("failed to run app %s", err.Error())
}

_, err = postgres.New(pgConfig.DSN())
if err != nil {
log.Error("failed to connect to db", sl.Err(err))
}

l, err := net.Listen("tcp", grpcConfig.Addr())
if err != nil {
log.Error("failed to listen", sl.Err(err))
}

s := grpc.NewServer()
reflection.Register(s)

desc.RegisterAuthV1Server(s, &auth.Server{})

log.Info("server listening", slog.String("Addr", grpcConfig.Addr()))

if err = s.Serve(l); err != nil {
log.Error("failed to serve", sl.Err(err))
}
}

func setupLogger() *slog.Logger {
var log *slog.Logger
env := os.Getenv("ENV")
switch env {
case envLocal:
log = setupPrettySlog()
case envDev:
log = slog.New(
slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}),
)
case envProd:
log = slog.New(
slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}),
)
}

return log
}

func setupPrettySlog() *slog.Logger {
opts := slogpretty.PrettyHandlerOptions{
SlogOpts: &slog.HandlerOptions{
Level: slog.LevelDebug,
},
}

handler := opts.NewPrettyHandler(os.Stdout)

return slog.New(handler)
}
11 changes: 7 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ module github.com/Str1m/auth
go 1.24.1

require (
github.com/Masterminds/squirrel v1.5.4
github.com/fatih/color v1.18.0
github.com/jackc/pgx/v5 v5.7.4
github.com/joho/godotenv v1.5.1
golang.org/x/crypto v0.37.0
google.golang.org/grpc v1.65.0
google.golang.org/protobuf v1.36.6
)
Expand All @@ -14,12 +16,13 @@ require (
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
golang.org/x/crypto v0.31.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/sync v0.10.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/text v0.21.0 // indirect
golang.org/x/sync v0.13.0 // indirect
golang.org/x/sys v0.32.0 // indirect
golang.org/x/text v0.24.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240604185151-ef581f913117 // indirect
)
23 changes: 15 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/Masterminds/squirrel v1.5.4 h1:uUcX/aBc8O7Fg9kaISIUsHXdKuqehiXAMQTYX8afzqM=
github.com/Masterminds/squirrel v1.5.4/go.mod h1:NNaOrjSoIDfDA40n7sr2tPNZRfjzjA400rg+riTZj10=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -15,6 +17,10 @@ github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 h1:SOEGU9fKiNWd/HOJuq6+3iTQz8KNCLtVX6idSoTLdUw=
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0/go.mod h1:dXGbAdH5GtBTC4WfIxhKZfyBF/HBFgRZSWwZ9g/He9o=
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 h1:P6pPBnrTSX3DEVR4fDembhRWSsG5rVo6hYhAB/ADZrk=
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0/go.mod h1:vmVJ0l/dxyfGW6FmdpVm2joNMFikkuWg0EoCKLGUMNw=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
Expand All @@ -23,22 +29,23 @@ github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
golang.org/x/crypto v0.37.0 h1:kJNSjF/Xp7kU0iB2Z+9viTPMW4EqqsrywMXLJOOsXSE=
golang.org/x/crypto v0.37.0/go.mod h1:vg+k43peMZ0pUMhYmVAWysMK35e6ioLh3wB8ZCAfbVc=
golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ=
golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.13.0 h1:AauUjRAJ9OSnvULf/ARrrVywoJDy0YS2AwQ98I37610=
golang.org/x/sync v0.13.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
golang.org/x/sys v0.32.0 h1:s77OFDvIQeibCmezSnk/q6iAfkdiQaJi4VzroCFrN20=
golang.org/x/sys v0.32.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/text v0.24.0 h1:dd5Bzh4yt5KYA8f9CJHCP4FB4D51c2c6JvN37xJJkJ0=
golang.org/x/text v0.24.0/go.mod h1:L8rBsPeo2pSS+xqN0d5u2ikmjtmoJbDBT1b7nHvFCdU=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240604185151-ef581f913117 h1:1GBuWVLM/KMVUv1t1En5Gs+gFZCNd360GGb4sSxtrhU=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240604185151-ef581f913117/go.mod h1:EfXuqaE1J41VCDicxHzUDm+8rk+7ZdXzHV0IhO/I6s0=
google.golang.org/grpc v1.65.0 h1:bs/cUb4lp1G5iImFFd3u5ixQzweKizoZJAwBNLR42lc=
Expand Down
20 changes: 20 additions & 0 deletions internal/api/user/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package user

import (
"context"

"github.com/Str1m/auth/internal/converter"
desc "github.com/Str1m/auth/pkg/auth_v1"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

func (i *Implementation) Create(ctx context.Context, req *desc.CreateRequest) (*desc.CreateResponse, error) {
id, err := i.userService.Create(ctx, converter.ToUserInfoFromDesc(req.GetUserInfo()))
if err != nil {
return nil, status.Error(codes.Internal, "failed to create user")
}
return &desc.CreateResponse{
Id: id,
}, nil
}
17 changes: 17 additions & 0 deletions internal/api/user/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package user

import (
"context"

desc "github.com/Str1m/auth/pkg/auth_v1"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
)

func (i *Implementation) Delete(ctx context.Context, req *desc.DeleteRequest) (*emptypb.Empty, error) {
if err := i.userService.Delete(ctx, req.GetId()); err != nil {
return nil, status.Error(codes.Internal, "failed to delete user")
}
return &emptypb.Empty{}, nil
}
18 changes: 18 additions & 0 deletions internal/api/user/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package user

import (
"context"

"github.com/Str1m/auth/internal/converter"
desc "github.com/Str1m/auth/pkg/auth_v1"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

func (i *Implementation) Get(ctx context.Context, req *desc.GetRequest) (*desc.GetResponse, error) {
user, err := i.userService.Get(ctx, req.GetId())
if err != nil {
return nil, status.Error(codes.Internal, "failed to get user")
}
return &desc.GetResponse{User: converter.ToUserFromService(user)}, nil
}
25 changes: 25 additions & 0 deletions internal/api/user/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package user

import (
"context"

modelService "github.com/Str1m/auth/internal/model"
desc "github.com/Str1m/auth/pkg/auth_v1"
)

type Service interface {
Create(ctx context.Context, userInfo *modelService.UserInfo) (int64, error)
Get(ctx context.Context, id int64) (*modelService.User, error)
Update(ctx context.Context, id int64, name *string, email *string) error
Delete(ctx context.Context, id int64) error
}
type Implementation struct {
desc.UnimplementedAuthV1Server
userService Service
}

func NewImplementation(userService Service) *Implementation {
return &Implementation{
userService: userService,
}
}
24 changes: 24 additions & 0 deletions internal/api/user/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package user

import (
"context"

desc "github.com/Str1m/auth/pkg/auth_v1"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
)

func (i *Implementation) Update(ctx context.Context, req *desc.UpdateRequest) (*emptypb.Empty, error) {
var name, email *string
if req.GetName() != nil {
name = &req.Name.Value
}
if req.GetEmail() != nil {
email = &req.Email.Value
}
if err := i.userService.Update(ctx, req.GetId(), name, email); err != nil {
return nil, status.Error(codes.Internal, "failed to update user")
}
return &emptypb.Empty{}, nil
}
Loading