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

Add database migrations and configuration files #3

Merged
merged 2 commits into from
Aug 26, 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ go.work.sum

# env file
.env
local.env
prod.env

/bin/
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
include .env
LOCAL_BIN:=$(CURDIR)/bin

install-deps:
Expand Down
94 changes: 87 additions & 7 deletions cmd/grpc_server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,127 @@ package main

import (
"context"
"fmt"
"errors"
"flag"
"log"
"net"

"github.com/brianvoe/gofakeit"
sq "github.com/Masterminds/squirrel"
"github.com/jackc/pgx/v4/pgxpool"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
"google.golang.org/protobuf/types/known/emptypb"

"github.com/Chuiko-GIT/chat-server/internal/config"
"github.com/Chuiko-GIT/chat-server/internal/config/env"
"github.com/Chuiko-GIT/chat-server/pkg/chat_api"
)

const (
grpcPort = 50052
var (
configPath string
)

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

type server struct {
chat_api.UnimplementedChatApiServer
pool *pgxpool.Pool
}

func (s server) Create(ctx context.Context, req *chat_api.CreateRequest) (*chat_api.CreateResponse, error) {
return &chat_api.CreateResponse{Id: gofakeit.Int64()}, nil
if len(req.Usernames) == 0 {
return &chat_api.CreateResponse{}, errors.New("failed usernames is nil")
}

builder := sq.Insert("chats").
PlaceholderFormat(sq.Dollar).
Columns("usernames").
Values(req.Usernames).
Suffix("RETURNING id")

query, args, err := builder.ToSql()
if err != nil {
return &chat_api.CreateResponse{}, errors.New("failed to build query")
}

var chatID int64
err = s.pool.QueryRow(ctx, query, args...).Scan(&chatID)
if err != nil {
return &chat_api.CreateResponse{}, errors.New("failed to create chat")
}

return &chat_api.CreateResponse{Id: chatID}, nil
}

func (s server) Delete(ctx context.Context, req *chat_api.DeleteRequest) (*emptypb.Empty, error) {
builder := sq.Delete("chats").
PlaceholderFormat(sq.Dollar).
Where("id = $1", req.Id)

query, args, err := builder.ToSql()
if err != nil {
return &emptypb.Empty{}, errors.New("failed to build query")
}

if _, err = s.pool.Exec(ctx, query, args...); err != nil {
return &emptypb.Empty{}, errors.New("failed to delete chat")
}

return &emptypb.Empty{}, nil
}

func (s server) SendMessage(ctx context.Context, req *chat_api.SendMessageRequest) (*emptypb.Empty, error) {
builder := sq.Insert("messages").
PlaceholderFormat(sq.Dollar).
Columns("message_from", "message_text").
Values(req.From, req.Text).
Suffix("RETURNING id")

query, args, err := builder.ToSql()
if err != nil {
return &emptypb.Empty{}, errors.New("failed to build query")
}

if _, err = s.pool.Query(ctx, query, args...); err != nil {
return &emptypb.Empty{}, errors.New("failed to send message")
}
return &emptypb.Empty{}, nil
}

func main() {
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", grpcPort))
flag.Parse()
ctx := context.Background()

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

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

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

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

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

s := grpc.NewServer()
reflection.Register(s)
chat_api.RegisterChatApiServer(s, server{})
chat_api.RegisterChatApiServer(s, server{pool: pool})

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

Expand Down
40 changes: 40 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
version: '3'

volumes:
postgres_volume_local:
postgres_volume_prod:

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

pg-prod:
image: postgres:14-alpine3.17
env_file:
- prod.env
ports:
- "54322:5432"
volumes:
- postgres_volume_prod:/var/lib/postgresql/data

migrator-local:
build:
context: .
dockerfile: migration_local.Dockerfile
restart: on-failure
environment:
DB_HOST: pg-local

migrator-prod:
build:
context: .
dockerfile: migration_prod.Dockerfile
restart: on-failure
environment:
DB_HOST: pg-prod
16 changes: 15 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,31 @@ module github.com/Chuiko-GIT/chat-server
go 1.22.5

require (
github.com/Masterminds/squirrel v1.5.4
github.com/brianvoe/gofakeit v3.18.0+incompatible
github.com/fatih/color v1.17.0
github.com/jackc/pgx/v4 v4.18.3
github.com/joho/godotenv v1.5.1
google.golang.org/grpc v1.65.0
google.golang.org/protobuf v1.34.2
)

require (
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/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/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
golang.org/x/crypto v0.24.0 // indirect
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
)
)
Loading
Loading