Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from eve import Eve
- from eve.auth import BasicAuth
- from myApp.auth import * #this is the auth.py
- from myApp.rate_limit import *
- from myApp.callbacks import inject_signature, log_every_get
- from myApp.middleware import Loggermiddleware
- from myApp.blueprints import simple_page, page_group_by
- from myApp.models import db
- import os
- import logging
- from logging.handlers import TimedRotatingFileHandler
- # from logbook import Logger, StreamHandler
- import redis
- r = redis.StrictRedis(host='127.0.0.1', password='blabla')
- class MyBasicAuth(BasicAuth):
- def check_auth(self, username, password, allowed_roles, resource, method):
- return username == 'admin' and password == 'secret'
- # Eve App Definition
- SETTINGS_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'instance/settings.py')
- LOGS_PATH = '/var/log/blabla.log'
- app = Eve(auth=MyBasicAuth, redis=r, settings=SETTINGS_PATH)
- # MongoDB
- db.init_app(app)
- # BluePrints (routs)
- app.register_blueprint(simple_page)
- app.register_blueprint(page_group_by)
- # Hooks / Callbacks
- app.on_post_GET += log_every_get
- app.on_fetched_item += inject_signature
- # Middleware
- app.wsgi_app = Loggermiddleware(app.wsgi_app)
- # Hooks
- @app.after_request
- def do_something_whenever_a_request_has_been_handled(response):
- # we have a response to manipulate, always return one
- print('after_request --> 02')
- return response
- @app.after_request
- def inject_x_rate_headers(response):
- print('after_request --> 01')
- limit = get_view_rate_limit()
- if limit and limit.send_x_headers:
- h = response.headers
- h.add('X-RateLimit-Remaining', str(limit.remaining))
- h.add('X-RateLimit-Limit', str(limit.limit))
- h.add('X-RateLimit-Reset', str(limit.reset))
- return response
- if __name__ == '__main__':
- handler = TimedRotatingFileHandler(LOGS_PATH)
- handler.setFormatter(logging.Formatter(
- '%(asctime)s %(levelname)s: %(message)s '
- '[in %(filename)s:%(lineno)d] -- ip: %(clientip)s, '
- 'url: %(url)s, method:%(method)s'))
- app.logger.addHandler(handler)
- app.logger.setLevel(logging.DEBUG)
- # let's go
- app.run(host='0.0.0.0', port=8000)
- SERVER_NAME = None
- DEBUG = True
- ENV = 'development'
- URL_PREFIX = 'api'
- API_VERSION = 'v1'
- SORTING = True
- # PAGINATION_DEFAULT = 25
- # PAGINATION_LIMIT = 50
- CACHE_CONTROL = 'max-age=15,must-revalidate'
- CACHE_EXPIRES = 15
- RATE_LIMIT_GET = (60, 60 * 1)
- RENDERERS = ['eve.render.JSONRenderer']
- # CORS support
- X_DOMAINS = '*'
- # RESOURCE_METHODS = ['GET', 'POST', 'DELETE']
- RESOURCE_METHODS = ['GET']
- # ITEM_METHODS = ['GET', 'PATCH', 'PUT', 'DELETE']
- ITEM_METHODS = ['GET']
- from eve.auth import TokenAuth
- from flask import Response, abort, request, current_app
- class MyTokenAuth(TokenAuth):
- def check_auth(self, token, allowed_roles, resource, method):
- """For the purpose of this example the implementation is as simple as
- possible. A 'real' token should probably contain a hash of the
- username/password combo, which should then validated against the account
- data stored on the DB.
- """
- print('TOKEN: {}'.format(token))
- accounts = current_app.data.driver.db['accounts']
- account = accounts.find_one({'token': token})
- print('-- ACCOUNT: {}'.format(account))
- if account:
- active = account["active"]
- if active:
- return True
- return False
- def authenticate(self):
- """ Returns a standard a 401. Override if you want to change the
- response.
- """
- resp = Response(None, 401, {'WWW-Authenticate': 'Basic realm="%s"' %
- __package__})
- abort(401, description='Please provide proper credentials :)',
- response=resp)
Add Comment
Please, Sign In to add comment