Skip to navigation
Example for a simple middleware for Falcon Python Logger_middleware
30.11.21
class Logger_middleware(object): def process_response(self, req, resp, view, p): print(dir(req)) def get_app(): cors = CORS(allow_origins_list=['*'], allow_all_origins=True, allow_all_headers=True, allow_all_methods=True) api = falcon.API(middleware=[cors.middleware, Logger_middleware()]) api.add_static_route('/static', os.path.abspath("api/templates/static/")) routes = [] env = Environment( loader=PackageLoader('api', 'templates'),
https://gist.github.com/jennyd/0ac8d38167bdabb9bc07
Reply
Anonymous
#!env python import os import falcon import yaml import time, datetime import random import couchdb2 from falcon_cors import CORS from multiprocessing import Process from jinja2 import Environment, PackageLoader, select_autoescape from api.views import index class Logger_middleware(object): def process_response(self, req, resp, view, p): print("...loggin") p = Process(target=self.log_to_couchdb, args=(req,)) p.start() def log_to_couchdb(self, req): data = { "_id": "{0}_api3{1}".format(int(time.time()),random.randint(10,99)), 'loggin': 'API3', 'IP': req.remote_addr, 'page': str(req.uri), 'url': str(req.url), 'method': str(req.method), 'user_agent': req.user_agent, "log_api": "https://couchdb.foo.bar.com/logger", 'unixtime': int(time.time()), 'date': datetime.datetime.fromtimestamp(int(time.time())).strftime('%Y%m%d%H%M'), 'fingerprint': '', 'timezone': '-7', } url = "http://couchdb.foo.bar.com" try: server = couchdb2.Server(href=url,username='captain',password='Tamitasmalogr',use_session=False,ca_file=None) db = server.get("logger") db.put(data) except Exception as e: print(e) def get_app(): cors = CORS(allow_origins_list=['*'], allow_all_origins=True, allow_all_headers=True, allow_all_methods=True) api = falcon.API(middleware=[cors.middleware, Logger_middleware()]) api.add_static_route('/static', os.path.abspath("api/templates/static/")) routes = [] env = Environment( loader=PackageLoader('api', 'templates'), autoescape=select_autoescape(['html', 'xml'], default_for_string=False) ) with open("config.yml", 'r') as stream: try: config = yaml.safe_load(stream) config['env'] = env routes.append(('/index', index.Main(config, routes, env))) except yaml.YAMLError as exc: config = False if config: for route in routes: api.add_route(*route) return api
30.11.21
Reply
Anonymous
Information Epoch 1732519341
Build a prototype as soon as possible.
Home
Notebook
Contact us