Search code examples
pythonroutesbottle

Route bottle server two subfolders?


Two subfolders are under same folder called frontend.

One is app, another is dist.

in my main.py, i route like following

@bottle.route('/') 
def server_static(filename="index.html"):
  return static_file(filename, root='./frontend/dist/')

@bottle.route('/<filepath:path>')
def server_static(filepath):
  return static_file(file path, root='./frontend/dist/')

Now when user visit the main site url like www.example.com/, they could successfully load everyting from folder dist.

But I wish to add a specific routing for folder app. so that when user visit www.example.com/dev/, everything from folder app will be loaded.

I have tried

@bottle.route('/dev') 
def server_static(filename="index.html"):
  return static_file(filename, root='./frontend/app/')

@bottle.route('/dev/<filepath:path>')
def server_static(filepath):
  return static_file(file path, root='./frontend/app/')

But this simply doesn't work. I think it's due to my using of filepath.

Anyone may kindly advise on how to route in this scenario?


Solution

  • When two routes match, the one declared first is chosen. You need to declare your most specific routes first:

    @bottle.route('/') 
    ...
    
    @bottle.route('/dev') 
    ...
    @bottle.route('/dev/<filepath:path>')
    ...
    
    # because this matches the previous two routes, it must come after them
    @bottle.route('/<filepath:path>')
    ...