A python microservice framework
Overview
Quick start
Project files
OpenAPI specification
API objects
Server code
Deployment pipeline
Docker packaging
JWT authentication
Configuration
Error handling
Asynchronous execution
Database serialisation
Testing
Monitoring
The only code you need to write to implement your pymacaron microservice is:
A ‘server.py’ file, which you can mostly copy/paste unchanged from pymacaron-helloworld/server.py
One method for every endpoint, located in python modules whose path are specified in the OpenAPI specifications via the ‘x-bind-server’ attribute.
A standard PyMacaron ‘server.py’ looks like:
import os
import sys
import logging
from flask import Flask
from flask_cors import CORS
from pymacaron import API, letsgo
log = logging.getLogger(__name__)
# WARNING: you must declare the Flask app as show
# below, keeping the variable name 'app' and the
# file name 'server.py', since gunicorn is configured
# to lookup the variable 'app' inside the code
# generated from 'server.py'.
app = Flask(__name__)
CORS(app)
# Here you could add custom routes, etc.
def start(port=80, debug=False):
# Your swagger api files are under ./apis, but you
# could have them anywhere else really.
here = os.path.dirname(os.path.realpath(__file__))
path_apis = os.path.join(here, "apis")
# Tell pymacaron to spawn apis inside this Flask app.
# Set the server's listening port, set Flask debug
# mode on or not. Other configuration parameters,
# such as JWT issuer, audience and secret, are set
# in 'pym-config.yaml'.
api = API(
app,
port=port,
debug=debug,
)
# Find all API specifications and load them into
# pymacaron-core
api.load_apis(path_apis)
# Optionally, publish the api specifications under
# the route '/doc/<api-name>', so you may read them
# in the Swagger-UI:
# api.publish_apis()
# Start the Flask app and serve all endpoints
# defined in 'apis/myservice.yaml'
api.start(serve="myservice")
# Entrypoint
letsgo(__name__, callback=start)
You start your server by going into the project’s root directory and doing:
python server.py --port 8080
See pymacaron-helloworld for examples.