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
8 changes: 8 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Service ChatRoom
APP_ENV_SERVICE=production
PORT_SERVICE=8080
DB_HOST=localhost
DB_PORT=5432
DB_USER=postgres
DB_PASSWORD=5430
DB_NAME=postgres
32 changes: 32 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Code coverage profiles and other test artifacts
*.out
coverage.*
*.coverprofile
profile.cov

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work
go.work.sum

# env file
.env

# Editor/IDE
# .idea/
# .vscode/
21 changes: 21 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
version: '3'

tasks:
grpc:
cmds:
- protoc -I ./proto --go_out=./gen/go/v1 --go_opt=paths=source_relative ./proto/chatroom/chatroom.proto
- protoc -I ./proto --go-grpc_out=./gen/go/v1 --go-grpc_opt=paths=source_relative ./proto/chatroom/chatroom.proto

grpc-gateway:
cmds:
- protoc -I ./proto --go_out=./gen/go/v1 --go_opt=paths=source_relative ./proto/chatroom/chatroom.proto
- protoc -I ./proto --go-grpc_out=./gen/go/v1 --go-grpc_opt=paths=source_relative ./proto/chatroom/chatroom.proto
- protoc -I ./proto --grpc-gateway_out=./gen/go/v1 --grpc-gateway_opt=paths=source_relative ./proto/chatroom/chatroom.proto

swagger:
cmds:
- protoc -I ./proto --openapiv2_out=allow_merge=true,merge_file_name=api:./gen/go/v1/chatroom/swagger ./proto/chatroom/chatroom.proto

run-chat-service:
cmds:
- cd ./chat-service && go run ./cmd/server/main.go
22 changes: 15 additions & 7 deletions chat-service/cmd/server/main.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
package main

import (
"log"
"context"
"log/slog"

"github.com/Lemper29/ChatRoom/chat-service/config"
"github.com/Lemper29/ChatRoom/chat-service/internal/logger"
"github.com/Lemper29/ChatRoom/chat-service/internal/server"
"github.com/Lemper29/ChatRoom/chat-service/internal/storage/db"
"gorm.io/driver/postgres"
)

func main() {
cfg := postgres.Config{
DSN: "host=localhost user=postgres password=postgres dbname=postgres port=5432 sslmode=disable",
cfg := config.Envs
ctx := context.Background()

logger := logger.New("chat-service", slog.LevelDebug)

cfgPostgres := postgres.Config{
DSN: cfg.DSN,
PreferSimpleProtocol: true,
}

storage, err := db.NewPostgresStorage(cfg)
storage, err := db.NewPostgresStorage(cfgPostgres, logger)
if err != nil {
log.Fatalf("DB error: %v", err)
logger.ErrorContext(ctx, "Database error", "error", err)
}

server := server.NewGrpcServer(":8080", storage)
server := server.NewGrpcServer(":"+cfg.Port, storage, logger)

if err := server.Server(); err != nil {
log.Fatalf("Server error: %v", err)
logger.ErrorContext(ctx, "Server error", "error", err)
}
}
62 changes: 62 additions & 0 deletions chat-service/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package config

import (
"fmt"
"log"
"os"
"path/filepath"
"runtime"

"github.com/joho/godotenv"
)

type Config struct {
Port string
DBHost string
DSN string
Env string
}

var Envs = Init()

func Init() *Config {

projectRoot := getProjectRoot()
envPath := filepath.Join(projectRoot, ".env")

if err := godotenv.Load(envPath); err != nil {
log.Printf("No .env file found at %s, using environment variables\n", envPath)
} else {
log.Printf("Successfully loaded .env from %s\n", envPath)
}

appEnv := getOrDefault("APP_ENV_SERVICE", "development")
port := getOrDefault("PORT_SERVICE", "8080")
dbHost := getOrDefault("DB_HOST", "localhost")
dbPort := getOrDefault("DB_PORT", "5432")
dbUser := getOrDefault("DB_USER", "postgres")
dbPassword := getOrDefault("DB_PASSWORD", "5430")
dbName := getOrDefault("DB_NAME", "postgres")

return &Config{
Env: appEnv,
Port: port,
DSN: fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
dbHost, dbPort, dbUser, dbPassword, dbName),
}
}

func getOrDefault(varName string, defaultValue string) string {
value := os.Getenv(varName)
if value == "" {
return defaultValue
}
return value
}

