From 82ab477af2a011334b3c3a648357755f6a0acebb Mon Sep 17 00:00:00 2001 From: Nigel Choi <9gel@users.noreply.github.com> Date: Sat, 12 Mar 2022 09:37:05 +0000 Subject: [PATCH 1/2] Better error handling for not found job id --- web.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/web.py b/web.py index ff30c90..90a8174 100644 --- a/web.py +++ b/web.py @@ -7,6 +7,7 @@ from os import stat, remove, makedirs, environ from time import strftime, time, localtime +from botocore.exceptions import ClientError import graphyte import requests from flask import jsonify, Flask, redirect, request, send_file @@ -530,15 +531,19 @@ def GET_v1_compile_job_id(job_id): 'created_at': job.created_at, 'enqueued_at': job.enqueued_at, 'id': job.id, - 'is_failed': job.is_failed or (job.result and job.result.get('returncode') != 0), + 'is_failed': job.is_failed, 'status': status, 'result': job.result, }) # Check for cached json if it's not in redis - job = get_job_metadata(job_id) - if job: - return jsonify(job) + try: + job = get_job_metadata(job_id) + if job: + return jsonify(job) + except ClientError as ex: + if ex.response['Error']['Code'] != 'NoSuchKey': + raise # Couldn't find it return error("Compile job not found", 404) From 9a542b897e80fc7e6251f040aed9add01c4b63ae Mon Sep 17 00:00:00 2001 From: Nigel Choi <9gel@users.noreply.github.com> Date: Sat, 12 Mar 2022 12:04:01 +0000 Subject: [PATCH 2/2] Added Ngix to protect gunicorn for production use --- Dockerfile | 4 +++ html/4xx.html | 8 ++++++ nginx.conf | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++ run | 2 ++ 4 files changed, 89 insertions(+) create mode 100644 html/4xx.html create mode 100644 nginx.conf diff --git a/Dockerfile b/Dockerfile index 49f105f..591ef38 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,14 @@ FROM python:3.7 MAINTAINER Zach White EXPOSE 5001 +EXPOSE 8080 WORKDIR /qmk_api COPY . /qmk_api RUN pip3 install -r requirements.txt git+git://github.com/qmk/qmk_compiler.git@master git+git://github.com/skullydazed/kle2xy.git@master +RUN apt-get update +RUN apt-get install -y nginx +COPY nginx.conf /etc/nginx/nginx.conf ENV LC_ALL=C.UTF-8 ENV LANG=C.UTF-8 CMD ./run diff --git a/html/4xx.html b/html/4xx.html new file mode 100644 index 0000000..c4cdaae --- /dev/null +++ b/html/4xx.html @@ -0,0 +1,8 @@ + + + QMK + + +

Not Found / Request Error

+ + diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..793e236 --- /dev/null +++ b/nginx.conf @@ -0,0 +1,75 @@ +worker_processes 1; + +#user nobody nogroup; +# 'user nobody nobody;' for systems with 'nobody' as a group instead +error_log stderr info; +pid /var/run/nginx.pid; + +events { + worker_connections 1024; # increase if you have lots of clients + accept_mutex off; # set to 'on' if nginx worker_processes > 1 + # 'use epoll;' to enable for Linux 2.6+ +} + +http { + include mime.types; + # fallback in case we can't determine a type + default_type application/octet-stream; + access_log /dev/stdout combined; + sendfile on; + + upstream app_server { + # fail_timeout=0 means we always retry an upstream even if it failed + # to return a good HTTP response + + # for a TCP configuration + server 127.0.0.1:5001 fail_timeout=0; + } + + root /qmk_api/html; + + #server { + # # if no Host match, close the connection to prevent host spoofing + # listen 80 default_server; + # return 444; + #} + + server { + # use 'listen 80 deferred;' for Linux + # use 'listen 80 accept_filter=httpready;' for FreeBSD + listen 8080; + client_max_body_size 40M; + + # set the correct host(s) for your site + server_name build-qmk.chonkerkeys.com; + + keepalive_timeout 5; + + location = / { + try_files /___sure_notfound___/$uri @app_proxy; + } + + location = /v1 { + try_files /___sure_notfound___/$uri @app_proxy; + } + + location ~ /v1/(healthcheck|compile|converters|keyboards|metrics|skeletons|telemetry|usb) { + try_files /___sure_notfound___/$uri @app_proxy; + } + + location @app_proxy { + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_set_header Host $http_host; + # we don't want nginx trying to do something clever with + # redirects, we set the Host: header above already. + proxy_redirect off; + proxy_pass http://app_server; + } + + error_page 400 401 402 403 404 405 406 /4xx.html; + location = /4xx.html { + internal; + } + } +} diff --git a/run b/run index 6cae309..34bc3f0 100755 --- a/run +++ b/run @@ -43,4 +43,6 @@ if [ -e /qmk_compiler ]; then pip3 install --editable /qmk_compiler || exit fi +service nginx start + exec gunicorn --access-logfile $ACCESS_LOGFILE --log-level $LOG_LEVEL -w $NUM_WORKERS -b 0.0.0.0:$QMK_API_PORT --max-requests $MAX_REQUESTS --max-requests-jitter $MAX_REQUESTS_JITTER -t $TIMEOUT $RELOAD web:app