Skip to content

Instantly share code, notes, and snippets.

@goyalankit
Created September 16, 2024 17:22
Show Gist options
  • Save goyalankit/8a5b73bfca12fa7a772e7b28e9dd28d0 to your computer and use it in GitHub Desktop.
Save goyalankit/8a5b73bfca12fa7a772e7b28e9dd28d0 to your computer and use it in GitHub Desktop.
SideCart Auth Proxy Example
from flask import Flask, jsonify, request
app = Flask(__name__)
@app.route('/auth', methods=['GET'])
def auth():
return jsonify({'message': 'Auth endpoint hit successfully!'}), 200
@app.route('/<path:subpath>', methods=['GET', 'POST', 'PUT', 'DELETE'])
def catch_all(subpath):
# Extract query parameters
query_params = request.args.to_dict()
# Construct response
response = {
'route': subpath,
'parameters': query_params
}
return jsonify(response), 200
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8000)
services:
nginx:
image: nginx:1.27.1
volumes:
- /home/ankit/scratch/nginx.conf:/etc/nginx/nginx.conf:ro
ports:
- 9900:80
networks:
- my_network
python-service:
image: local-app:0.0.3
# command: python -m http.server
networks:
- my_network
networks:
my_network:
driver: bridge
FROM python:3.10
RUN pip install flask
COPY app.py app.py
CMD ["python3", "app.py"]
# Define the user and worker processes
user nginx;
worker_processes auto;
# Configure error logging
error_log /var/log/nginx/error.log warn;
pid /run/nginx.pid;
# Set up the events module
events {
worker_connections 1024;
}
# Define the HTTP server configuration
http {
# Log format and access log location
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;
# Set up MIME types
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Configure compression
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
# Set up the server block
server {
listen 80;
server_name localhost;
location /private/ {
auth_request /auth;
error_page 401 = @error401;
auth_request_set $user $upstream_http_x_forwarded_user;
proxy_set_header X-Forwarded-User $user;
proxy_pass http://192.168.16.2:8000;
}
location = /auth {
proxy_set_header Host $host;
proxy_pass http://192.168.16.2:8000;
proxy_pass_request_body off;
proxy_set_header Content-Length "";
proxy_set_header X-Original-URI $request_uri;
}
# Define the location for proxying
location / {
proxy_pass http://192.168.16.2:8000;
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;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment