Flask-restplus通过API路由服务静态

时间:2019-04-05 02:37:40

标签: python flask flask-restplus

我已经处理了两天了,但是还没有任何解决方案。最近,我想将我的应用程序中的路线从香草瓶路线转移到flask-restplus api路线,以简化我的招摇文档。但是,我发现我无法在我的根端点中提供我的静态文件夹(我正在使用webpack使用react生成的静态文件)。它说没有找到路线。

我一直在尝试在文档或任何其他内容中寻找答案,但是我只找到了来自香草瓶的解决方案。

这是代码:

import os
import json
import datetime
import pprint

from bson.objectid import ObjectId
from flask import Flask, Blueprint, send_from_directory
from flask_sqlalchemy import SQLAlchemy
from flask_security import SQLAlchemyUserDatastore, login_required
from flask_cors import CORS
from flask_restplus import Resource

from app.auth import oauth
from app.plugins import db, security, users, roles, api

def create_app(test_config=None):
    project_root = os.path.dirname(os.path.dirname(__file__))
    app = Flask(__name__, static_url_path='/public', static_folder='./public', instance_relative_config=True)
    app.config.from_object('instance.config.Config')
    app.config.from_pyfile('config.py', silent=True)
    CORS(app)

    # ensure the instance folder exists
    try:
        os.makedirs(app.instance_path)
    except OSError:
        pass 


    with app.app_context():
        # from config import Config
        from app.routes.main import routes_blueprint
        from app.routes.auth import routes_auth_blueprint
        from app.routes.db import routes_db_blueprint

        # setup DB 
        db.init_app(app)

        # setup security
        user_datastore = SQLAlchemyUserDatastore(db, users, roles)
        security.init_app(app, user_datastore)

        # create roles
        db.create_all()
        admin_role = user_datastore.find_or_create_role('admin')
        user_role = user_datastore.find_or_create_role('user')

        # set config for datastore
        app.config['user_datastore'] = user_datastore
        app.config['admin_role'] = admin_role
        app.config['user_role'] = user_role


        # setup RESTPlus API
        doc_blueprint = Blueprint('api', __name__, url_prefix='/api')
        api.init_app(doc_blueprint, version='0.1', title='BK API CONSOLE SERVER', description='Prototype of BK\'s next API server')

        # setup OAuth from Auth0
        oauth.init_app(app)

        @api.route('/', defaults={'path': ''})
        @api.route('/<path:path>')
        class Home(Resource):
            def get(self, path):
                print('path' + path)
                if path != "" and os.path.exists("public/build/" + path):
                    # print(f"path!= ''  = {os.path.exists('public/build' + path)}")
                    return send_from_directory(os.path.join(project_root, 'public/build'), path)
                else:
                    # print(f"else = {os.path.exists('public/build' + path)}")
                    return send_from_directory(os.path.join(project_root, 'public/build'), 'index.html')

        app.register_blueprint(doc_blueprint)
        app.register_blueprint(routes_blueprint)
        app.register_blueprint(routes_auth_blueprint)
        app.register_blueprint(routes_db_blueprint)

        return app

所以我的问题是如何在flask-restplus中使用静态广告?谢谢:)

0 个答案:

没有答案