Skip to content

Commit

Permalink
Merge pull request #30 from moheladwy/SolveCORS
Browse files Browse the repository at this point in the history
fixed the bug in the cors
  • Loading branch information
moheladwy authored Jan 13, 2025
2 parents e6437f9 + f4671da commit ce52ee6
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 3 deletions.
69 changes: 69 additions & 0 deletions docker-compose-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
services:
cache:
image: redis:alpine
container_name: todo-redis-cache
networks:
- todo-network
ports:
- "6379:6379"
volumes:
- redis-data:/data
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 10s
timeout: 10s
retries: 3
labels:
- "com.centurylinklabs.watchtower.enable=true"

api:
image: only1adwy/todo-api:latest
environment:
- ASPNETCORE_ENVIRONMENT=Production
deploy:
mode: replicated
replicas: 2
update_config:
parallelism: 1
restart_policy:
condition: on-failure
resources:
limits:
cpus: "1"
memory: 1G
reservations:
cpus: "0.25"
memory: 125M
depends_on:
- cache
networks:
- todo-network
command:
- "sh -c dotnet Todo.Api.dll ef database update && dotnet Todo.Api.dll"
labels:
- "com.centurylinklabs.watchtower.enable=true"

nginx:
image: nginx:alpine
container_name: todo-reverse-proxy
ports:
- "8070:8070"
volumes:
- ./conf.d:/etc/nginx/conf.d
- /var/run/docker.sock:/tmp/docker.sock:ro
depends_on:
- cache
- api
networks:
- todo-network
labels:
- "com.centurylinklabs.watchtower.enable=true"

volumes:
redis-data:
name: redis-data

networks:
todo-network:
driver: bridge
name: todo-network
39 changes: 36 additions & 3 deletions nginx.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,39 @@
upstream api_backend {
server api:8080; # Docker will automatically load balance between replicas
keepalive 32; # Keep connections alive
}

# API Server
server {
listen 80;
listen 8070;
server_name api-todo.aladawy.duckdns.org;

# Compression settings
gzip on;
gzip_types text/plain application/json;

location / {
proxy_pass http://todo-api:8070;
}
proxy_pass http://api_backend/;
# Add CORS headers
add_header 'Access-Control-Allow-Origin' 'https://tasker-aladawy.netlify.app' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type' always;
add_header 'Access-Control-Allow-Credentials' 'true' always;

# Handle OPTIONS method
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' 'https://tasker-aladawy.netlify.app' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type' always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}

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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ public static void AddCorsService(this WebApplicationBuilder builder)
builder
.WithOrigins(Constants.ClientCrossOriginPolicyProductionURL)
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});
Expand Down

0 comments on commit ce52ee6

Please sign in to comment.