yacoma / morepath-realworld-example-app

Exemplary real world backend API built with Morepath (Python)
https://realworld.io
MIT License
5 stars 0 forks source link

Morepath RealWorld Example App

Morepath codebase containing real world examples (CRUD, auth, advanced patterns, etc) that adheres to the RealWorld spec and API.

Demo

This codebase was created to demonstrate a fully fledged fullstack application built with Morepath, using Pony object-relational mapper, including CRUD operations, authentication, routing, pagination, and more.

We've gone to great lengths to adhere to the Morepath community styleguides & best practices.

For more information on how to this works with other frontends/backends, head over to the RealWorld repo.

A demo conduit-morepath backend server is running at https://conduit.yacoma.it/api.

Getting started

Clone this repo and adjust the settings in the settings folder. Remember to change the master_secret.

From inside the project directory create a clean Python environment with virtualenv and activate it:

$ virtualenv -p python3 env
$ source env/bin/activate

After this you can install the package including dependencies using:

(env) $ pip install -Ue .

Once that is done you can start the server:

(env) $ gunicorn conduit.run

You can access the conduit server at http://localhost:8000/api.

You can also start the server on another host/port:

(env) $ gunicorn --bind=example.com:3000 conduit.run

Code Overview

Dependencies

Application Structure

The core application under the conduit folder contains:

The subapplication folders auth/ and blog/ contain:

Error Handling

For validating the incoming JSON data on requests we use Cerberus. The Cerberus schemas are defined in schema.yml inside the app folders (auth/ and blog/). You can also add custom validators, e.g. the EmailValidator defined in auth/validator.py.

Cerberus returns an ValidationError. How to handle the response including returning a 422 status code and formatting the view is defined in error_view.py.

Authentication

We use the Morepath identity policy provided by more.jwtauth which allows authentication of request through the JWT token in the Authorization header.

Testing

For installing the test suite and running the tests use:

(env) $ pip install -Ur requirements/develop.txt
(env) $ py.test

To check for test coverage:

(env) $ py.test --cov

The example App ships with 100% test coverage.

To check if your code is conform with PEP8:

(env) $ flake8 conduit setup.py

You can also run tox locally if you have installed Python 3.5 or/and Python 3.6:

(env) $ tox

Install pre-commit hook for Black integration

We're using Black for formatting the code and it's recommended to install the pre-commit hook for Black integration before committing:

$ pre-commit install

Black

To format the code with the Black Code Formatter run in the root directory:

$ black .

Black has also integration for the most popular editors.

Deployment

Requirements for the server

On Debian/Ubuntu you can install them as superuser with:

$ apt-get install nginx supervisor make

Overview

We use a post-receive git hook to publish the repository on the production server. He triggers on every push to the git repo on the server.

The post-receive hook uses the path defined in the prod_path variable. Make sure that this path exists on the server before pushing the first time.

The hook triggers make deploylive which is defined in Makefile. This install the dependencies and build the App.

Configuration

The deploy/conf directory contains samples for git hook and server configuration with gunicord behind a nginx reverse proxy. For monitoring and controlling gunicord we use supervisor.

The samples have to be configured by replacing the placeholders indicated by [PLACEHOLDER].

The following placeholders are used:

Serve a conduit client together with the backend

Optionally you can also serve a conduit client like the Cerebral Example App together with the Morepath backend.

Database

PonyORM supports SQLite, PostgreSQL, MySQL/MariaDB and Oracle. In the example App we use Postgres in production and SQLite for development and testing.

When you want to use another database then SQLite you have to first create a database for conduit-morepath on the server.

Then configure conduit/settings/production.yml according to the database setup.