Chalice: Routes in multiple python files

Created on 30 Jun 2018  路  2Comments  路  Source: aws/chalice

Can I create multiple files with routes? I would like to separate by "modules" and each module be a folder with the routes and all scripts. In the release note of 0.4 it is commented that the "chalicelib" folder has been added but does not speak if I can get the routes from the "app.pyf".

As I understand it, what I can do is create all the routes in the main file and make imports and each auxiliary function. But I do not think it would be a clean project when I hit 40 API calls.

A current test:
(importing the scripts only when the correct function is requested)

app.py

app.route('/recordings',
           methods=['POST'])
def create_recording():
    from chalicelib.polly_s3 import synthesize_speech, upload_to_s3, index_in_dynamodb;

    request = app.current_request
    body = request.json_body
    record_id = str(uuid.uuid4())
    text = body.get("text")
    voice = 'Vitoria'

    synthesize_speech(record_id, text, voice)
    url = upload_to_s3(record_id, S3_BUCKET)
    item = index_in_dynamodb(record_id, text, voice, url, DYNAMO_DB_TABLE)
    return [item]

I wonder if this is a viable solution:

app.py:
import chalicelib

chalicelib > __init__.py:
from . import users, teams

chalicelib > users > __init__.py:
from . import route

chalicelib > users > route.py:

from app import app
from . import controller

@app.route("/users/test")
def test():
    return controller.test()
feature-request

Most helpful comment

This won't work currently because the actual handler function needs to be located in app.py. Its intended to be the one place all your route information is stored in. I'll mark this as a feature request.

All 2 comments

This won't work currently because the actual handler function needs to be located in app.py. Its intended to be the one place all your route information is stored in. I'll mark this as a feature request.

@stealthycoin I'm glad that this could go into a queue for improvements, but you see, the way I did it apparently works: I imported the app object from app.py into the final file, there I did the decorator and it worked.

Now the question is: If my system has 6 "modules", in this logic, will it load my costs with AWS? I ask this, because what I understand is that each call the system will load 100% of my code and will spend more RAM, to perform a simple function lambda. No?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nedlowe picture nedlowe  路  3Comments

vrinda1410 picture vrinda1410  路  3Comments

variable picture variable  路  4Comments

cdalar picture cdalar  路  4Comments

Erstwild picture Erstwild  路  4Comments