Skip to content

Commit

Permalink
1.0.5 - added middlewares and CORS
Browse files Browse the repository at this point in the history
  • Loading branch information
mauro-balades committed Oct 3, 2021
1 parent 40bba8b commit 97e05f0
Show file tree
Hide file tree
Showing 5 changed files with 566 additions and 3 deletions.
42 changes: 42 additions & 0 deletions expross/cors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""
The MIT License (MIT)
Copyright (c) 2021 expross
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""


class CORSMiddleware:
"""This is the middleware that applies a CORS object to requests.
Args:
cors (CORS, required): An instance of :py:class:`~falcon.cors.CORS`.
default_enabled (bool, optional): Whether CORS processing should
take place for every resource. Default ``True``.
"""

def __init__(self, cors, default_enabled=True):
self.cors = cors
self.default_enabled = default_enabled

def process_resource(self, req, resp, resource, *args):
if not getattr(resource, "cors_enabled", self.default_enabled):
return
cors = getattr(resource, "cors", self.cors)
cors.process(req, resp, resource)
11 changes: 11 additions & 0 deletions expross/log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import logging


def get_default_logger(level=None):
logger = logging.getLogger("expross")
logger.setLevel(logging.INFO)
logger.propogate = False
if not logger.handlers:
handler = logging.StreamHandler()
logger.addHandler(handler)
return logger
13 changes: 10 additions & 3 deletions expross/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import falcon
import os


class Expross(object):
"""Expross is a lightweight web server to introduce JavaScript developers familiar with Express to Python.
Expand Down Expand Up @@ -66,12 +67,14 @@ def __init__(self, *argv, **kwargs):
self.default_port: int = kwargs.get("port", self.default_port)
self.default_host_name: str = kwargs.get("host_name", self.default_port)

self.routes = []
self.errors = []
self.routes: list = []
self.errors: list = []
self.middlewares: list = kwargs.get("middlewares", [])

self.req: Request = None
self.res: Response = None

self.app: falcon.App = falcon.App()
self.app: falcon.App = falcon.App(middleware=self.middlewares)

# Jinja2 initialitaion
_templates: str = kwargs.get("templates", self.default_templates)
Expand Down Expand Up @@ -108,6 +111,10 @@ def set_templates(self, name: str = "templates"):
file_loader: FileSystemLoader = FileSystemLoader(name)
self.jinja_env: Environment = Environment(loader=file_loader)

def add_middleware(self, middleware):
self.app.add_middleware(middleware)
self.middlewares.append(middleware)

def error(self, error):
"""add an error handler to your app
Expand Down
Loading

0 comments on commit 97e05f0

Please sign in to comment.