Skip to content

Commit bbec18f

Browse files
grpc server
1 parent cd2b2ab commit bbec18f

39 files changed

+4447
-45
lines changed

backend/lms-sys/Makefile

Lines changed: 251 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,265 @@
1-
.PHONY: all build run test clean fmt vet lint tidy docker-build docker-run help
1+
# Makefile
2+
.PHONY: help install-tools create-env db-url migrate-up migrate-down migrate-create migrate-force migrate-version migrate-drop sqlc-generate clean build run test docker-up docker-down dev-setup prod-setup
23

3-
APP_NAME ?= server
4-
DOCKER_IMAGE ?= $(APP_NAME):latest
5-
GO_FILES := $(shell find . -type f -name '*.go' -not -path "./vendor/*")
4+
# Load environment variables
5+
include .env
6+
export
67

7-
all: fmt vet lint test build
8+
# Database URL construction
9+
DB_URL = postgresql://$(DB_USER):$(DB_PASSWORD)@$(DB_HOST):$(DB_PORT)/$(DB_NAME)?sslmode=$(DB_SSLMODE)
810

9-
build:
10-
@echo "🔨 Building application..."
11-
@go build -o bin/$(APP_NAME) ./cmd
11+
# Default target
12+
help: ## Show this help message
13+
@echo 'Usage: make [target]'
14+
@echo ''
15+
@echo 'Targets:'
16+
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf " %-20s %s\n", $$1, $$2}' $(MAKEFILE_LIST)
1217

13-
run:
14-
@echo "🚀 Running application..."
15-
@go run ./cmd
18+
install-tools: ## Install required tools
19+
@echo "Installing required tools..."
20+
go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest
21+
go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest
22+
@echo "Tools installed successfully!"
1623

17-
test:
18-
@echo "🧪 Running tests..."
19-
@go test -v ./...
24+
create-env: ## Create .env file from .env.example
25+
@if [ ! -f .env ]; then \
26+
cp .env.example .env; \
27+
echo ".env file created! Please update it with your actual values."; \
28+
else \
29+
echo ".env file already exists!"; \
30+
fi
2031

21-
test-coverage:
22-
@echo "🧪 Running tests with coverage..."
23-
@go test -v -coverprofile=coverage.out ./...
24-
@go tool cover -html=coverage.out -o coverage.html
32+
db-url: ## Show database URL (for debugging)
33+
@echo "Database URL: $(DB_URL)"
2534

26-
clean:
27-
@echo "🧹 Cleaning..."
28-
@rm -rf bin/
29-
@rm -f coverage.out coverage.html
35+
# Migration targets
36+
migrate-up: ## Apply all pending migrations
37+
@echo "Applying migrations..."
38+
migrate -path $(MIGRATIONS_PATH) -database "$(DB_URL)" up
39+
@echo "Migrations applied successfully!"
3040

31-
fmt:
32-
@echo "📝 Formatting code..."
33-
@gofmt -s -w $(GO_FILES)
41+
migrate-down: ## Rollback all migrations
42+
@echo "Rolling back all migrations..."
43+
@read -p "Are you sure you want to rollback ALL migrations? (y/N): " confirm; \
44+
if [ "$$confirm" = "y" ] || [ "$$confirm" = "Y" ]; then \
45+
migrate -path $(MIGRATIONS_PATH) -database "$(DB_URL)" down; \
46+
echo "All migrations rolled back!"; \
47+
else \
48+
echo "Migration rollback cancelled."; \
49+
fi
3450

35-
vet:
36-
@echo "🔍 Running go vet..."
37-
@go vet ./...
51+
migrate-down-1: ## Rollback last migration
52+
@echo "Rolling back last migration..."
53+
migrate -path $(MIGRATIONS_PATH) -database "$(DB_URL)" down 1
54+
@echo "Last migration rolled back!"
3855

39-
lint:
40-
@echo "🔍 Running linter..."
41-
@golangci-lint run
56+
migrate-up-1: ## Apply next migration
57+
@echo "Applying next migration..."
58+
migrate -path $(MIGRATIONS_PATH) -database "$(DB_URL)" up 1
59+
@echo "Next migration applied!"
4260

43-
tidy:
44-
@echo "📦 Tidying go modules..."
45-
@go mod tidy
61+
migrate-create: ## Create a new migration file
62+
@read -p "Enter migration name: " name; \
63+
if [ -z "$$name" ]; then \
64+
echo "Migration name cannot be empty!"; \
65+
exit 1; \
66+
fi; \
67+
migrate create -ext sql -dir $(MIGRATIONS_PATH) -seq $$name; \
68+
echo "Migration files created for: $$name"
4669