func getProjectRoot() string {
_, filename, _, _ := runtime.Caller(0)

projectRoot := filepath.Join(filepath.Dir(filename), "..", "..")
return projectRoot
}
100 changes: 88 additions & 12 deletions chat-service/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,110 @@ module github.com/Lemper29/ChatRoom/chat-service
go 1.25.1

require (
github.com/Lemper29/ChatRoom v0.0.0-20251007122616-713807057ca8
github.com/stretchr/testify v1.8.1
google.golang.org/grpc v1.75.1
gorm.io/gorm v1.25.10
github.com/Lemper29/ChatRoom v0.0.0-20251010092257-e1511b5af19e
github.com/stretchr/testify v1.11.1
google.golang.org/grpc v1.76.0
gorm.io/gorm v1.31.0
)

require (
dario.cat/mergo v1.0.2 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cloudflare/cfssl v1.6.4 // indirect
github.com/containerd/cgroups/v3 v3.0.5 // indirect
github.com/containerd/errdefs v1.0.0 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/distribution/reference v0.6.0 // indirect
github.com/docker/distribution v2.8.3+incompatible // indirect
github.com/docker/go-connections v0.6.0 // indirect
github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c // indirect
github.com/docker/go-metrics v0.0.1 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/certificate-transparency-go v1.1.4 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/mux v1.8.1 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.1 // indirect
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
github.com/hashicorp/go-memdb v1.3.2 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/ishidawataru/sctp v0.0.0-20250829011129-4b890084db30 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/pgx/v5 v5.6.0 // indirect
github.com/jackc/pgx/v5 v5.7.6 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/jmoiron/sqlx v1.3.3 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/moby/buildkit v0.25.1 // indirect
github.com/moby/docker-image-spec v1.3.1 // indirect
github.com/moby/swarmkit/v2 v2.1.1 // indirect
github.com/moby/sys/atomicwriter v0.1.0 // indirect
github.com/moby/sys/sequential v0.6.0 // indirect
github.com/moby/sys/userns v0.1.0 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.1 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.22.0 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.62.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/rogpeppe/go-internal v1.14.1 // indirect
github.com/stretchr/objx v0.5.0 // indirect
golang.org/x/crypto v0.39.0 // indirect
golang.org/x/sync v0.15.0 // indirect
github.com/rootless-containers/rootlesskit/v2 v2.3.5 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spf13/pflag v1.0.10 // indirect
github.com/stretchr/objx v0.5.3 // indirect
github.com/vishvananda/netlink v1.3.1 // indirect
github.com/vishvananda/netns v0.0.5 // indirect
github.com/weppos/publicsuffix-go v0.15.1-0.20210511084619-b1f36a2d6c0b // indirect
github.com/zmap/zcrypto v0.0.0-20210511125630-18f1e0152cfc // indirect
github.com/zmap/zlint/v3 v3.1.0 // indirect
go.etcd.io/bbolt v1.4.3 // indirect
go.etcd.io/etcd/raft/v3 v3.5.6 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.63.0 // indirect
go.opentelemetry.io/contrib/processors/baggagecopy v0.11.0 // indirect
go.opentelemetry.io/otel v1.38.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.35.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.35.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.35.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.35.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.35.0 // indirect
go.opentelemetry.io/otel/log v0.14.0 // indirect
go.opentelemetry.io/otel/metric v1.38.0 // indirect
go.opentelemetry.io/otel/sdk v1.38.0 // indirect
go.opentelemetry.io/otel/sdk/log v0.14.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.38.0 // indirect
go.opentelemetry.io/otel/trace v1.38.0 // indirect
go.opentelemetry.io/proto/otlp v1.5.0 // indirect
golang.org/x/crypto v0.43.0 // indirect
golang.org/x/sync v0.17.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20251014184007-4626949a642f // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/klog/v2 v2.90.1 // indirect
)

require (
golang.org/x/net v0.41.0 // indirect
golang.org/x/sys v0.33.0 // indirect
golang.org/x/text v0.26.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20250707201910-8d1bb00bc6a7 // indirect
github.com/docker/docker v28.5.1+incompatible
github.com/joho/godotenv v1.5.1
golang.org/x/net v0.46.0 // indirect
golang.org/x/sys v0.37.0 // indirect
golang.org/x/text v0.30.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20251007200510-49b9836ed3ff // indirect
google.golang.org/protobuf v1.36.10 // indirect
gorm.io/driver/postgres v1.6.0
)
Loading