Skip to content

crazytruth/insanic

Repository files navigation

Insanic

Insanic

Build Status Documentation Status Codecov

PyPI pyversions PyPI version PyPI license Black

Insanic is a microservice framework that extends sanic. It tries to include all the best practices for developing in a microservice architecture. To do this, certain stacks were needed, as a result Insanic is a pretty opinionated framework.

Think of this as django-rest-framework is to django but for microservice usage (and a lot less functionality than drf).

Why we needed this

We needed this because we need a framework for our developers to quickly develop services while migrating to a microservice architecture.

As stated before, this is very opinionated and the reason being, to reduce research time when trying to select packages to use for their service. It lays down all the necessary patterns and bootstraps the application for quick cycle time between idea and deployment.

FEATURES

  • Authentication and Authorization for Users and other Services (like drf)
  • Easy Service Requests
  • Normalized Error Message Formats
  • Connection manager to redis
  • Utils for extracting public routes (will help when registering to api gateway)
  • Bootstrap monitoring endpoints
  • Throttling

Documentation

For more detailed information please refer to the documentation

Installation

Prerequisites

Core dependencies include:

  • sanic - extends sanic
  • httpx - to make async requests to other services
  • PyJWT - for authentication
  • Redis - for cache and throttling

To install:

$ pip install insanic-framework

Usage

For very basic usage, it is pretty much the same as Sanic:

  1. Create a python file. ex. app.py
from insanic import Insanic
from insanic.conf import settings
from sanic.response import json

settings.configure()
__version__ = "0.1.0"

app = Insanic(__name__, version=__version__)

@app.route('/')
async def example(request):
    return json({"insanic": "Gotta go insanely fast!"})

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8000)
  1. Run with
python run.py
  1. Check in browser or curl
curl http://localhost:8000/

For more examples and usage, please refer to the documentation.

Testing

Insanic tests are run with pytest and tox.

$ pytest

# with coverage
$ pytest --cov=insanic --cov-report term-missing:skip-covered

# a certain set of tests
$ pytest --pytest-args tests/test_pact.py

# tox, run for sanic > 19.12 and python >= 3.6
$ tox

Release History

For full changelogs, please refer to the CHANGELOG.rst.

Since Insanic was initially developed and released internally, for changes made during that period, please refer to CHANGELOG_LEGACY.rst.

Contributing

For guidance on setting up a development environment and how to make a contribution to Insanic, see the CONTRIBUTING.rst guidelines.

Known Issues

  • Insanic cannot run with more than 1 worker.

Meta

Distributed under the MIT license. See LICENSE for more information.

Thanks to all the people at my prior company that worked with me to make this possible.