Skip to main content

Introduction

Configuration

The base configuration file is located on the config dir.

SSL Encryption

For enabling ssl encryption within the application, you will need to add in the "SERVER" key entry in the config file

  SSL:
    Certificate: "path to the cer file (public key)"
    PrivateKey: "path to the pki file (private key)"

You can add default database using only the configuration file.

Default database with builtin driver in sqlalchemy

...
DATABASES: 
  default: mysql
  mysql: 
    driver: mysql+pymysql
    user: "replace this with your database user"
    password: "replace this with your database user's password"
    database: "replace this with your database name"
    address: "replace this with your hostname"
    models: "mysql (python module that require to be put under models.persistent module)"
    readonly: false
...

Default database with non builtin driver in sqlalchemy

...
DATABASES:
  informix:
    driver: informix
    user: "replace this with your database user"
    password: "replace this with your database user's password"
    database: "replace this with your database name"
    address: "replace this with your hostname"
    models: "informix (python module that require to be put under models.persistent module)"
    params:
      SERVER: "replace with your server name"
      CLIENT_LOCALE: "replace with your client locale"
      DB_LOCALE: "replace with your server locale"
    dialects:
      informix: 
        module: IfxAlchemy.IfxPy
        class: IfxDialect_IfxPy
      informix.IfxPy: 
        module: IfxAlchemy.IfxPy
        class: IfxDialect_IfxPy
      informix.pyodbc: 
        module: IfxAlchemy.pyodbc
        class: IfxDialect_pyodbc
    readonly: false
...

"params" are parameters that need to be send within the connection to the database. In that example using informix database "SERVER", "CLIENT_LOCALE" and "DB_LOCALE" are required parameters for the connection to the database.

"dialects" are the python modules configuration to translate models into sql statements to query the database

By default escape char between url and first param is ? and escape char between parameters is & but they can be changed by adding within your database params section:

...
      url_param_separator: '?' #Change it with yours
      params_separator: '&' #Change it with yours
...

Multiple databases

...
DATABASES:
  db01:
    ...
  db02:
    ...
...

Creating server routes

There are 3 files where you could register your flask server routes, You could find these file under the src/server folder:

  • Errors:

All the server http error code must be registered inside the init method of the errorhandler.py file.

Example:

server.add_exception_handler(500, controllers.web.errors.http_500)
  • Web based http file routes:

All the web based http routes must be registered inside the init method of the web.py file.

Example:

server.add_route(path='/', route=controllers.web.home.index, methods=["GET"], name='home')

Can also loads router:

#controllers.ws.api.router needs to be a fastapi APIRouter instance
server.include_router(controllers.web.router, prefix='/api/v1')
  • Rest api routes:

All the Rest API based routes must be registered inside the init method of the ws.py file.

Example:

server.add_api_route('/api/content/', controller.ws.api.index, methods=['GET'], 'api.content')

Can also loads router:

#controllers.ws.api.router needs to be a fastapi APIRouter instance
server.include_router(controllers.ws.api.v1.router, prefix='/api/v1/')

Creating controllers:

In case of database used within controllers, you will need to use @safe from fastapi_framework_mvc.database.decorators over your function. Example bellow:

from fastapi_framework_mvc.database.decorators import safe


class Content(object):

    @safe
    @staticmethod
    def index(api_param):
        return api_param


class Controller(Content):

    @classmethod
    def index(cls, api_param:str):
        return super(Controller, cls).index(api_param)
  • Web based http file controllers:

All web based http file controllers must be placed under the controllers.web module.

The class based controllers that you register into the app must be imported into the __init__.py file of the controller.web module.

The file based that contain your view functions must must also be imported into the __init__.py file of the controller.web module.

  • Rest api controllers:

All Rest API based controllers must be placed under the controllers.ws folder.

The class based controllers that you register into the app must be imported into the __init__.py file of the controller.ws module.

The file based that contain your view functions must must also be imported into the __init__.py file of the controller.ws module.

Creating models:

you can create SQLAlchemy models by creating a new module under the models.persistent module and place each models inside your module that you previously created.

The models that you register into the app must be an fastapi_framework_mvc.database.Model or fastapi_framework_mvc.database.get_models_by_name('replace that with your database connection name') object, you could import this object using the following line into your database model:

from fastapi_framework_mvc.database import Database

All models must be imported inside the __init__.py of your base module and you must import this module in the __init__.py of the models.persistent module

Static folder:

The src/static folder contains all static file for your web based application.

Template folder:

The src/template folder contains layouts and templates file for your web based application. Those files are content configurable, you can also import layout inside the your template file, it allow you to have only content editable part into your template file.


Using docker-compose file:

  • First start of the fastapi server:
docker-compose up 
  • To start the flask server:
docker-compose start 
  • To restart the flask server
docker-compose restart 
  • To shutdown the flask server:
docker-compose stop 

Running on Azure Function App:

# coding: utf-8
import azure.functions as functions
import fastapi_framework_mvc.azure 
import logging
import os


os.environ.setdefault('CONFIG_FILE', './config/config.yml')
app = functions.AsgiFunctionApp(fastapi_framework_mvc.azure.AzureFunctionsApp(), http_auth_level=functions.AuthLevel.ANONYMOUS)
  • Make sure to have in your host.json:
...
  "extensions": {
    "http": { "routePrefix": ""}
  },
...

Running on local desktop:

We assume that your system already had python v3+ and pip v3+ installed.

installation:

git clone https://github.com/frederickney/fastapi-framework-mvc.git
cd fastapi-framework-mvc
pip3 install .

or

pip install fastapi-framework-mvc