47-
docker-build:
48-
@echo "🐳 Building Docker image..."
49-
@docker build -t $(DOCKER_IMAGE) .
70+
migrate-force: ## Force migration to specific version
71+
@read -p "Enter version to force: " version; \
72+
if [ -z "$$version" ]; then \
73+
echo "Version cannot be empty!"; \
74+
exit 1; \
75+
fi; \
76+
migrate -path $(MIGRATIONS_PATH) -database "$(DB_URL)" force $$version; \
77+
echo "Migration forced to version: $$version"
5078

51-
docker-run:
52-
@echo "🐳 Running Docker container..."
53-
@docker run -p 8080:8080 $(DOCKER_IMAGE)
79+
migrate-version: ## Show current migration version
80+
@migrate -path $(MIGRATIONS_PATH) -database "$(DB_URL)" version
5481

55-
help:
56-
@echo "Available targets:"
57-
@grep -E '^##' $(MAKEFILE_LIST) | sed 's/##//'
82+
migrate-drop: ## Drop everything in database
83+
@echo "This will DROP ALL DATA in the database!"
84+
@read -p "Are you absolutely sure? Type 'DROP' to confirm: " confirm; \
85+
if [ "$$confirm" = "DROP" ]; then \
86+
migrate -path $(MIGRATIONS_PATH) -database "$(DB_URL)" drop; \
87+
echo "Database dropped!"; \
88+
else \
89+
echo "Database drop cancelled."; \
90+
fi
91+
92+
# SQLC targets
93+
sqlc-generate: ## Generate Go code from SQL
94+
@echo "Generating Go code from SQL..."
95+
sqlc generate
96+
@echo "Go code generated successfully!"
97+
98+
sqlc-verify: ## Verify SQL queries
99+
@echo "Verifying SQL queries..."
100+
sqlc verify
101+
@echo "SQL queries verified successfully!"
102+
103+
sqlc-vet: ## Lint SQL queries
104+
@echo "Linting SQL queries..."
105+
sqlc vet
106+
@echo "SQL queries linted successfully!"
107+
108+
# Database management
109+
db-create: ## Create database
110+
@echo "Creating database: $(DB_NAME)"
111+
createdb -h $(DB_HOST) -p $(DB_PORT) -U $(DB_USER) $(DB_NAME)
112+
@echo "Database created successfully!"
113+
114+
db-drop: ## Drop database
115+
@echo "Dropping database: $(DB_NAME)"
116+
@read -p "Are you sure you want to drop the database? (y/N): " confirm; \
117+
if [ "$$confirm" = "y" ] || [ "$$confirm" = "Y" ]; then \
118+
dropdb -h $(DB_HOST) -p $(DB_PORT) -U $(DB_USER) $(DB_NAME); \
119+
echo "Database dropped!"; \
120+
else \
121+
echo "Database drop cancelled."; \
122+
fi
123+
124+
db-reset: ## Drop and recreate database, then run migrations
125+
@echo "Resetting database..."
126+
make db-drop
127+
make db-create
128+
make migrate-up
129+
@echo "Database reset complete!"
130+
131+
# Application targets
132+
build: ## Build the application
133+
@echo "Building application..."
134+
go build -o bin/lms-app ./cmd/server
135+
@echo "Application built successfully!"
136+
137+
run: ## Run the application
138+
@echo "Running application..."
139+
go run ./cmd/server
140+
141+
test: ## Run tests
142+
@echo "Running tests..."
143+
go test -v ./...
144+
145+
test-coverage: ## Run tests with coverage
146+
@echo "Running tests with coverage..."
147+
go test -v -coverprofile=coverage.out ./...
148+
go tool cover -html=coverage.out -o coverage.html
149+
@echo "Coverage report generated: coverage.html"
150+
151+
# Docker targets
152+
docker-up: ## Start docker containers
153+
@echo "Starting Docker containers..."
154+
docker-compose up -d
155+
@echo "Docker containers started!"
156+
157+
docker-down: ## Stop docker containers
158+
@echo "Stopping Docker containers..."
159+
docker-compose down
160+
@echo "Docker containers stopped!"
161+
162+
docker-logs: ## Show docker logs
163+
docker-compose logs -f
164+
165+
# Setup targets
166+
dev-setup: create-env install-tools migrate-up sqlc-generate ## Setup development environment
167+
@echo "Development environment setup complete!"
168+
@echo "Next steps:"
169+
@echo "1. Update .env file with your actual database credentials"
170+
@echo "2. Run 'make db-create' to create the database"
171+
@echo "3. Run 'make migrate-up' to apply migrations"
172+
@echo "4. Run 'make sqlc-generate' to generate Go code"
173+
@echo "5. Run 'make run' to start the application"
174+
175+
prod-setup: ## Setup production environment
176+
@echo "Setting up production environment..."
177+
@if [ "$(APP_ENV)" != "production" ]; then \
178+
echo "Warning: APP_ENV is not set to 'production'"; \
179+
fi
180+
make migrate-up
181+
make sqlc-generate
182+
make build
183+
@echo "Production setup complete!"
184+
185+
# Cleanup targets
186+
clean: ## Clean build artifacts
187+
@echo "Cleaning build artifacts..."
188+
rm -rf bin/
189+
rm -f coverage.out coverage.html
190+
@echo "Clean complete!"
191+
192+
clean-all: clean ## Clean everything including generated code
193+
@echo "Cleaning all generated files..."
194+
rm -rf $(GENERATED_PATH)
195+
@echo "All clean complete!"
196+
197+
# Seed data (example)
198+
seed: ## Seed database with sample data
199+
@echo "Seeding database with sample data..."
200+
@if [ -f ./scripts/seed.sql ]; then \
201+
psql "$(DB_URL)" -f ./scripts/seed.sql; \
202+
echo "Database seeded successfully!"; \
203+
else \
204+
echo "Seed file not found: ./scripts/seed.sql"; \
205+
fi
206+
207+
# Backup and restore
208+
backup: ## Backup database
209+
@echo "Creating database backup..."
210+
pg_dump "$(DB_URL)" > backups/$(DB_NAME)_$(shell date +%Y%m%d_%H%M%S).sql
211+
@echo "Backup created in backups/ directory"
212+
213+
restore: ## Restore database from backup
214+
@read -p "Enter backup filename: " filename; \
215+
if [ -f "backups/$$filename" ]; then \
216+
psql "$(DB_URL)" < "backups/$$filename"; \
217+
echo "Database restored from: $$filename"; \
218+
else \
219+
echo "Backup file not found: backups/$$filename"; \
220+
fi
221+
222+
# Proto targets
223+
proto-setup: ## Create proto folder structure
224+
@echo "Creating proto folder structure..."
225+
chmod +x scripts/generate_proto_structure.sh
226+
./scripts/generate_proto_structure.sh
227+
228+
proto-clean: ## Clean proto directories
229+
@echo "Cleaning proto directories..."
230+
rm -rf proto/
231+
@echo "Proto directories cleaned!"
232+
233+
proto-tree: ## Show proto directory structure
234+
@echo "Proto directory structure:"
235+
@if command -v tree >/dev/null 2>&1; then \
236+
tree proto/; \
237+
else \
238+
find proto/ -type d | sed 's|[^/]*/| |g'; \
239+
fi
240+
protoc:
241+
@echo "Generating proto..."
242+
cd proto && protoc --go_out=../protogen --go_opt=paths=source_relative \
243+
--go-grpc_out=../protogen --go-grpc_opt=paths=source_relative \
244+
--grpc-gateway_out=../protogen --grpc-gateway_opt paths=source_relative \
245+
--grpc-gateway_opt generate_unbound_methods=true \
246+
--openapiv2_out=../doc/swagger --openapiv2_opt=allow_merge=true,merge_file_name=api \
247+
--experimental_allow_proto3_optional \
248+
./**/*.proto
249+
@echo "Generating swagger statik..."
250+
statik -src=doc/swagger -dest=doc -f
251+
252+
# Show current configuration
253+
show-config: ## Show current configuration
254+
@echo "Current Configuration:"
255+
@echo "====================="
256+
@echo "Database Host: $(DB_HOST)"
257+
@echo "Database Port: $(DB_PORT)"
258+
@echo "Database Name: $(DB_NAME)"
259+
@echo "Database User: $(DB_USER)"
260+
@echo "Migrations Path: $(MIGRATIONS_PATH)"
261+
@echo "Schema Path: $(SCHEMA_PATH)"
262+
@echo "Queries Path: $(QUERIES_PATH)"
263+
@echo "Generated Path: $(GENERATED_PATH)"
264+
@echo "App Port: $(APP_PORT)"
265+
@echo "App Environment: $(APP_ENV)"

backend/lms-sys/api/server.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package api
2+
3+
import (
4+
tpb "github.com/multi-tenants-cms-golang/lms-sys/protogen/tenants"
5+
)
6+
type Server struct {
7+
}

0 commit comments

Comments
 (0)