starlette-skin
A wrapper for starlette in order to code in a tornado-like way
Starlette is a flask-like python web framework, I write this module to enable a easy way which make starlette act like a tornado one.
I also make postgresql and redis easy-use interface in it.
installation
- pip install starlette-skin
usage
Use StarletteSkin instead of starlette to instantiate. I add three optional parameters:
- settings is a dict object of your own settings,
- enable_dbs is a list object which you can add 'pg' or 'redis'.
When you want to access db in handlers, you can use request.app._skin.pg/redis to achieve safe using of db which is a simplified way of using asyncpg and aioredis in which you have to always use async with statement.
from starlette_skin import StarletteSkin
from starlette.routing import Route
from starlette.responses import UJSONResponse
import uvicorn
settings = {'PG':{
'host':'127.0.0.1',
'port':5432,
'user':'postgres',
'password':'postgres',
'database':'community',
'min_size':10,
'max_size':10,
},
'REDIS':{
'address':('127.0.0.1', 6379),
'db':0,
'minsize':10,
'maxsize':10,
}
}
async def homepage(request):
user = await request.app._skin.pg('fetchval', 'select username from auth_user where id=$1', 1)
return UJSONResponse({'hello':user})
app = StarletteSkin(debug=False, routes=[Route('/', endpoint=homepage, methods=['GET'])], settings=settings, enable_dbs=['pg', 'redis'])
if __name__ == '__main__':
uvicorn.run(app, host='0.0.0.0', port=8000)