CLI interface:

  • Powershell
fastapi_framework_mvc.cli -h
  • Bash
fastapi_framework_mvc.cli -h
  • Python module
python -m fastapi_framework_mvc.cli -h

Create a new project:

  • Powershell:
fastapi_framework_mvc.cli project -c <your project>

or

fastapi_framework_mvc.cli controller -c <your project>
  • Bash:
fastapi_framework_mvc.cli project -c <your project>

or

fastapi_framework_mvc.cli project --create <your project>
  • Python module:
python -m fastapi_framework_mvc.cli project -c <your project>

or

python -m fastapi_framework_mvc.cli project --create <your project>

A project can also be packaged and later used by the framework. In order to do so, you need to create a pyproject.toml that will build your project into a python package. Best practices ar to put the pyproject.toml in the parent directory of your created project.

Then you will still have to create a server pathon module (and a models.persistent python module if using database connection(s)).

In the server part you will only have to import from your packages the modules under the package server module within the __init__.py in server. example:

# coding: utf-8
#__init__.py

from your_project.server import web, ws, errorhandler, plugins, middleware, socket

or if you want multiples application packaged, you will have to create the same python module server arborescence as the one initially on your project but instead of rewriting the routes or loading the routers, you can just for example:

# coding: utf-8
# example with two project on the ws.py file
import your_first_project.server.ws
import your_second_project.server.ws


class Route(object):
    """
    Class that will configure all ws services based routes for the server
    """
    def __init__(self, server):
        """
        Constructor
        :param server: FastAPI instance
        :type server: fastapi.FastAPI
        :return: Route object
        """
        your_first_project.server.ws.Route(server)
        your_second_project.server.ws.Route(server)
        return

In the models.persistent part you will only have to import from your packages the modules under the package models.persistent module within the __init__.py in models.persistent. example:

# coding: utf-8
#__init__.py

from your_project.models.persistent import *

or if you want multiples application packaged, and have one or many model declaration conflicts.

# coding: utf-8
# example with two project on the __init__.py file
from your_first_project.models import persistent as  your_first_project # or any other identifier
from your_second_project.models import persistent as  your_second_project # or any other identifier

Create new controllers:

When the project is created, you can create new controllers, middlewares and even link controllers to the correct file under the server module. Example:

  1. create standalone controller:

  • Powershell:
fastapi_framework_mvc.cli controller -c controllers/ws/contents
  • Bash:
fastapi_framework_mvc.cli controller -c controllers/ws/contents
  • Python module:
python -m fastapi_framework_mvc.cli controller -c controllers/ws/contents
  1. create router controller:

  • Powershell:
fastapi_framework_mvc.cli controller -c controllers/ws/contents -router
  • Bash:
fastapi_framework_mvc.cli controller -c controllers/ws/contents -router
  • Python module:
python -m fastapi_framework_mvc.cli controller -c controllers/ws/contents -router
  1. install standalone controller:

  • Powershell:
fastapi_framework_mvc.cli manager -l controllers/ws/contents
  • Bash:
fastapi_framework_mvc.cli manager -l controllers/ws/contents
  • Python module:
python -m fastapi_framework_mvc.cli manager -l controllers/ws/contents
  1. install router controller:

  • Powershell:
fastapi_framework_mvc.cli manager -l controllers/ws/contents -p /api/
  • Bash:
fastapi_framework_mvc.cli manager -l controllers/ws/contents -p /api/
  • Python module:
python -m fastapi_framework_mvc.cli manager -l controllers/ws/contents -p /api/
  1. Create middlewares:

  • Powershell:
fastapi_framework_mvc.cli middleware -c grant/authorization
  • Bash:
fastapi_framework_mvc.cli middleware -c grant/authorization
  • Python module:
python -m fastapi_framework_mvc.cli middleware -c grant/authorization

see -h for usages


Launching

  • On every startup

Linux/Mac:

export CONFIG_FILE=config/config.yml

Windows:

$env:CONFIG_FILE = "C:\Users\Z01FNEY\PycharmProjects\fastapi-framework-mvc\test\config\config.yml"
  • Starting using fastapi

In dev mode:

# needs app.py in your current working directory.
python -m fastapi dev

In standalone mode:

# needs app.py in your current working directory.
python -m fastapi run

app.py example here

  • Starting the fastapi server in standalone
python -m fastapi_framework_mvc.server
  • Starting the flask server with gunicorn and workers process
python -m fastapi_framework_mvc.wsgi

LICENSE

See License file

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_framework_mvc-1.3.1.tar.gz (114.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_framework_mvc-1.3.1-py3-none-any.whl (115.1 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_framework_mvc-1.3.1.tar.gz.

File metadata

  • Download URL: fastapi_framework_mvc-1.3.1.tar.gz
  • Upload date:
  • Size: 114.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for fastapi_framework_mvc-1.3.1.tar.gz
Algorithm Hash digest
SHA256 1f911e729b3dda06df5b9039de2cddf7cdd8d4160a52aed5d9a4cab9fd29393b
MD5 54d81bc76d617fa4177957ca641c5ecf
BLAKE2b-256 86df2d4c3d470ca96a76db48848997ad3165699b83f13e379a04f29f01d2a9dd

See more details on using hashes here.

File details

Details for the file fastapi_framework_mvc-1.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_framework_mvc-1.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ba6bfd862fbde1480a6e331774dfa72ccccf5a39b7ef4d4682985ac4ea10d4f9
MD5 1671edf46268a77c710144ed8d93e8cf
BLAKE2b-256 09dcb98900443ba5712a41e501d5d0d5c21ed411bf113326793c7b37b02a1a97

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page