提交代码
This commit is contained in:
150
scripts/gateway.sh
Executable file
150
scripts/gateway.sh
Executable file
@@ -0,0 +1,150 @@
|
||||
#!/bin/bash
|
||||
|
||||
# 网关管理脚本
|
||||
|
||||
set -e
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
|
||||
GATEWAY_DIR="$PROJECT_ROOT/gateway"
|
||||
NGINX_CONF_DIR="$GATEWAY_DIR/nginx"
|
||||
|
||||
cd "$PROJECT_ROOT"
|
||||
|
||||
# 颜色输出
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
log_info() {
|
||||
echo -e "${GREEN}[INFO]${NC} $1"
|
||||
}
|
||||
|
||||
log_warn() {
|
||||
echo -e "${YELLOW}[WARN]${NC} $1"
|
||||
}
|
||||
|
||||
log_error() {
|
||||
echo -e "${RED}[ERROR]${NC} $1"
|
||||
}
|
||||
|
||||
# 测试 nginx 配置
|
||||
test_config() {
|
||||
log_info "Testing nginx configuration..."
|
||||
|
||||
docker run --rm \
|
||||
-v "$NGINX_CONF_DIR/nginx.conf:/etc/nginx/nginx.conf:ro" \
|
||||
-v "$NGINX_CONF_DIR/conf.d:/etc/nginx/conf.d:ro" \
|
||||
nginx:1.25-alpine \
|
||||
nginx -t
|
||||
|
||||
if [ $? -eq 0 ]; then
|
||||
log_info "Configuration test passed!"
|
||||
else
|
||||
log_error "Configuration test failed!"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
# 重新加载配置(热重载)
|
||||
reload_config() {
|
||||
log_info "Reloading nginx configuration..."
|
||||
|
||||
CONTAINER_ID=$(docker ps -q -f name=api-gateway)
|
||||
|
||||
if [ -z "$CONTAINER_ID" ]; then
|
||||
log_error "Gateway container is not running"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
docker exec "$CONTAINER_ID" nginx -s reload
|
||||
log_info "Configuration reloaded successfully"
|
||||
}
|
||||
|
||||
# 查看网关日志
|
||||
view_logs() {
|
||||
log_info "Viewing gateway logs..."
|
||||
|
||||
if [ "$1" == "follow" ] || [ "$1" == "-f" ]; then
|
||||
tail -f "$NGINX_CONF_DIR/logs/"*.log 2>/dev/null || docker logs -f api-gateway 2>/dev/null
|
||||
else
|
||||
tail -n 100 "$NGINX_CONF_DIR/logs/"*.log 2>/dev/null || docker logs --tail 100 api-gateway 2>/dev/null
|
||||
fi
|
||||
}
|
||||
|
||||
# 生成自签名证书(开发用)
|
||||
generate_certs() {
|
||||
log_info "Generating self-signed certificates..."
|
||||
|
||||
CERT_DIR="$NGINX_CONF_DIR/ssl"
|
||||
mkdir -p "$CERT_DIR"
|
||||
|
||||
openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
|
||||
-keyout "$CERT_DIR/key.pem" \
|
||||
-out "$CERT_DIR/cert.pem" \
|
||||
-subj "/CN=api.example.com" \
|
||||
-addext "subjectAltName=DNS:api.example.com,DNS:localhost,IP:127.0.0.1"
|
||||
|
||||
log_info "Certificates generated in $CERT_DIR"
|
||||
}
|
||||
|
||||
# 显示状态
|
||||
status() {
|
||||
log_info "Gateway Status:"
|
||||
|
||||
CONTAINER_ID=$(docker ps -q -f name=api-gateway)
|
||||
|
||||
if [ -n "$CONTAINER_ID" ]; then
|
||||
echo " Container: Running ($CONTAINER_ID)"
|
||||
docker exec "$CONTAINER_ID" nginx -V 2>/dev/null | head -1
|
||||
else
|
||||
echo " Container: Not running"
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo " Configuration files:"
|
||||
ls -la "$NGINX_CONF_DIR/conf.d/"
|
||||
}
|
||||
|
||||
# 使用说明
|
||||
usage() {
|
||||
echo "Gateway Management Script"
|
||||
echo ""
|
||||
echo "Usage: $0 <command>"
|
||||
echo ""
|
||||
echo "Commands:"
|
||||
echo " test Test nginx configuration"
|
||||
echo " reload Reload configuration (hot reload)"
|
||||
echo " logs View logs (use 'logs follow' for real-time)"
|
||||
echo " certs Generate self-signed certificates (dev only)"
|
||||
echo " status Show gateway status"
|
||||
echo " help Show this help message"
|
||||
}
|
||||
|
||||
# 主逻辑
|
||||
case "${1:-help}" in
|
||||
test)
|
||||
test_config
|
||||
;;
|
||||
reload)
|
||||
reload_config
|
||||
;;
|
||||
logs)
|
||||
view_logs "$2"
|
||||
;;
|
||||
certs)
|
||||
generate_certs
|
||||
;;
|
||||
status)
|
||||
status
|
||||
;;
|
||||
help|--help|-h)
|
||||
usage
|
||||
;;
|
||||
*)
|
||||
log_error "Unknown command: $1"
|
||||
usage
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
Reference in New Issue
Block a user