Beispiel #1
0
def create_app(env_name):
    app = Flask(__name__)
    app.config.from_object(app_config[env_name])
    api = Api(app)

    bcrypt.init_app(app)
    db.init_app(app)
    jwt.init_app(app)
    cors.init_app(app)
    app.errorhandler(InvalidUsage)(error_handler)

    api.add_resource(UserRegister, '/register')
    api.add_resource(UserLogin, '/login')
    api.add_resource(UserMe, '/me')
    api.add_resource(UserResource, '/@<string:username>')
    api.add_resource(PostsByUserResource, '/@<string:username>/posts')
    api.add_resource(FavoritePostsByUserResource,
                     '/@<string:username>/favorites')
    api.add_resource(TagResource, '/tags')
    api.add_resource(PostsResource, '/posts')
    api.add_resource(PostResource, '/posts/<int:post_id>')
    api.add_resource(FavoriteResource, '/posts/<int:post_id>/favorite')
    api.add_resource(CommentsResource, '/posts/<int:post_id>/comments')
    api.add_resource(CommentResource,
                     '/posts/<int:post_id>/comments/<int:comment_id>')

    @app.route('/', methods=['GET'])
    def index():
        return 'Welcome'

    return app
Beispiel #2
0
def register_extensions(app):
    """Register Flask extensions."""
    bcrypt.init_app(app)
    cache.init_app(app)
    db.init_app(app)
    migrate.init_app(app, db)
    jwt.init_app(app)
Beispiel #3
0
def create_app(config_class=DevelopmentConfig):
    app = Flask(__name__)
    app.config.from_object(config_class)
    load_dotenv()
    jwt.init_app(app)
    # CORS(app)
    api = Api(app)
    db.init_app(app)
    app.cli.add_command(create_tables)  # To interact with app from CLI
    b_crypt.init_app(app)
    ma.init_app(app)
    migrate = Migrate(app, db)
    # oauth.init_app(app)

    # USER API
    api.add_resource(UserRegister, '/user/register')
    api.add_resource(UserLogin, '/user/login')
    api.add_resource(UserLogout, '/user/logout')
    api.add_resource(UserPasswordRestoreRequest, '/user/restore')
    api.add_resource(UserPasswordReSetter, '/user/restore/<string:token>')
    api.add_resource(User, '/user/<int:_id>')
    api.add_resource(UserList, '/users/<int:limit>')
    api.add_resource(TokenRefresher, '/user/refreshing')
    api.add_resource(UserEmail2FA, '/user/fa2_auth/<string:token>')

    # REQUEST API
    api.add_resource(RequestsList, '/requests')
    api.add_resource(RequestCreation, '/requests/new')

    print(f"App current configuration: {config_class.CONFIG_NAME}")

    # OAuth API
    # api.add_resource(GithubLogin, "/login/oauth/github")
    # api.add_resource(GithubAuthorize, "/login/oauth/github/authorized")

    # CONFIRMATION API
    api.add_resource(Confirmation, '/user/confirmation/<string:confirmation_id>')

    # api.add_resource(User, '/users/<int:user_id>')
    api.add_resource(Content, '/content')

    @app.route('/')
    def home():
        return render_template("index.html")

    # Logging
    log_level = logging.INFO if app.config['DEBUG'] else logging.ERROR
    handler = WatchedFileHandler('server.log')
    formatter = logging.Formatter('%(asctime)s | %(levelname)s: %(message)s',
                                  '%d-%m-%Y %H:%M:%S')
    handler.setFormatter(formatter)
    root = logging.getLogger()
    root.setLevel(log_level)
    root.addHandler(handler)
    logging.info('\n------------------- Starting Server -------------------')

    return app
Beispiel #4
0
def register_extensions(app):
    """Register Flask extensions."""
    origins = app.config.get('CORS_ORIGIN_WHITELIST', '*')
    cors.init_app(app, resources={r"/api/*": {"origins": origins}})
    bcrypt.init_app(app)
    cache.init_app(app)
    db.init_app(app)
    migrate.init_app(app, db)
    jwt.init_app(app)
    docs.init_app(app)
    api.init_app(app)
Beispiel #5
0
def create_app():
    app = Flask(__name__)
    app.config.from_object(Config)
    api.init_app(app)
    jwt.init_app(app)
    db.init_app(app)

    @app.before_first_request
    def create_tables():
        db.create_all()

    return app
def create_app():
    app = Flask(__name__)
    app.secret_key = 'ChangeMe!'
    app.config['JWT_BLACKLIST_ENABLED'] = True
    app.config['JWT_BLACKLIST_TOKEN_CHECKS'] = ['access', 'refresh']
    app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite://"
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

    db.init_app(app)
    jwt.init_app(app)

    # In a real application, these would likely be blueprints
    register_endpoints(app)

    return app
Beispiel #7
0
def create_app(config_class='configurations.py'):
    app = Flask(__name__)
    app.config.from_object(ProductionConfig)
    load_dotenv()
    jwt.init_app(app)
    api = Api(app)
    db.init_app(app)
    app.cli.add_command(create_tables)  # To interact with app from CLI
    b_crypt.init_app(app)
    ma.init_app(app)
    migrate = Migrate(app, db)
    oauth.init_app(app)

    # USER API
    api.add_resource(UserRegister, '/users/register')
    api.add_resource(UserLogin, '/users/login')
    api.add_resource(UserLogout, '/users/logout')
    api.add_resource(User, '/user/<int:_id>')
    api.add_resource(UserList, '/users/<int:limit>')
    api.add_resource(TokenRefresher, '/users/refreshing')
    api.add_resource(UserEmail2FA, '/users/fa2_auth/<string:token>')

    # OAuth API
    api.add_resource(GithubLogin, "/login/oauth/github")
    api.add_resource(GithubAuthorize, "/login/oauth/github/authorized")

    # CONFIRMATION API
    api.add_resource(Confirmation, '/user_id/<string:confirmation_id>')

    # resources.add_resource(User, '/users/<int:user_id>')
    api.add_resource(CreatePost, '/posts/create')
    api.add_resource(Content, '/content')

    # Logging
    log_level = logging.INFO if app.config['DEBUG'] else logging.ERROR
    handler = WatchedFileHandler('server.log')
    formatter = logging.Formatter('%(asctime)s | %(levelname)s: %(message)s',
                                  '%d-%m-%Y %H:%M:%S')
    handler.setFormatter(formatter)
    root = logging.getLogger()
    root.setLevel(log_level)
    root.addHandler(handler)
    logging.info('\n------------------- Starting Server -------------------')

    return app