import urllib from flask import Flask, request, make_response, render_template import config import db import utils app = Flask(__name__, static_url_path='') db_connection = db.DbAccess(config.DATABASE_FILE_PATH) # Prometheus metrics if config.EXPOSE_METRICS: from metrics import init_metrics init_metrics(app, db_connection) def add_cookie(response, url): """ Add a cookie that may later be checked for repeated requests in small amounts of time """ response.set_cookie(url, utils.get_cookie_value_to_set(), expires=utils.get_expiration(), secure=True, samesite="none") def make_text_response(count, url, cookie_required): """ Create a request with the count with a 200 status and give cookie back """ response = make_response(str(count), 200) if cookie_required: add_cookie(response, url) return response
import config import db import utils from flask import Flask, request, make_response, render_template app = Flask(__name__, static_url_path='') db_connection = db.DbAccess(config.DATABASE_FILENAME) if config.ENABLE_SSL: from flask_sslify import SSLify sslify = SSLify(app) def makeTextRequest(count, url, cookie_required): """ Create a request with the count with a 200 status and give cookie back """ response = make_response(str(count), 200) if cookie_required: response.set_cookie(url, utils.getCookieValueToSet(), expires=utils.getExpiration()) return response def makeSVGRequest(count, url, cookie_required, alternative): sizes = utils.calculateSVGSizes(count) svg = utils.getSVG( count, sizes['width'], sizes['recWidth'], sizes['textX'], url).encode( 'utf-8') if alternative == False else utils.getAlternativeSVG( count, url) response = make_response(svg, 200) response.content_type = 'image/svg+xml'
def setup_class(cls): cls.db_access = db.DbAccess(settings.DB_HOST, settings.DB_USER, settings.DB_PASSWORD, settings.DB_NAME)