initial commit
This commit is contained in:
commit
e7987e88a7
70 changed files with 13661 additions and 0 deletions
132
deploy/deploy.sh
Executable file
132
deploy/deploy.sh
Executable file
|
|
@ -0,0 +1,132 @@
|
|||
#!/bin/bash
|
||||
# Deployment script for go-first / URPI
|
||||
# Usage: ./deploy.sh [production|staging]
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
# Colors
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
ENVIRONMENT="${1:-staging}"
|
||||
PROJECT_NAME="go-first"
|
||||
DOCKER_IMAGE="ghcr.io/dnoulet/${PROJECT_NAME}"
|
||||
VERSION=$(git describe --tags --always --dirty 2>/dev/null || echo "dev")
|
||||
|
||||
log_info() {
|
||||
echo -e "${GREEN}[INFO]${NC} $1"
|
||||
}
|
||||
|
||||
log_warn() {
|
||||
echo -e "${YELLOW}[WARN]${NC} $1"
|
||||
}
|
||||
|
||||
log_error() {
|
||||
echo -e "${RED}[ERROR]${NC} $1"
|
||||
}
|
||||
|
||||
check_requirements() {
|
||||
log_info "Checking requirements..."
|
||||
|
||||
command -v docker >/dev/null 2>&1 || { log_error "docker is required but not installed."; exit 1; }
|
||||
command -v docker-compose >/dev/null 2>&1 || { log_error "docker-compose is required but not installed."; exit 1; }
|
||||
|
||||
if [[ "$ENVIRONMENT" == "production" ]]; then
|
||||
command -v kubectl >/dev/null 2>&1 || log_warn "kubectl not found, using docker-compose"
|
||||
fi
|
||||
}
|
||||
|
||||
build_and_push() {
|
||||
log_info "Building Docker image: ${DOCKER_IMAGE}:${VERSION}"
|
||||
docker build -t "${DOCKER_IMAGE}:${VERSION}" -t "${DOCKER_IMAGE}:latest" .
|
||||
|
||||
log_info "Pushing Docker image..."
|
||||
docker push "${DOCKER_IMAGE}:${VERSION}"
|
||||
docker push "${DOCKER_IMAGE}:latest"
|
||||
}
|
||||
|
||||
deploy_docker_compose() {
|
||||
log_info "Deploying with docker-compose (${ENVIRONMENT})..."
|
||||
|
||||
ENV_FILE=".env.${ENVIRONMENT}"
|
||||
if [[ ! -f "$ENV_FILE" ]]; then
|
||||
log_error "Environment file ${ENV_FILE} not found!"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Pull latest images
|
||||
docker-compose --env-file "$ENV_FILE" pull
|
||||
|
||||
# Start services
|
||||
docker-compose --env-file "$ENV_FILE" up -d --remove-orphans
|
||||
|
||||
# Wait for health checks
|
||||
log_info "Waiting for services to be healthy..."
|
||||
sleep 10
|
||||
|
||||
# Check health
|
||||
if docker-compose --env-file "$ENV_FILE" ps | grep -q "unhealthy"; then
|
||||
log_error "Some services are unhealthy!"
|
||||
docker-compose --env-file "$ENV_FILE" ps
|
||||
exit 1
|
||||
fi
|
||||
|
||||
log_info "Deployment successful!"
|
||||
}
|
||||
|
||||
deploy_kubernetes() {
|
||||
log_info "Deploying to Kubernetes (${ENVIRONMENT})..."
|
||||
|
||||
# Apply Kubernetes manifests
|
||||
kubectl apply -k "deploy/kubernetes/overlays/${ENVIRONMENT}"
|
||||
|
||||
# Wait for rollout
|
||||
kubectl rollout status deployment/${PROJECT_NAME} -n ${PROJECT_NAME}-${ENVIRONMENT} --timeout=300s
|
||||
|
||||
log_info "Kubernetes deployment successful!"
|
||||
}
|
||||
|
||||
run_migrations() {
|
||||
log_info "Running database migrations..."
|
||||
|
||||
if [[ "$ENVIRONMENT" == "production" ]]; then
|
||||
# Run migrations in production
|
||||
docker-compose --env-file ".env.${ENVIRONMENT}" exec -T app ./go-first migrate up
|
||||
else
|
||||
docker-compose --env-file ".env.${ENVIRONMENT}" exec -T app ./go-first migrate up
|
||||
fi
|
||||
}
|
||||
|
||||
cleanup() {
|
||||
log_info "Cleaning up old Docker images..."
|
||||
docker image prune -f
|
||||
}
|
||||
|
||||
main() {
|
||||
log_info "Starting deployment for ${ENVIRONMENT} (version: ${VERSION})"
|
||||
|
||||
check_requirements
|
||||
|
||||
if [[ "$ENVIRONMENT" != "production" && "$ENVIRONMENT" != "staging" ]]; then
|
||||
log_error "Invalid environment. Use 'production' or 'staging'"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
build_and_push
|
||||
|
||||
if command -v kubectl >/dev/null 2>&1 && [[ "$ENVIRONMENT" == "production" ]]; then
|
||||
deploy_kubernetes
|
||||
else
|
||||
deploy_docker_compose
|
||||
fi
|
||||
|
||||
run_migrations
|
||||
cleanup
|
||||
|
||||
log_info "Deployment completed successfully!"
|
||||
log_info "Application available at: https://www.urpi.be"
|
||||
}
|
||||
|
||||
main "$@"
|
||||
186
deploy/nginx.conf
Normal file
186
deploy/nginx.conf
Normal file
|
|
@ -0,0 +1,186 @@
|
|||
# NGINX Configuration for go-first / URPI
|
||||
# Deploy to /etc/nginx/nginx.conf
|
||||
|
||||
user nginx;
|
||||
worker_processes auto;
|
||||
error_log /var/log/nginx/error.log warn;
|
||||
pid /var/run/nginx.pid;
|
||||
|
||||
events {
|
||||
worker_connections 1024;
|
||||
use epoll;
|
||||
multi_accept on;
|
||||
}
|
||||
|
||||
http {
|
||||
include /etc/nginx/mime.types;
|
||||
default_type application/octet-stream;
|
||||
|
||||
# Logging
|
||||
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
|
||||
'$status $body_bytes_sent "$http_referer" '
|
||||
'"$http_user_agent" "$http_x_forwarded_for" '
|
||||
'rt=$request_time uct="$upstream_connect_time" '
|
||||
'uht="$upstream_header_time" urt="$upstream_response_time"';
|
||||
|
||||
access_log /var/log/nginx/access.log main;
|
||||
|
||||
# Performance
|
||||
sendfile on;
|
||||
tcp_nopush on;
|
||||
tcp_nodelay on;
|
||||
keepalive_timeout 65;
|
||||
types_hash_max_size 2048;
|
||||
server_tokens off;
|
||||
|
||||
# Gzip
|
||||
gzip on;
|
||||
gzip_vary on;
|
||||
gzip_proxied any;
|
||||
gzip_comp_level 6;
|
||||
gzip_min_length 1000;
|
||||
gzip_types
|
||||
text/plain
|
||||
text/css
|
||||
text/xml
|
||||
text/javascript
|
||||
application/json
|
||||
application/javascript
|
||||
application/xml+rss
|
||||
application/atom+xml
|
||||
image/svg+xml;
|
||||
|
||||
# Brotli (if available)
|
||||
# brotli on;
|
||||
# brotli_comp_level 6;
|
||||
# brotli_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
|
||||
|
||||
# Rate limiting
|
||||
limit_req_zone $binary_remote_addr zone=api:10m rate=10r/s;
|
||||
limit_req_zone $binary_remote_addr zone=login:10m rate=5r/m;
|
||||
|
||||
# Upstream
|
||||
upstream go_first_backend {
|
||||
server app:8080;
|
||||
keepalive 32;
|
||||
}
|
||||
|
||||
# HTTP -> HTTPS redirect
|
||||
server {
|
||||
listen 80;
|
||||
listen [::]:80;
|
||||
server_name www.urpi.be urpi.be;
|
||||
|
||||
# ACME challenge for Let's Encrypt
|
||||
location /.well-known/acme-challenge/ {
|
||||
root /var/www/certbot;
|
||||
}
|
||||
|
||||
# Redirect all other traffic to HTTPS
|
||||
location / {
|
||||
return 301 https://www.urpi.be$request_uri;
|
||||
}
|
||||
}
|
||||
|
||||
# HTTPS server
|
||||
server {
|
||||
listen 443 ssl http2;
|
||||
listen [::]:443 ssl http2;
|
||||
server_name www.urpi.be urpi.be;
|
||||
|
||||
# SSL Configuration
|
||||
ssl_certificate /etc/nginx/ssl/fullchain.pem;
|
||||
ssl_certificate_key /etc/nginx/ssl/privkey.pem;
|
||||
ssl_session_timeout 1d;
|
||||
ssl_session_cache shared:SSL:10m;
|
||||
ssl_session_tickets off;
|
||||
|
||||
# Modern SSL config
|
||||
ssl_protocols TLSv1.2 TLSv1.3;
|
||||
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
|
||||
ssl_prefer_server_ciphers off;
|
||||
|
||||
# HSTS
|
||||
add_header Strict-Transport-Security "max-age=31536000" always;
|
||||
|
||||
# OCSP Stapling
|
||||
ssl_stapling on;
|
||||
ssl_stapling_verify on;
|
||||
resolver 1.1.1.1 8.8.8.8 valid=300s;
|
||||
resolver_timeout 5s;
|
||||
|
||||
# Security headers
|
||||
add_header X-Frame-Options "DENY" always;
|
||||
add_header X-Content-Type-Options "nosniff" always;
|
||||
add_header X-XSS-Protection "1; mode=block" always;
|
||||
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
|
||||
add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self' data:; connect-src 'self'; frame-ancestors 'none';" always;
|
||||
|
||||
# Frontend static files
|
||||
root /var/www/html;
|
||||
index index.html;
|
||||
|
||||
# Cache static assets
|
||||
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
|
||||
expires 1y;
|
||||
add_header Cache-Control "public, immutable";
|
||||
try_files $uri =404;
|
||||
}
|
||||
|
||||
# API routes
|
||||
location /api/ {
|
||||
limit_req zone=api burst=20 nodelay;
|
||||
|
||||
proxy_pass http://go_first_backend;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Upgrade $http_upgrade;
|
||||
proxy_set_header Connection 'upgrade';
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
proxy_cache_bypass $http_upgrade;
|
||||
|
||||
# Timeouts
|
||||
proxy_connect_timeout 5s;
|
||||
proxy_send_timeout 30s;
|
||||
proxy_read_timeout 30s;
|
||||
|
||||
# Buffering
|
||||
proxy_buffering on;
|
||||
proxy_buffer_size 4k;
|
||||
proxy_buffers 8 4k;
|
||||
}
|
||||
|
||||
# Health check endpoint (no rate limit)
|
||||
location /api/v1/health {
|
||||
proxy_pass http://go_first_backend;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
access_log off;
|
||||
}
|
||||
|
||||
# SPA fallback - serve index.html for all other routes
|
||||
location / {
|
||||
try_files $uri $uri/ /index.html;
|
||||
|
||||
# Cache HTML for short time
|
||||
add_header Cache-Control "no-cache, must-revalidate";
|
||||
}
|
||||
|
||||
# Block access to hidden files
|
||||
location ~ /\. {
|
||||
deny all;
|
||||
access_log off;
|
||||
log_not_found off;
|
||||
}
|
||||
|
||||
# Block access to backup/config files
|
||||
location ~* \.(env|log|conf|bak|backup|sql)$ {
|
||||
deny all;
|
||||
access_log off;
|
||||
log_not_found off;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue