Skip to content

Commit

Permalink
Merge branch 'week_4'
Browse files Browse the repository at this point in the history
  • Loading branch information
olezhek28 committed Jul 30, 2024
2 parents 80a9e03 + 2b1cedc commit 2f2fdb1
Show file tree
Hide file tree
Showing 83 changed files with 2,983 additions and 55 deletions.
17 changes: 17 additions & 0 deletions week_4/clean_redis/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
POSTGRES_DB=note
POSTGRES_USER=note-user
POSTGRES_PASSWORD=note-password
MIGRATION_DIR=./migrations

PG_DSN="host=localhost port=54321 dbname=note user=note-user password=note-password sslmode=disable"

GRPC_HOST=localhost
GRPC_PORT=50051

REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_CONNECTION_TIMEOUT_SEC=5
REDIS_MAX_IDLE=10
REDIS_IDLE_TIMEOUT_SEC=300

STORAGE_MODE=pg
33 changes: 33 additions & 0 deletions week_4/clean_redis/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
include .env
LOCAL_BIN:=$(CURDIR)/bin

install-deps:
GOBIN=$(LOCAL_BIN) go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28.1
GOBIN=$(LOCAL_BIN) go install -mod=mod google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2
GOBIN=$(LOCAL_BIN) go install github.com/pressly/goose/v3/cmd/goose@v3.14.0

get-deps:
go get -u google.golang.org/protobuf/cmd/protoc-gen-go
go get -u google.golang.org/grpc/cmd/protoc-gen-go-grpc


generate:
make generate-note-api

generate-note-api:
mkdir -p pkg/note_v1
protoc --proto_path api/note_v1 \
--go_out=pkg/note_v1 --go_opt=paths=source_relative \
--plugin=protoc-gen-go=bin/protoc-gen-go \
--go-grpc_out=pkg/note_v1 --go-grpc_opt=paths=source_relative \
--plugin=protoc-gen-go-grpc=bin/protoc-gen-go-grpc \
api/note_v1/note.proto

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

local-migration-up:
$(LOCAL_BIN)/goose -dir ${MIGRATION_DIR} postgres ${PG_DSN} up -v

local-migration-down:
$(LOCAL_BIN)/goose -dir ${MIGRATION_DIR} postgres ${PG_DSN} down -v
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import "google/protobuf/timestamp.proto";
import "google/protobuf/empty.proto";
import "google/protobuf/wrappers.proto";

option go_package = "github.com/olezhek28/microservices_course/week_4/config/pkg/note_v1;note_v1";
option go_package = "github.com/olezhek28/microservices_course/week_4/clean_redis/pkg/note_v1;note_v1";

service NoteV1 {
rpc Create(CreateRequest) returns (CreateResponse);
Expand Down
Binary file added week_4/clean_redis/bin/goose
Binary file not shown.
File renamed without changes.
File renamed without changes.
22 changes: 22 additions & 0 deletions week_4/clean_redis/cmd/grpc_server/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"context"
"log"

"github.com/olezhek28/microservices_course/week_4/clean_redis/internal/app"
)

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

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

err = a.Run()
if err != nil {
log.Fatalf("failed to run app: %s", err.Error())
}
}
23 changes: 23 additions & 0 deletions week_4/clean_redis/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
version: '3'

volumes:
postgres_volume:
redis-data:

services:
pg-local:
image: postgres:14-alpine3.17
env_file:
- .env
ports:
- "54321:5432"
volumes:
- postgres_volume:/var/lib/postgresql/data

redis:
image: redis:7.2.5-alpine3.20
container_name: redis-container
ports:
- "6379:6379"
volumes:
- redis-data:/data
34 changes: 34 additions & 0 deletions week_4/clean_redis/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module github.com/olezhek28/microservices_course/week_4/clean_redis

go 1.22.4

require (
github.com/Masterminds/squirrel v1.5.4
github.com/gomodule/redigo v1.9.2
github.com/joho/godotenv v1.5.1
github.com/olezhek28/platform_common v0.0.0-20230822195735-04af626dd264
github.com/pkg/errors v0.9.1
google.golang.org/grpc v1.57.0
google.golang.org/protobuf v1.34.1
)

require (
github.com/georgysavva/scany v1.2.1 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.14.3 // 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-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgtype v1.14.0 // indirect
github.com/jackc/pgx/v4 v4.18.2 // 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
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/net v0.21.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-20230525234030-28d5490b6b19 // indirect
)
259 changes: 259 additions & 0 deletions week_4/clean_redis/go.sum

Large diffs are not rendered by default.

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

import (
"context"
"log"

"github.com/olezhek28/microservices_course/week_4/clean_redis/internal/converter"
desc "github.com/olezhek28/microservices_course/week_4/clean_redis/pkg/note_v1"
)

func (i *Implementation) Create(ctx context.Context, req *desc.CreateRequest) (*desc.CreateResponse, error) {
id, err := i.noteService.Create(ctx, converter.ToNoteInfoFromDesc(req.GetInfo()))
if err != nil {
return nil, err
}

log.Printf("inserted note with id: %d", id)

return &desc.CreateResponse{
Id: id,
}, nil
}
22 changes: 22 additions & 0 deletions week_4/clean_redis/internal/api/note/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package note

import (
"context"
"log"

"github.com/olezhek28/microservices_course/week_4/clean_redis/internal/converter"
desc "github.com/olezhek28/microservices_course/week_4/clean_redis/pkg/note_v1"
)

func (i *Implementation) Get(ctx context.Context, req *desc.GetRequest) (*desc.GetResponse, error) {
noteObj, err := i.noteService.Get(ctx, req.GetId())
if err != nil {
return nil, err
}

log.Printf("id: %d, title: %s, body: %s, created_at: %v, updated_at: %v\n", noteObj.ID, noteObj.Info.Title, noteObj.Info.Content, noteObj.CreatedAt, noteObj.UpdatedAt)

return &desc.GetResponse{
Note: converter.ToNoteFromService(noteObj),
}, nil
}
17 changes: 17 additions & 0 deletions week_4/clean_redis/internal/api/note/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package note

import (
"github.com/olezhek28/microservices_course/week_4/clean_redis/internal/service"
desc "github.com/olezhek28/microservices_course/week_4/clean_redis/pkg/note_v1"
)

type Implementation struct {
desc.UnimplementedNoteV1Server
noteService service.NoteService
}

func NewImplementation(noteService service.NoteService) *Implementation {
return &Implementation{
noteService: noteService,
}
}
97 changes: 97 additions & 0 deletions week_4/clean_redis/internal/app/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package app

import (
"context"
"log"
"net"

"github.com/olezhek28/platform_common/pkg/closer"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/reflection"

"github.com/olezhek28/microservices_course/week_4/clean_redis/internal/config"
desc "github.com/olezhek28/microservices_course/week_4/clean_redis/pkg/note_v1"
)

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

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

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

return a, nil
}

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 {
err := config.Load(".env")
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.RegisterNoteV1Server(a.grpcServer, a.serviceProvider.NoteImpl(ctx))

return nil
}

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

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

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

return nil
}
Loading

0 comments on commit 2f2fdb1

Please sign in to comment.