Skip to content

Instantly share code, notes, and snippets.

View Sytten's full-sized avatar

Émile Fugulin Sytten

View GitHub Profile
@Sytten
Sytten / comparison.md
Created March 30, 2019 23:49
Comparison of Flask Framworks
Framework Input Validation Output Validation Typing Support Community Documentation Stars
Flask-apispec webargs Marshmallow Average Creator OK Poor 325
flask-rest-api webargs Marshmallow Average Research center Small OK 90
Flask-RESTPlus Custom Custom Average Abandonned Good OK 1400
Flask-Rebar Marshmallow Marshmallow Average Company None Good 50
Connexion OpenAPI OpenAPI Low Company Excellent Good 2000
@Sytten
Sytten / schemas.py
Created March 31, 2019 00:25
Flask-Rebar-2
from marshmallow import fields, Schema
class HealthSchema(Schema):
status = fields.String()
@Sytten
Sytten / app.py
Created March 31, 2019 00:28
Flask-Rebar-1
from flask import Flask
from flask_rebar import Rebar
rebar = Rebar()
registry = rebar.create_handler_registry(prefix='/api')
def create_app() -> Flask:
app = Flask(__name__)
@Sytten
Sytten / controller.py
Created March 31, 2019 00:34
Flask-Rebar-3
from flask_rebar import errors
from .app import registry
from .schemas import HealthSchema
@registry.handles(rule="/health", method="GET", marshal_schema=HealthSchema())
def get_health():
return {"status": "OK"}
@Sytten
Sytten / message.py
Created November 23, 2019 20:37
Snipped from Gunicorn
curr = lines.pop(0)
header_length = len(curr)
if curr.find(":") < 0:
raise InvalidHeader(curr.strip())
name, value = curr.split(":", 1)
name = name.rstrip(" \t").upper()
if HEADER_RE.search(name):
raise InvalidHeaderName(name)
@Sytten
Sytten / message.py
Created November 23, 2019 20:48
Snipped from gunicorn
def set_body_reader(self):
chunked = False
content_length = None
for (name, value) in self.headers:
if name == "CONTENT-LENGTH":
content_length = value
elif name == "TRANSFER-ENCODING":
chunked = value.lower() == "chunked"
elif name == "SEC-WEBSOCKET-KEY1":
content_length = 8
@Sytten
Sytten / Dockerfile
Last active October 21, 2021 01:19
Dockerfile for Typescript, Prisma2 and lerna
### BASE ###
FROM node:12-buster-slim AS base
RUN apt-get update && apt-get install --no-install-recommends --yes openssl
WORKDIR /app
### BUILDER ###
FROM base AS builder
@Sytten
Sytten / routes.py
Last active May 19, 2020 00:49
failing-revenge-route
@bp.route("/<school_name>/course/<course_id>")
@valid_school
def grades(school_name, course_id):
grades = _api(f'/course/{course_id}')
if grades == {}:
return render_template("error.html", message="Course not found")
return render_template("grades.html", school_name=school_name,
grades=grades['grades'],
course_name=grades['course_name'])
@Sytten
Sytten / Flag.py
Created May 19, 2020 00:36
failing-revenge-grade-flag
class FlagApi(Resource):
def get(self):
return jsonify({'status':'err','description':Config.FLAG_ONE})
@Sytten
Sytten / routes.py
Last active May 19, 2020 00:49
failing-revenge-error-handling
@bp.app_errorhandler(Exception)
def handle_exception(e):
if type(e) == json.JSONDecodeError:
message=f"Unexpected JSON object: {e.doc}"
elif type(e) == APIException:
message=f"APIError due to API answer: {e.response}"
else:
message=f"Unexpected error: {type(e).__name__}"
import traceback