/fast-api-admin-template

A test driven micro-service template to build and deploy a fast-api service with admin feature.

Primary LanguageJavaScriptMIT LicenseMIT

Fasy API Admin Template

A test driven micro-service template to build and deploy a fast API service with Docker and on API Gateway / AWS Lambda.

This template also includes auto admin capabilities for models.

Check out the live site.

API

App Views

Admin Views

Components

  • Python

  • Docker is used to run locally and in the cloud.

    • Dockerfile Dockerfile with multi-architecture support
    • docker-compose API Gateway and Lambda functions built from docker image
  • API sample routes are mapped to:

    • GET /
    • GET /resources
    • POST /resources
    • PUT /resources/{:id}
    • DELETE /resources/{:id}
    • GET /tasks
    • POST /tasks
    • PUT /tasks/{:id}
    • DELETE /tasks/{:id}
  • Admin routes are mapped to:

    • GET /admin/
    • GET /admin/{:resource}
    • GET /admin/{:resource}/{:id}
  • Serverless API Gateway and functions mapped to lambda-web on cloud (HttpServer locally).

  • Deployment Actions to deploy on branch commits

    • .github/workflows Github actions
      • lint [feature]
      • test [feature]
      • deploy [main, production]
      • tag [production]
  • Views Bootstrap 5 views based of the following templates

Requirements

Project Directory Structure

  ├── apps
        ├── api
              ├── models
              ├── routes
              ├── services
        ├── views
  ├── tests
        ├── integration
        ├── unit
  ├── templates
        ├── accounts
        ├── admin
        ├── includes
        ├── layout
        ├── layout
  ├── static
        ├── assets
  ├── .github
        ├── workflows

Creating a new service

Create a new service by selecting the button above 👇

To setup your local environment:

python -m venv venv
. ./venv/bin/activate
pip install -r requirements.txt

Development

While working on a feature to start the service locally run:

  python apps/main.py 

Or use docker

  docker-compose up 
  docker-compose up --build

Before you are ready to merge your feature, to test running lamnbda, you can run:

  serverless offline 

Adding a new route

For CRUD applications, follow the example in resource.rs and add your resource route to:

  apps/api/routes 

For example if you are building an API to manage Users and Shares, you would create:

  apps/api/routes/users.rs
  apps/api/routes/shares.rs

Adding a new model

For admin enabled views, inherit from BaseAdmin. Follow the example resource.rs add your models:

  apps/api/models/users.rs
  apps/api/models/shares.rs

Connecting to a source DB

TODO: Add DB support

Adding views

Follow the example resource/dashboard.html for a custom model view:


Test Driven Development

Before writing any functionality, we recommend you write your tests first. See the examples in:

To add tests, simply add to the appropriate package and using automod it will be automatically detected to run.

To run tests

  pytest test

To run specific tests

  pytest test --package <package-name>

** When using actix-web modules, it is not recommended (or easy) to unit test a route module. Follow the example in tests/integration/routes/resource.rs to add an route integration test.


Deploying

aws configure has been run.

To deploy locally:

npm install
serverless deploy

Continuous Integration

Github workflows are will trigger of off specific branches

  • feature.yml: Lint and test worflows will run any time there is a push to any feature branch.
  • development.yml: Lint, test and deployment worflows will run any time there is a push to main branch.
  • production.yml: Lint, test, deployment and release worflows will run any time there is a push to main branch.

This project uses Github workflows.

You will need to configure a role in AWS for the github service to use. Follow the instructions here.

Permissions

You may need to add more permissions to your role. Be as restrictive as possible when adding more permissions


Contributing to this template

If you have additions, changes, fixes, create a Pull Request and tag any reviewers and it will be reviewed promptly.

If you are having issues, create an issue and we will investigate.