Skip to content

Commit

Permalink
🔥 wip: enhance structure
Browse files Browse the repository at this point in the history
  • Loading branch information
zcubbs committed Oct 15, 2023
1 parent c371629 commit 559a463
Show file tree
Hide file tree
Showing 31 changed files with 172 additions and 432 deletions.
70 changes: 35 additions & 35 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,20 @@ vars:
# sqlite connection string
TEST_DB_URL: "file:./db.sqlite?cache=shared&mode=rwc"
LOCAL_HOST: 127.0.0.1
GO_PACKAGE: github.com/zcubbs/linkup
GO_PACKAGE_SHORT: linkup
GO_PACKAGE: github.com/zcubbs/tlz
GO_PACKAGE_SHORT: tlz

tasks:
build:
desc: Build Go Binaries
cmds:
- go build -o ./bin/agent ./cmd/agent
- go build -o ./bin/server ./cmd/server
- go build -o ./bin/cli ./cmd/cli

build:cli:
desc: Build the CLI
cmds:
- task: build:cli:{{OS}}

build:cli:windows:
desc: Build the CLI
cmds:
- go build -o ./bin/linkup.exe ./cmd/cli

build:docker:server:
desc: Build the Docker image for the server
cmds:
- docker build -t server:latest -f build/server/Dockerfile .

run:server:
desc: Run the server
cmds:
- cmd: go run .\cmd\server\main.go

run:registry:
desc: Run the registry
cmds:
- cmd: go run .\cmd\registry\main.go

run:docker:server:
desc: Run the server inside a Docker container
cmds:
- docker run -p 50051:50051 server:latest

migrate:new:
desc: "Generate migration. Usage: task migrate:new -- <migration_name>"
cmds:
Expand Down Expand Up @@ -113,11 +86,6 @@ tasks:
cmds:
- cmd: sqlc generate

evans:
desc: Run Evans CLI
cmds:
- evans --host localhost --port 9000 -r repl

lint:
desc: Run linter
cmds:
Expand All @@ -127,7 +95,6 @@ tasks:
desc: Generate mocks
cmds:
- task: mock:server:db
- task: mock:cli:rpc

mock:server:db:
desc: Generate mocks for server db
Expand Down Expand Up @@ -217,6 +184,20 @@ tasks:
cmds:
- scoop install gcc

containers:up:
desc: Start all containers
cmds:
- task: pg
- task: redis
- task: mailpit

containers:down:
desc: Stop all containers
cmds:
- task: pg:stop
- task: redis:down
- task: mailpit:down

pg:
desc: Start Postgres
cmds:
Expand All @@ -227,6 +208,25 @@ tasks:
cmds:
- docker stop linkup-postgres

redis:
desc: Start Redis
cmds:
- docker run --rm --name tlz-redis -p 6379:6379 -d redis:alpine

redis:down:
desc: Stop Redis
cmds:
- docker stop tlz-redis

mailpit:
desc: Start Mailpit
cmds:
- >-
docker run --rm -d --name tlz-mailpit
-p 1025:1025 -p 1080:1080
-e MP_SMTP_AUTH_ACCEPT_ANY=true -e MP_SMTP_AUTH_ALLOW_INSECURE=true
axllent/mailpit
test-grpc:
desc: Run grpcurl tests
cmds:
Expand Down
10 changes: 5 additions & 5 deletions cmd/server/api/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ package api

import (
"github.com/stretchr/testify/require"
"github.com/zcubbs/tlz/cmd/server/config"
db "github.com/zcubbs/tlz/cmd/server/db/sqlc"
"github.com/zcubbs/tlz/internal/util"
"github.com/zcubbs/tlz/worker"
"github.com/zcubbs/tlz/cmd/server/worker"
"github.com/zcubbs/x/random"
"testing"
"time"
)

