#!/usr/bin/env python3

config = '''user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';

    access_log /var/log/nginx/access.log main;

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

    resolver 127.0.0.11 valid=300s;
    resolver_timeout 5s;

    gzip on;
    gzip_vary on;
    gzip_min_length 1024;
    gzip_types text/plain text/css text/xml text/javascript application/javascript application/json;
    gzip_comp_level 6;

    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-XSS-Protection "1; mode=block" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header Referrer-Policy "strict-origin-when-cross-origin" always;

    server {
        listen 80;
        server_name www.fengzhongxing.com;
        root /usr/share/nginx/html;
        index index.html;

        location /official/ {
            try_files $uri $uri/ /official/index.html;
        }

        location /api/ {
            proxy_pass http://fzx-aliyun-gateway:8080;
            proxy_set_header Host $host;
        }

        location / {
            return 301 /official/;
        }
    }

    server {
        listen 80;
        server_name platform.fengzhongxing.com;
        root /usr/share/nginx/html;
        index index.html;

        location /platform/ {
            try_files $uri $uri/ /platform/index.html;
        }

        location /api/ {
            proxy_pass http://fzx-aliyun-gateway:8080;
            proxy_set_header Host $host;
        }

        location / {
            return 301 /platform/;
        }
    }

    server {
        listen 80;
        server_name merchant.fengzhongxing.com;
        root /usr/share/nginx/html;
        index index.html;

        location /merchant/ {
            try_files $uri $uri/ /merchant/index.html;
        }

        location /api/ {
            proxy_pass http://fzx-aliyun-gateway:8080;
            proxy_set_header Host $host;
        }

        location / {
            return 301 /merchant/;
        }
    }

    server {
        listen 80;
        server_name h5.fengzhongxing.com;
        root /usr/share/nginx/html;
        index index.html;

        location /h5/ {
            try_files $uri $uri/ /h5/index.html;
        }

        location /api/ {
            proxy_pass http://fzx-aliyun-gateway:8080;
            proxy_set_header Host $host;
        }

        location / {
            return 301 /h5/;
        }
    }

    server {
        listen 80;
        server_name rider.fengzhongxing.com;
        root /usr/share/nginx/html;
        index index.html;

        location /rider/ {
            try_files $uri $uri/ /rider/index.html;
        }

        location /api/ {
            proxy_pass http://fzx-aliyun-gateway:8080;
            proxy_set_header Host $host;
        }

        location / {
            return 301 /rider/;
        }
    }

    server {
        listen 80;
        server_name api.fengzhongxing.com;

        add_header Access-Control-Allow-Origin *;
        add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS,PATCH;
        add_header Access-Control-Allow-Headers Content-Type,Authorization,token;
        add_header Access-Control-Expose-Headers Authorization,token;

        if ($request_method = OPTIONS) {
            return 204;
        }

        location /api/ {
            proxy_pass http://fzx-aliyun-gateway:8080;
            proxy_set_header Host $host;
        }

        location /actuator/ {
            proxy_pass http://fzx-aliyun-gateway:8080;
            proxy_set_header Host $host;
        }
    }

    server {
        listen 80 default_server;
        server_name _;

        root /usr/share/nginx/html;
        index index.html;

        location /platform/ {
            try_files $uri $uri/ /platform/index.html;
        }

        location /merchant/ {
            try_files $uri $uri/ /merchant/index.html;
        }

        location /official/ {
            try_files $uri $uri/ /official/index.html;
        }

        location /api/ {
            proxy_pass http://fzx-aliyun-gateway:8080;
            proxy_set_header Host $host;
        }

        location / {
            return 302 /platform/;
        }
    }
}
'''

with open('/opt/Fzxpodman/infrastructure/nginx/conf/aliyun-dev.conf', 'w') as f:
    f.write(config)

print('配置文件已生成')
