HistoryShow HN: Fastapi-MVC – generate customizable FastAPI projects

Show HN: Fastapi-MVC – generate customizable FastAPI projects

-

- Advertisment -

Example generated project: https://github.com/rszamszur/fastapi-mvc-example


Features

Generated project core implemented using MVC architectural pattern

Generated project is structured in MVC architectural pattern to help developers who don’t know FastAPI yet but are familiar with MVC to get up to speed quickly.

WSGI + ASGI for high performance and better configuration

TLDR;

Running WSGI as a master worker with ASGI workers gives better results than running pure ASGI (for FastAPI for now):

  • better performance (requests per second)
  • better support for different protocols
  • broader configuration
  • better support with using reverse proxy

- Advertisement -

First of all, whether it’s ASGI, WSGI, or combined, think of this as something that serves the application. For instance, Ruby on Rails uses Puma. The result of any of those servers is a TCP/IP or UNIX socket which is later on utilized by reverse proxy ex: Nginx (a TCP/IP socket you can access directly over the network, but still in production, usually it’ll be behind a reverse proxy).

Now to WSGI + ASGI part. FastAPI is implemented with asyncio (https://docs.python.org/3/library/asyncio.html), so having a pure WSGI server doesn’t make sense since you’d lose all the benefits of asynchronous concurrency. That’s where ASGI comes in. However, Python journey with asyncio is still pretty young. Most projects have yet to reach maturity level (you should expect early bugs and a limited feature set). FastAPI, as ASGI server uses uvicorn, which is still prior 1.x.x release (17 in total so far, current 0.16.0) and lacks support for some protocols (ex: no HTTP/2).
Moreover, some reverse proxy might not know how to work with asynchronous servers, and some problems or early bugs on this layer might happen as well.

I’m not saying uvicorn is bad. Quite contrary, if you’d run 4 pure uvicorn workes, you’d still get great results. But if you’d run the same amount of workers with gunicorn (WSGI) as a master worker, it turns out you can even pump those numbers up.

Gunicorn with 4 Uvicorn Workers (source: https://stackoverflow.com/a/62977786/10566747):

Requests per second: 7891.28 [#/sec] (mean)
Time per request: 126.722 [ms] (mean)
Time per request: 0.127 [ms] (mean, across all concurrent requests)
- Advertisement -

Pure Uvicorn with 4 workers:

Requests per second: 4359.68 [#/sec] (mean)
Time per request: 229.375 [ms] (mean)
Time per request: 0.229 [ms] (mean, across all concurrent requests)

~80% better

I guess gunicorn does a better job in worker management. However, it’s a more mature project, so it’s probably a matter of time when uvicorn (or other ASGI for that matter) will catch up to this benchmark.

Last but not least, gunicorn gives a ton of settings to configure (https://docs.gunicorn.org/en/stable/settings.html), which can come in handy.

Generated project comes with tests at 99% coverage
- Advertisement -

Unit test coverage is at 99% regardless of chosen configuration. There is also a placeholder for integration tests with an example dummy test.

Proper Dockerfile created with best practices for the cloud and Kubernetes

Container image features:

  • Based on lightweight alpine.
  • Run as PID 1 (with child processes)
  • Utilizes multi-stage build for smallest size possible, also this results in having only necessary libraries/dependencies/tools in outcome container image.
  • DigestSHA – immutable identifier instead of tags, for better reproducibility and security.
  • Signal handling, for Kubernetes to be able to gracefully shut down pods.
  • Created with common layers.

Based on Google Best practices for building containers and own experience.

Extensive GitHub actions for CI

ci_example

Helm chart for Kubernetes

For easily deploying application in Kubernetes cluster.

Reproducible virtualized development environment

Easily boot virtual machine with Vagrant. Code and test straight away.

Note: Project directory is rsync’ed between Host and Guest.

Note2: provided Vagrant vm doesn’t have port forwarding configured which means, that application won’t be accessible on Host OS.

Redis and aiohttp utilities

For your discretion, I’ve provided some basic utilities:

  • RedisClient .app.utils.redis
  • AiohttpClient .app.utils.aiohttp_client

They’re initialized in asgi.py on FastAPI startup event handler, and are available for whole application scope without passing object instances between controllers.

Kubernetes deployment with HA Redis cluster

Application stack in Kubernetes:
k8s_arch

Readable and documented code

The metrics stage in CI workflow ensures important PEP rules are enforced. For additional readability and formatting checks – black is used. Every piece of generated code is documented with docstrings. Last but not least there is also extended README with how to.

Configurable from env

Generated application provides flexibility of configuration. All significant settings are defined by the environment variables, each with the default value.

Quick start

$ pip install fastapi-mvc
$ fastapi-mvc new my-project
$ cd my-project
$ my-project serve

Prerequisites

Installation

Usage

This package exposes simple CLI for easier interaction:

$ fastapi-mvc --help
Usage: fastapi-mvc [OPTIONS] COMMAND [ARGS]...

  Generate and manage fastapi-mvc projects.

Options:
  -v, --verbose  Enable verbose logging.
  --help         Show this message and exit.

Commands:
  new  Create a new FastAPI application.
$ fastapi-mvc new --help
Usage: fastapi-mvc new [OPTIONS] APP_PATH

  Create a new FastAPI application.

  The 'fastapi-mvc new' command creates a new FastAPI application with a
  default directory structure and configuration at the path you specify.

Options:
  -R, --skip-redis                Skip Redis utility files.
  -A, --skip-aiohttp              Skip aiohttp utility files.
  -V, --skip-vagrantfile          Skip Vagrantfile.
  -H, --skip-helm                 Skip Helm chart files.
  -G, --skip-actions              Skip GitHub actions files.
  -C, --skip-codecov              Skip codecov in GitHub actions.
  -I, --skip-install              Dont run make install
  --license [MIT|BSD2|BSD3|ISC|Apache2.0|LGPLv3+|LGPLv3|LGPLv2+|LGPLv2|no]
                                  Choose license.  [default: MIT]
  --repo-url TEXT                 Repository url.
  --help                          Show this message and exit.

To generate new project:

> ~/.bashrc
– for zsh: $ echo ‘eval “$(_MY_PROJE

NOW WITH OVER +8500 USERS. people can Join Knowasiak for free. Sign up on Knowasiak.com
Read More

- Advertisement -
Charlie avatar
Charliehttps://plus.google.com/105215503769457384118
Fill your life with experiences so you always have a great story to tell

1 Comment

  1. Hi, author here. I wanted to share a project which generates a customizable FastAPI project core for high-performance applications[1, 2]. Based on MVC architectural pattern and WSGI + ASGI.

    As of today, FastAPI doesn't have any project generator like other known web frameworks ex: Django, Rails, etc., which makes creating new projects based on it that much more time-consuming. The idea behind this package is that one can generate a new project with one command and then start implementing endpoints logic straightaway, rather than creating the whole project from scratch. Moreover, the project is structured in MVC architectural pattern to help developers who don't know FastAPI yet but are familiar with MVC to get up to speed quickly.

    During development, I mainly focused on quality, code readability, reproducibility, and ease of use in the best way I could.

    Current features:

    * Generated project core implemented using MVC architectural pattern

    * WSGI + ASGI for high performance and better configuration

    * Generated project comes with tests at 99% coverage

    * Proper Dockerfile created with best practices for the cloud and Kubernetes

    * Extensive GitHub actions for CI

    * Reproducible virtualized development environment

    * Redis and aiohttp utilities

    * Kubernetes deployment with HA Redis cluster

    * Readable and documented code

    * Configurable from env

    Future development:

    * Databases integration

    * Generators for model, controler, scaffold, etc.

    * Community requests

    [1] https://stackoverflow.com/a/62977786/10566747
    [2] https://stackoverflow.com/a/63427961/10566747

You might also likeRELATED
Recommended to you

Google Play app with 500k downloads sent user contacts to Russian server

so many infections — Joker malware, which surreptitiously signs up users to pricey services, strikes again. Dan Goodin - Dec 16, 2021 10:21 pm UTC An Android app with more than 500,000 downloads from Google Play has been caught hosting malware that surreptitiously sends users’ contacts to an attacker-controlled server and signs up users to…

Robot Hacking Manual (RHM) v0.4. From robotics to cybersecurity

Read on what a security-first approach in here. ↩

Pixelfed

Is it possible install in an Apache own server with cPanel? I can't find a 'normal' script to send into the server and then install it.

Faker.js is now a community controlled project

January 14th, 2022👋 We're pretty excited to give new life to this project.We want the project to have a fresh start and become even cooler.What is Faker? #Faker is a library that generates fake (but reasonable) data for you. Mock data. Data for testing, development, and the like.Faker was first implemented in Perl in 2004…
- Advertisement -

AWS is not a dumb pipe

In 1997, David Isenberg wrote an article called the Rise of the Stupid Network. Isenberg worked at Bell Labs,...

Typed Config Languages

Posted on 2022-01-19Configuration languages are a serialization format used as an interface between humans (usually technical ones) and computers....

Must read

Show HN: K8s Cross-Cluster Application Migration in 1 Minute or Less

Σχετικά μεΤύποςΠνευματικά δικαιώματαΕπικοινωνήστε μαζί μαςΔημιουργοίΔιαφήμισηΠρογραμματιστέςΌροιΑπόρρητοΠολιτική και ασφάλειαΤρόπος λειτουργίας του...

Show HN: I built my own second brain software tool

I needed a good place for notes, so I built my own tool. If you want to give it a try you’ll find the project on Github in my X1 Repo. Ten years ago I used Dokuwiki for my website. I stored a lot of random notes there and I was productive using it. But,…
- Advertisement -