-
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.
release/38851105: Validation server side and docker-compose
- Loading branch information
Showing
26 changed files
with
481 additions
and
217 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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# [Задача №00000000](https://bigbosop215.kaiten.ru/space/439413/card/00000000) | ||
|
||
### Что было не так: | ||
<описание проблемы/причины заведения новой фичи> | ||
### Что было сделано: | ||
* слой repository; | ||
* добавлена валидация на уникальность email; | ||
* ручка на получение подборок фильмов; | ||
* написаны тесты и моки. | ||
### Чек лист до ревью : | ||
- [ ] PR разбит на логические коммиты (если нет, то нужно разбить); | ||
- [ ] PR не содержит секретов (пароли, токены, ключи); | ||
- [ ] PR не содержит лишних файлов (например, `.env`); | ||
- [ ] PR не содержит коммитов, которые не относятся к задаче; | ||
|
||
**Перед тем, как отдать на ревью нужно убедиться, что все пункты выполнены** | ||
|
||
### как вливать ветку | ||
Если это обычная задача, то просто `squash merge` в `develop` | ||
Если это хотфикс, то обычный `merge` в `master` и `develop` (должно быть два `pull request`). При этом в ветке должен быть один коммит | ||
Если это релиз, то обычный `merge` и в `master` и в `develop`. Там будет несколько коммитов |
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,44 @@ | ||
version: '3' | ||
|
||
volumes: | ||
postgres_volume: | ||
redis: | ||
|
||
services: | ||
validation_ms: | ||
build: | ||
context: ./validation-service/ | ||
dockerfile: Dockerfile | ||
restart: always | ||
ports: | ||
- 5050:5050 | ||
networks: | ||
- netflix | ||
redis: | ||
image: 'redis:latest' | ||
command: redis-server | ||
ports: | ||
- '6379:6379' | ||
volumes: | ||
- redis:/data | ||
networks: | ||
- netflix | ||
db: | ||
image: postgres:latest | ||
environment: | ||
- "POSTGRES_DB=netflix" | ||
- "POSTGRES_USER=test" | ||
- "POSTGRES_PASSWORD=test" | ||
ports: | ||
- "5432:5432" | ||
volumes: | ||
- "postgres_volume:/var/lib/postgresql/data" | ||
networks: | ||
- netflix | ||
|
||
|
||
networks: | ||
netflix: | ||
default: | ||
external: | ||
name: netflix |
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 |
---|---|---|
@@ -1,5 +1,21 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
|
||
"github.com/go-park-mail-ru/2024_2_GOATS/validation-service/internal/app" | ||
) | ||
|
||
const serverPort = 5050 | ||
|
||
func main() { | ||
|
||
ctx := context.Background() | ||
a, err := app.New(ctx, serverPort) | ||
|
||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
a.Run() | ||
} |
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,16 @@ | ||
module github.com/go-park-mail-ru/2024_2_GOATS | ||
module github.com/go-park-mail-ru/2024_2_GOATS/validation-service | ||
|
||
go 1.22.2 | ||
|
||
require ( | ||
google.golang.org/grpc v1.65.0 | ||
google.golang.org/protobuf v1.34.2 | ||
) | ||
|
||
require ( | ||
golang.org/x/net v0.26.0 // indirect | ||
golang.org/x/sys v0.21.0 // indirect | ||
golang.org/x/text v0.16.0 // indirect | ||
google.golang.org/genproto/googleapis/rpc v0.0.0-20240604185151-ef581f913117 // indirect | ||
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.5.1 // indirect | ||
google.golang.org/protobuf v1.34.2 // indirect | ||
) |
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
26 changes: 26 additions & 0 deletions
26
validation-service/internal/app/api/converter/converter.go
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 +1,27 @@ | ||
package converter | ||
|
||
import ( | ||
"github.com/go-park-mail-ru/2024_2_GOATS/validation-service/internal/app/model" | ||
desc "github.com/go-park-mail-ru/2024_2_GOATS/validation-service/internal/pb/validation" | ||
"google.golang.org/protobuf/types/known/timestamppb" | ||
) | ||
|
||
func ToUserRegisterDataFromDesc(data *desc.ValidateRegistrationRequest) *model.UserRegisterData { | ||
birthday := data.GetBitrhdate().AsTime() | ||
ts := timestamppb.New(birthday) | ||
|
||
return &model.UserRegisterData{ | ||
Email: data.GetEmail(), | ||
Password: data.GetPassword(), | ||
PasswordConfirm: data.GetPasswordConfirm(), | ||
Sex: data.GetSex(), | ||
Birthday: int(ts.Seconds), | ||
} | ||
} | ||
|
||
func ToErrorsFromServ(data *model.ErrorResponse) *desc.ErrorMessage { | ||
return &desc.ErrorMessage{ | ||
Code: data.Code, | ||
Error: data.Error, | ||
} | ||
} |
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 +1,17 @@ | ||
package handler | ||
package validation | ||
|
||
import ( | ||
desc "github.com/go-park-mail-ru/2024_2_GOATS/validation-service/internal/pb/validation" | ||
"github.com/go-park-mail-ru/2024_2_GOATS/validation-service/internal/app/service" | ||
) | ||
|
||
type Implementation struct { | ||
desc.UnimplementedValidationServer | ||
validationService service.ValidationService | ||
} | ||
|
||
func NewImplementation(validationService service.ValidationService) *Implementation { | ||
return &Implementation{ | ||
validationService: validationService, | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
validation-service/internal/app/api/validation/validation.go
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,21 @@ | ||
package validation | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/go-park-mail-ru/2024_2_GOATS/validation-service/internal/app/api/converter" | ||
desc "github.com/go-park-mail-ru/2024_2_GOATS/validation-service/internal/pb/validation" | ||
) | ||
|
||
func (i *Implementation) ValidateRegistration(ctx context.Context, req *desc.ValidateRegistrationRequest) (*desc.ValidationResponse, error) { | ||
validData := i.validationService.ValidateRegistration(ctx, converter.ToUserRegisterDataFromDesc(req)) | ||
descErrors := make([]*desc.ErrorMessage, 0) | ||
for _, errData := range validData.Errors { | ||
descErrors = append(descErrors, converter.ToErrorsFromServ(&errData)) | ||
} | ||
|
||
return &desc.ValidationResponse{ | ||
Success: validData.Success, | ||
Errors: descErrors, | ||
}, 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 |
---|---|---|
@@ -1,3 +1,49 @@ | ||
package main | ||
package app | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"net" | ||
|
||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/reflection" | ||
|
||
validationAPI "github.com/go-park-mail-ru/2024_2_GOATS/validation-service/internal/app/api/validation" | ||
validationService "github.com/go-park-mail-ru/2024_2_GOATS/validation-service/internal/app/service/validation" | ||
serv "github.com/go-park-mail-ru/2024_2_GOATS/validation-service/internal/app/service" | ||
desc "github.com/go-park-mail-ru/2024_2_GOATS/validation-service/internal/pb/validation" | ||
) | ||
|
||
type App struct { | ||
serverPort int | ||
validationService serv.ValidationService | ||
} | ||
|
||
func New(ctx context.Context, serverPort int) (*App, error) { | ||
validationService := validationService.NewService() | ||
|
||
return &App{ | ||
serverPort: serverPort, | ||
validationService: validationService, | ||
}, nil | ||
} | ||
|
||
func (a *App) Run() { | ||
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", a.serverPort)) | ||
if err != nil { | ||
log.Fatal("Failed to listen: %v", err) | ||
} | ||
|
||
validationSrv := validationService.NewService() | ||
s := grpc.NewServer() | ||
reflection.Register(s) | ||
|
||
desc.RegisterValidationServer(s, validationAPI.NewImplementation(validationSrv)) | ||
|
||
log.Printf("Server listening at %v", lis.Addr()) | ||
|
||
if err = s.Serve(lis); err != nil { | ||
log.Fatalf("failed to serve: %v", err) | ||
} | ||
} |
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,12 @@ | ||
package errors | ||
|
||
var ( | ||
ErrInvalidEmailCode = "invalid_email" | ||
ErrInvalidEmailText = "email is incorrect" | ||
ErrInvalidPasswordCode = "invalid_password" | ||
ErrInvalidPasswordText = "password is too short. The minimal len is 8" | ||
ErrInvalidSexCode = "invalid_sex" | ||
ErrInvalidSexText = "only male or female allowed" | ||
ErrInvalidBirthdateCode = "invalid_birthdate" | ||
ErrInvalidBirthdateText = "bithdate should be before current time" | ||
) |
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 +1,19 @@ | ||
package model | ||
|
||
type UserRegisterData struct { | ||
Email string | ||
Password string | ||
PasswordConfirm string | ||
Sex string | ||
Birthday int | ||
} | ||
|
||
type ErrorResponse struct { | ||
Code string | ||
Error string | ||
} | ||
|
||
type ValidationResponse struct { | ||
Success bool | ||
Errors []ErrorResponse | ||
} |
1 change: 0 additions & 1 deletion
1
validation-service/internal/app/repository/converter/converter.go
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
validation-service/internal/app/repository/validation/registration_validation.go
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 +1,11 @@ | ||
package service | ||
|
||
import ( | ||
"context" | ||
"github.com/go-park-mail-ru/2024_2_GOATS/validation-service/internal/app/model" | ||
) | ||
|
||
|
||
type ValidationService interface { | ||
ValidateRegistration(ctx context.Context, userData *model.UserRegisterData) *model.ValidationResponse | ||
} |
59 changes: 59 additions & 0 deletions
59
validation-service/internal/app/service/validation/helpers/user_helper.go
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,59 @@ | ||
package helpers | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"regexp" | ||
"slices" | ||
"time" | ||
|
||
errVals "github.com/go-park-mail-ru/2024_2_GOATS/validation-service/internal/app/errors" | ||
"google.golang.org/protobuf/types/known/timestamppb" | ||
) | ||
|
||
var ( | ||
emailRegex = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$") | ||
passwordLength = 8 | ||
sexVals = []string{"male", "female"} | ||
) | ||
|
||
func ValidatePassword(pass, passConf string) error { | ||
if passConf == "" { | ||
log.Println("password confirm is missing, but now its OK") | ||
} | ||
|
||
if len(pass) < passwordLength { | ||
log.Println(errVals.ErrInvalidPasswordText) | ||
return fmt.Errorf(errVals.ErrInvalidPasswordText) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func ValidateEmail(email string) error { | ||
if !emailRegex.MatchString(email) { | ||
log.Println(errVals.ErrInvalidEmailText) | ||
return fmt.Errorf(errVals.ErrInvalidEmailText) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func ValidateBirthdate(birthdate int) error { | ||
ts := timestamppb.New(time.Now()) | ||
if int(ts.Seconds) < birthdate { | ||
log.Println(errVals.ErrInvalidBirthdateCode) | ||
return fmt.Errorf(errVals.ErrInvalidBirthdateText) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func ValidateSex(sex string) error { | ||
if !slices.Contains(sexVals, sex) { | ||
log.Println(errVals.ErrInvalidSexCode) | ||
return fmt.Errorf(errVals.ErrInvalidSexText) | ||
} | ||
|
||
return nil | ||
} |
1 change: 0 additions & 1 deletion
1
validation-service/internal/app/service/validation/registartion_validation.go
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.