项目作者: lordent

项目描述 :
Simple sanic boilerplate project
高级语言: Python
项目地址: git://github.com/lordent/sanic-boilerplate.git
创建时间: 2018-03-16T19:09:21Z
项目社区:https://github.com/lordent/sanic-boilerplate

开源协议:

下载


Sanic Boilerplate

This projet proposes a kickstarter python project based on the Sanic framework
It embeds :

Getting Started

To use this Boilerplate just clone the repo as follows :

  1. $ git clone https://github.com/lordent/sanic-boilerplate.git projectname
  2. $ cd projectname
  3. $ python3 -m venv .env
  4. $ source .env/bin/activate
  5. $ pip install -r requirements.txt

Once fetched, copy/paste the files to your local project repository and add your code (routes, …). See the section How to add routes
Before running your app, don’t forget to install the dependencies, see Installing the dependencies

Prerequisites

This boilerplate uses Sanic, so you must have Python 3.5 or higher installed

Installing the dependencies

The dependencies are listed in requirements.txt, you can simply install them with pip install -r requirements.txt

If you do not intend to use Sentry, just remove raven and raven_aiohttp from the requirements.txt file, and don’t forget to remove or comment the code in ``settings/sentry.py```

How to add routes

Let’s say you want to create a GET route /foo which displays “hello John Doe”, and a POST route which must pass a parameter “bar” :

Edit the file api/blueprints.py, import a new package at the head of the file like this:

  1. from api.content.foo import FooView

Then add a new route at the bottom of the file like this:

  1. content.add_route(FooView.as_view(), uri='/foo')

Edit the file api/content/foo.py, copy/paste the structure of the file welcome.py and adapt like this:

  1. from sanic import response
  2. from sanic.views import HTTPMethodView
  3. from marshmallow import Schema, fields
  4. from helpers.request import validate
  5. from oad import openapi
  6. class FooPost(Schema):
  7. """In this example, the POST method requires a "bar" parameter of type string
  8. More examples and tutorials can be found here : https://marshmallow.readthedocs.io/en/3.0/
  9. """
  10. bar = fields.String(required=True)
  11. @openapi.doc({
  12. 'description': 'The new route displaying "hello John Doe"',
  13. })
  14. class FooView(HTTPMethodView):
  15. @openapi.doc({
  16. 'summary': 'Foo get method example',
  17. })
  18. @openapi.response()
  19. async def get(self, request, parameter_id):
  20. return response.json('hello John Doe!')
  21. @openapi.doc({
  22. 'summary': 'Foo post method example',
  23. })
  24. @openapi.response({
  25. 'description': 'Foo result response',
  26. }, schema=FooPost(many=True))
  27. @validate.body(FooPost())
  28. async def post(self, request, parameter_id):
  29. return response.json(
  30. {'message': 'Your message to the user'}
  31. )

Add a file api/tests/test_foo.py, and write the pytest functions that make sense for you !

Running the tests

You can test the code by executing py.test at the root of the Boilerplate. This will run the tests located under tests, api/tests and helpers/tests. Thanks to pytest auto-discovery features, your new tests will be automatically run