Automatic generate a flask server project by command.(easy-flask)
Install
1 | pip3 install easy-flask-restful |
Make a flask server project at current dir:
1 | easy-flask -n demo_server |
By this tool you don`t need care how to make a flask server project.
What you need to do is write api-file in easy format inside dir api
.
This project will automatically load uri by api-file, and pack json response with same format.
Project structure
1 | . |
api
- write api files here.
internal.error
- define error info here.
app.py
- start app by this file, you can rename it.
conf
- config for application
control.sh
- run app with command by gunicorn
For example:
1 | from flask import g |
uri
- required by all api file
g.logger
is loaded before each request for making different log_id.
self.parse_request_data
can help you to parse params, it will return ParamsError
when param invalid.
Normal response:
1 | { |
Error response:
return errno
and msg
according to error.py
.
1 | { |
A real project must have complex logic and interactions with DB or other service.
By this tool, you just can get a simple framework. So I give some suggestions here for writing better code.
logic
dir (or other name) for writing complex logic, keep simple in api file.flask-sqlalchemy
, and mkdir dto
.client
dir for interact with other service, and make BaseClient
class(inherited by other client), because you never want to write same code for many times.