func newTestServer(t *testing.T, store db.Store, taskDistributor worker.TaskDistributor) *Server {
config := util.Config{
Auth: util.AuthConfig{
cfg := config.Config{
Auth: config.AuthConfig{
TokenSymmetricKey: random.String(32),
AccessTokenDuration: time.Minute,
RefreshTokenDuration: 5 * time.Minute,
},
}

server, err := NewServer(store, taskDistributor, config)
server, err := NewServer(store, taskDistributor, cfg)
require.NoError(t, err)

return server
Expand Down
4 changes: 2 additions & 2 deletions cmd/server/api/rpc_create_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"context"
"github.com/hibiken/asynq"
db "github.com/zcubbs/tlz/cmd/server/db/sqlc"
"github.com/zcubbs/tlz/internal/validator"
"github.com/zcubbs/tlz/cmd/server/internal/validator"
"github.com/zcubbs/tlz/cmd/server/worker"
"github.com/zcubbs/tlz/pb"
"github.com/zcubbs/tlz/worker"
"github.com/zcubbs/x/password"
"google.golang.org/genproto/googleapis/rpc/errdetails"
"google.golang.org/grpc/codes"
Expand Down
4 changes: 2 additions & 2 deletions cmd/server/api/rpc_create_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"github.com/stretchr/testify/require"
mockdb "github.com/zcubbs/tlz/cmd/server/db/mock"
db "github.com/zcubbs/tlz/cmd/server/db/sqlc"
"github.com/zcubbs/tlz/cmd/server/worker"
mockwk "github.com/zcubbs/tlz/cmd/server/worker/mock"
"github.com/zcubbs/tlz/pb"
"github.com/zcubbs/tlz/worker"
mockwk "github.com/zcubbs/tlz/worker/mock"
"github.com/zcubbs/x/password"
"github.com/zcubbs/x/random"
"go.uber.org/mock/gomock"
Expand Down
2 changes: 1 addition & 1 deletion cmd/server/api/rpc_update_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"errors"
"github.com/jackc/pgx/v5/pgtype"
db "github.com/zcubbs/tlz/cmd/server/db/sqlc"
"github.com/zcubbs/tlz/internal/validator"
"github.com/zcubbs/tlz/cmd/server/internal/validator"
"github.com/zcubbs/tlz/pb"
"github.com/zcubbs/x/password"
"google.golang.org/genproto/googleapis/rpc/errdetails"
Expand Down
2 changes: 1 addition & 1 deletion cmd/server/api/rpc_verify_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"github.com/google/uuid"
db "github.com/zcubbs/tlz/cmd/server/db/sqlc"
"github.com/zcubbs/tlz/internal/validator"
"github.com/zcubbs/tlz/cmd/server/internal/validator"
"github.com/zcubbs/tlz/pb"
"google.golang.org/genproto/googleapis/rpc/errdetails"
"google.golang.org/grpc/codes"
Expand Down
8 changes: 4 additions & 4 deletions cmd/server/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"github.com/charmbracelet/log"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/rs/cors"
"github.com/zcubbs/tlz/cmd/server/config"
db "github.com/zcubbs/tlz/cmd/server/db/sqlc"
"github.com/zcubbs/tlz/internal/util"
"github.com/zcubbs/tlz/cmd/server/worker"
"github.com/zcubbs/tlz/pb"
"github.com/zcubbs/tlz/pkg/token"
"github.com/zcubbs/tlz/worker"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
"google.golang.org/protobuf/encoding/protojson"
Expand All @@ -24,7 +24,7 @@ type Server struct {
pb.UnimplementedTlzServer
store db.Store
tokenMaker token.Maker
cfg util.Config
cfg config.Config
embedAssets []EmbedAssetsOpts
taskDistributor worker.TaskDistributor
}
Expand All @@ -38,7 +38,7 @@ type EmbedAssetsOpts struct {
}

func NewServer(store db.Store, taskDistributor worker.TaskDistributor,
cfg util.Config, embedOpts ...EmbedAssetsOpts) (*Server, error) {
cfg config.Config, embedOpts ...EmbedAssetsOpts) (*Server, error) {
tokenMaker, err := token.NewPasetoMaker(cfg.Auth.TokenSymmetricKey)
if err != nil {
return nil, fmt.Errorf("cannot create new tokenMaker: %w", err)
Expand Down
54 changes: 48 additions & 6 deletions cmd/server/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ package config
import "time"

type Config struct {
Debug bool `mapstructure:"debug"`
HttpServer HttpServerConfig `mapstructure:"http_server"`
GrpcServer GrpcServerConfig `mapstructure:"grpc_server"`
Auth AuthConfig `mapstructure:"auth"`
Database DatabaseConfig `mapstructure:"database"`
InitAdminPassword string `mapstructure:"init_admin_password"`
Debug bool `mapstructure:"debug"`
HttpServer HttpServerConfig `mapstructure:"http_server"`
GrpcServer GrpcServerConfig `mapstructure:"grpc_server"`
Auth AuthConfig `mapstructure:"auth"`
Database DatabaseConfig `mapstructure:"database"`
InitAdminPassword string `mapstructure:"init_admin_password"`
Redis RedisConfig `mapstructure:"redis"`
Cron CronConfig `mapstructure:"cron"`
Notification NotificationConfig `mapstructure:"notification"`
}

type HttpServerConfig struct {
Expand Down Expand Up @@ -72,3 +75,42 @@ type PostgresConfig struct {
// MinConns is the minimum number of connections in the pool. Default value: 2
MinConns int32 `mapstructure:"min_conns" json:"min_conns"`
}

type RedisConfig struct {
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
}

type CronConfig struct {
CheckCertificateValidity `mapstructure:"check_certificate_validity"`
SendMailNotification `mapstructure:"send_mail_notification"`
}

type CheckCertificateValidity struct {
Enabled bool `mapstructure:"enabled"`
CronPattern string `mapstructure:"cron_pattern"`
}

type SendMailNotification struct {
Enabled bool `mapstructure:"enabled"`
CronPattern string `mapstructure:"cron_pattern"`
}

type NotificationConfig struct {
Mail MailConfig `mapstructure:"mail"`
ApiDomainName string `mapstructure:"api_domain_name"`
}

type MailConfig struct {
Smtp SmtpConfig `mapstructure:"smtp"`
}

type SmtpConfig struct {
Enabled bool `mapstructure:"enabled"`
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
FromAddress string `mapstructure:"from_address"`
FromName string `mapstructure:"from_name"`
}
2 changes: 1 addition & 1 deletion cmd/server/db/mock/store.go

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

11 changes: 6 additions & 5 deletions cmd/server/db/sqlc/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@ package db
import (
"context"
"github.com/charmbracelet/log"
"github.com/zcubbs/tlz/internal/logger"
"github.com/zcubbs/tlz/internal/util"
"github.com/zcubbs/tlz/cmd/server/config"
"github.com/zcubbs/tlz/cmd/server/db/migration"
"github.com/zcubbs/tlz/cmd/server/db/util"
"os"
"testing"
)

var testStore Store

func TestMain(m *testing.M) {
config := util.Bootstrap()
cfg := config.Bootstrap()
ctx := context.Background()
// Migrate database
err := migration.Run(config.Database)
err := migration.Run(cfg.Database)
if err != nil {
log.Fatal("failed perform database migrations", "error", err)
}
conn, err := util.DbConnect(ctx, config.Database, logger.GetLogger())
conn, err := util.Connect(ctx, cfg.Database)
if err != nil {
log.Fatal("failed to connect to database", "error", err)
}
Expand Down
Loading

0 comments on commit 559a463

Please sign in to comment.