Skip to content

Commit

Permalink
docker-compose
Browse files Browse the repository at this point in the history
  • Loading branch information
Prrromanssss committed Apr 25, 2024
1 parent d855f85 commit b5a0475
Show file tree
Hide file tree
Showing 21 changed files with 250 additions and 130 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
frontend/node_modules/
backend/vendor
sqlc.yaml
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ vendor/
# Go workspace file
go.work
.env
local.env
local.yaml

# Logs
logs
Expand Down
7 changes: 1 addition & 6 deletions backend/cmd/agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ import (
"os"
"os/signal"
"syscall"
"time"

agentapp "github.com/Prrromanssss/DAEC-fullstack/internal/app/agent"
"github.com/Prrromanssss/DAEC-fullstack/internal/config"
"github.com/Prrromanssss/DAEC-fullstack/internal/lib/logger/logcleaner"
"github.com/Prrromanssss/DAEC-fullstack/internal/lib/logger/setup"
"github.com/Prrromanssss/DAEC-fullstack/internal/storage"
)
Expand All @@ -23,15 +21,12 @@ func main() {
cfg := config.MustLoad()

// Configuration Logger
log := setup.SetupLogger(cfg.Env, cfg.LogPathAgent)
log := setup.SetupLogger(cfg.Env)
log.Info(
"start agent",
slog.String("env", cfg.Env),
slog.String("version", "2"),
)
log.Debug("debug messages are enabled")

go logcleaner.CleanLog(10*time.Minute, cfg.LogPathAgent, 100)

// Configuration Storage
dbCfg := storage.NewStorage(log, cfg.StorageURL)
Expand Down
2 changes: 1 addition & 1 deletion backend/cmd/auth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func main() {
cfg := config.MustLoad()

// Configuration Logger
log := setup.SetupLogger(cfg.Env, cfg.LogPathAuth)
log := setup.SetupLogger(cfg.Env)
log.Info(
"start grpc server",
slog.String("env", cfg.Env),
Expand Down
7 changes: 1 addition & 6 deletions backend/cmd/orchestrator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"os"
"os/signal"
"syscall"
"time"

orchestratorapp "github.com/Prrromanssss/DAEC-fullstack/internal/app/orchestrator"
daecv1 "github.com/Prrromanssss/DAEC-fullstack/internal/protos/gen/go/daec"
Expand All @@ -17,7 +16,6 @@ import (
"github.com/Prrromanssss/DAEC-fullstack/internal/config"
"github.com/Prrromanssss/DAEC-fullstack/internal/http-server/handlers"
mwlogger "github.com/Prrromanssss/DAEC-fullstack/internal/http-server/middleware/logger"
"github.com/Prrromanssss/DAEC-fullstack/internal/lib/logger/logcleaner"
"github.com/Prrromanssss/DAEC-fullstack/internal/lib/logger/setup"
"github.com/Prrromanssss/DAEC-fullstack/internal/lib/logger/sl"
"github.com/Prrromanssss/DAEC-fullstack/internal/storage"
Expand All @@ -35,15 +33,12 @@ func main() {
cfg := config.MustLoad()

// Configuration Logger
log := setup.SetupLogger(cfg.Env, cfg.LogPathOrchestrator)
log := setup.SetupLogger(cfg.Env)
log.Info(
"start orchestrator",
slog.String("env", cfg.Env),
slog.String("version", "2"),
)
log.Debug("debug messages are enabled")

go logcleaner.CleanLog(10*time.Minute, cfg.LogPathOrchestrator, 100)

// Configuration Storage
dbCfg := storage.NewStorage(log, cfg.StorageURL)
Expand Down
12 changes: 5 additions & 7 deletions backend/config/local.yaml
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
env: "local"
log_path_agent: "../../log/agent.log"
log_path_orchestrator: "../../log/orchestrator.log"
log_path_auth: "../../log/auth.log"
inactive_time_for_agent: 20
time_for_ping: 10
tokenTTL: 1h
grpc_server:
address: "localhost:44044"
address: ":44044"
grpc_client_connection_string: "auth:44044"
rabbit_queue:
rabbitmq_url: "amqp://guest:guest@localhost:5672/"
rabbitmq_url: "amqp://guest:guest@rabbitmq:5672/"
queue_for_expressions_to_agents: "Expressions to agents"
queue_for_results_from_agents: "Results from agents"
http_server:
address: "localhost:3000"
address: ":3000"
timeout: 4s
idle_timeout: 60s
database_instance:
goose_migration_dir: "./backend/sql/schema"
storage_url: "postgres://postgres:6,62607004@localhost:5434/daec?sslmode=disable"
storage_url: "postgres://postgres:postgres@db:5432/daec?sslmode=disable"
2 changes: 1 addition & 1 deletion backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.21.1
require (
github.com/go-chi/chi v1.5.5
github.com/go-chi/cors v1.2.1
github.com/joho/godotenv v1.5.1
github.com/joho/godotenv v1.5.1 // indirect
)

require github.com/lib/pq v1.10.9
Expand Down
19 changes: 2 additions & 17 deletions backend/internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
package config

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

"github.com/ilyakaznacheev/cleanenv"
"github.com/joho/godotenv"
)

type Config struct {
Env string `yaml:"env" env:"ENV" env-default:"local"`
LogPathAgent string `yaml:"log_path_agent" env-required:"true"`
LogPathOrchestrator string `yaml:"log_path_orchestrator" env-required:"true"`
LogPathAuth string `yaml:"log_path_auth" env-required:"true"`
InactiveTimeForAgent int32 `yaml:"inactive_time_for_agent" env-default:"200"`
TimeForPing int32 `yaml:"time_for_ping" end-default:"100"`
TokenTTL time.Duration `yaml:"tokenTTL" env-default:"1h"`
Expand Down Expand Up @@ -44,20 +38,11 @@ type DatabaseInstance struct {
}

type GRPCServer struct {
Address string `yaml:"address" env-default:"localhost:44044"`
Address string `yaml:"address" env-default:"localhost:44044"`
GRPCClientConnectionString string `yaml:"grpc_client_connection_string" env-default:"auth:44044"`
}

func MustLoad() *Config {
path, err := os.Getwd()
if err != nil {
log.Fatalf("can't get pwd: %v", err)
}

err = godotenv.Load(fmt.Sprintf("%s/local.env", filepath.Dir(filepath.Dir(filepath.Dir(path)))))
if err != nil {
log.Fatalf("can't parse env file: %v", err)
}

configPath := os.Getenv("CONFIG_PATH")
if configPath == "" {
log.Fatal("CONFIG_PATH is not set")
Expand Down
12 changes: 0 additions & 12 deletions backend/internal/lib/jwt/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,15 @@ import (
"log"
"net/http"
"os"
"path/filepath"
"strings"
"time"

"github.com/Prrromanssss/DAEC-fullstack/internal/storage/postgres"
"github.com/golang-jwt/jwt/v5"
"github.com/joho/godotenv"
)

// NewToken creates new JWT token.
func NewToken(user postgres.User, duration time.Duration) (string, error) {
path, err := os.Getwd()
if err != nil {
log.Fatalf("can't get pwd: %v", err)
}

err = godotenv.Load(fmt.Sprintf("%s/local.env", filepath.Dir(filepath.Dir(filepath.Dir(path)))))
if err != nil {
log.Fatalf("can't parse env file: %v", err)
}

jwtSecret := os.Getenv("JWT_SECRET")
if jwtSecret == "" {
log.Fatal("JWT_SECRET is not set")
Expand Down
61 changes: 0 additions & 61 deletions backend/internal/lib/logger/logcleaner/logcleaner.go

This file was deleted.

17 changes: 4 additions & 13 deletions backend/internal/lib/logger/setup/logger.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package setup

import (
"io"
"log/slog"
"os"
)
Expand All @@ -12,29 +11,21 @@ const (
envProd = "prod"
)

func SetupLogger(env, logPath string) *slog.Logger {
func SetupLogger(env string) *slog.Logger {
var log *slog.Logger

logFile, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
panic("failed to open log file: " + err.Error())
}
defer logFile.Close()

writer := io.Writer(logFile)

switch env {
case envLocal:
log = SetupPrettySlog(writer)
log = SetupPrettySlog()
case envDev:
log = slog.New(
slog.NewJSONHandler(writer, &slog.HandlerOptions{
slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelDebug,
}),
)
case envProd:
log = slog.New(
slog.NewJSONHandler(writer, &slog.HandlerOptions{
slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelInfo,
}),
)
Expand Down
3 changes: 1 addition & 2 deletions backend/internal/lib/logger/setup/pretty_logger.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package setup

import (
"io"
"log/slog"
"os"

"github.com/Prrromanssss/DAEC-fullstack/internal/lib/logger/handlers/slogpretty"
)

func SetupPrettySlog(logFile io.Writer) *slog.Logger {
func SetupPrettySlog() *slog.Logger {
opts := slogpretty.PrettyHandlerOptions{
SlogOpts: &slog.HandlerOptions{
Level: slog.LevelDebug,
Expand Down
60 changes: 60 additions & 0 deletions backend/sql/daec.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
DROP TYPE IF EXISTS agent_status;
CREATE TYPE agent_status AS ENUM ('running', 'waiting', 'sleeping', 'terminated');

CREATE TABLE IF NOT EXISTS agents (
agent_id int GENERATED ALWAYS AS IDENTITY,
number_of_parallel_calculations int NOT NULL DEFAULT 5,
last_ping timestamp NOT NULL,
status agent_status NOT NULL,

PRIMARY KEY(agent_id)
);

ALTER TABLE agents ADD COLUMN created_at timestamp NOT NULL;

CREATE TABLE IF NOT EXISTS users (
user_id int GENERATED ALWAYS AS IDENTITY,
email text UNIQUE NOT NULL,
password_hash bytea NOT NULL,

PRIMARY KEY(user_id)
);

DROP TYPE IF EXISTS expression_status;
CREATE TYPE expression_status AS ENUM ('ready_for_computation', 'computing', 'result', 'terminated');

CREATE TABLE IF NOT EXISTS expressions (
expression_id int GENERATED ALWAYS AS IDENTITY,
user_id int NOT NULL,
agent_id int,
created_at timestamp NOT NULL,
updated_at timestamp NOT NULL,
data text NOT NULL,
parse_data text NOT NULL,
status expression_status NOT NULL,
result int NOT NULL DEFAULT 0,
is_ready boolean NOT NULL DEFAULT false,

PRIMARY KEY(expression_id),
FOREIGN KEY(agent_id)
REFERENCES agents(agent_id)
ON DELETE SET NULL,
FOREIGN KEY(user_id)
REFERENCES users(user_id)
ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS operations (
operation_id int GENERATED ALWAYS AS IDENTITY,
operation_type varchar(1) NOT NULL,
execution_time int NOT NULL DEFAULT 100,
user_id int NOT NULL,

PRIMARY KEY(operation_id),
CONSTRAINT operation_type_user_id UNIQUE(operation_type, user_id),
FOREIGN KEY(user_id)
REFERENCES users(user_id)
ON DELETE CASCADE
);

ALTER TABLE agents ADD COLUMN number_of_active_calculations int NOT NULL DEFAULT 0;
Loading

0 comments on commit b5a0475

Please sign in to comment.