2

I have multiple routes which have the same URL root. Example:

  • abc/def/upload
  • abc/def/list
  • abc/def/page/{page_id}

Can I define abc/def to be URL root. (Something similar to what can be done in Java using Spring or Apache CXF)

Thanks

Harshdeep
  • 5,614
  • 10
  • 37
  • 45

4 Answers4

5

In flask-restful it is possible to prefix all your routes on api initialization:

>>> app = Flask(__name__)
>>> api = restful.Api(app, prefix='/abc/def')

You can then wire your resources ignoring any prefixes:

>>> api.add_resource(MyResource, '/upload')
>>> ...
el.atomo
  • 5,200
  • 3
  • 30
  • 28
3

You can use the APPLICATION_ROOT key for your app's config.

app.config['APPLICATION_ROOT'] = "/abc/def"

source - Add a prefix to all Flask routes

Community
  • 1
  • 1
Bhargav
  • 898
  • 1
  • 14
  • 32
0

I needed similar so called "context-root". I did it in conf file under /etc/httpd/conf.d/ using WSGIScriptAlias :

myapp.conf

<VirtualHost *:80>
    WSGIScriptAlias /myapp /home/<myid>/myapp/wsgi.py

    <Directory /home/<myid>/myapp>
        Order deny,allow
        Allow from all
    </Directory>

</VirtualHost>

So now I can access my app as : http://localhost:5000/myapp

See the guide - http://modwsgi.readthedocs.io/en/develop/user-guides/quick-configuration-guide.html

dganesh2002
  • 1,917
  • 1
  • 26
  • 29
0

Just use Blueprint from Flask:

app = Flask(__name__)
bp = Blueprint('', __name__, url_prefix="/abc/def")

def upload():
    pass

def list():
    pass

bp.add_url_rule('/upload', 'upload', view_func=upload)
bp.add_url_rule('/list', 'list', view_func=list)
...

app.register_blueprint(bp)
renatodamas
  • 16,555
  • 8
  • 30
  • 51