Skip to content

Commit

Permalink
Merge pull request #5 from josephmancuso/develop
Browse files Browse the repository at this point in the history
Adding Middleware Support
  • Loading branch information
josephmancuso committed Jan 14, 2018
2 parents 4dd5984 + 2e2e9c8 commit b699c11
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 50 deletions.
10 changes: 10 additions & 0 deletions masonite/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def __init__(self, environ):
self.url_params = None
self.redirect_url = False
self.redirect_route = False
self.user_model = None

def input(self, param):
''' Returns either the FORM_PARAMS during a POST request
Expand Down Expand Up @@ -76,6 +77,15 @@ def get_cookie(self, provided_cookie):

return None

def set_user(self, user_model):
''' Loads the user into the class '''
self.user_model = user_model
return self

def user(self):
''' Retreives the user model '''
return self.user_model

def redirect(self, route):
''' Redirect the user based on the route specified '''
self.redirect_url = route
Expand Down
97 changes: 47 additions & 50 deletions masonite/routes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
''' Module for the Routing System '''
import json
import re
import importlib
import re
from pydoc import locate

from config import middleware

class Route():
''' Loads the environ '''
Expand Down Expand Up @@ -63,81 +65,76 @@ def compile_route_to_regex(self, route):

def generated_url_list(self):
return self.url_list
class Get():
''' Class for specifying GET requests '''

def __init__(self):
self.method_type = 'GET'
self.continueroute = True
self.output = False
self.route_url = None
self.named_route = None

class BaseHttpRoute(object):
method_type = 'GET'
continueroute = True
output = False
route_url = None
request = None
named_route = None
list_middleware = []

def route(self, route, output):
''' The model route given by the developer.
The output parameter is a controller and method
'''
''' Loads the route into the class '''

if isinstance(output, str):
mod = output.split('@')

# import the module
# Import the module
module = importlib.import_module('app.http.controllers.' + mod[0])

# get the controller from the module
# Get the controller from the module
controller = getattr(module, mod[0])

# get the view from the controller
# Get the view from the controller
view = getattr(controller(), mod[1])
self.output = view
else:
self.output = output

self.route_url = route
return self

def middleware(self, middleware):
''' Blocking middleware '''
self.continueroute = bool(middleware)
return self

def name(self, name):
''' Specifies the name of the route '''
self.named_route = name
return self

class Post():
''' Class for specifying POST requests '''
def load_request(self, request):
''' Load the request into this class '''
self.request = request
return self

def __init__(self):
self.method_type = 'POST'
self.continueroute = True
self.output = False
self.route_url = None
def middleware(self, *args):
''' Loads a list of middleware to run '''
self.list_middleware = args
return self

def route(self, route, output):
''' Loads the route into the class '''
def run_middleware(self, type_of_middleware):
''' type_of_middleware should be a string that contains either 'before' or 'after' '''

if isinstance(output, str):
mod = output.split('@')
# Get the list of middleware to run for a route.
for arg in self.list_middleware:

# import the module
module = importlib.import_module('app.http.controllers.' + mod[0])
# Locate the middleware based on the string specified
located_middleware = locate(middleware.ROUTE_MIDDLEWARE[arg])(self.request)

# get the controller from the module
controller = getattr(module, mod[0])
# If the middleware has the specific type of middleware (before or after)
# then execute that
if hasattr(located_middleware, type_of_middleware):
getattr(located_middleware, type_of_middleware)()

# get the view from the controller
view = getattr(controller(), mod[1])
self.output = view
else:
self.output = output
self.route_url = route
return self
class Get(BaseHttpRoute):
''' Class for specifying GET requests '''

def middleware(self, middleware):
''' Blocking middleware '''
self.continueroute = bool(middleware)
return self
def __init__(self):
self.method_type = 'GET'

class Post(BaseHttpRoute):
''' Class for specifying POST requests '''

def __init__(self):
self.method_type = 'POST'

class Api():
''' API class docstring '''
Expand All @@ -158,7 +155,7 @@ def model(self, model):
''' Loads the model into the class '''
if not self.url:
self.url = '/api/' +model.__name__.lower()
print('the route is ' + self.url)
print('API Route: ' + self.url)

self.model_obj = model
return self
Expand Down

0 comments on commit b699c11

Please sign in to comment.