GithubHelp home page GithubHelp logo

bcorfman / highscore-micro-two Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 2.0 176 KB

A FastAPI containerized microservice to create/query a high score list using SQLModel. Connects to a separate PostgreSQL database deployed on the ElephantSQL cloud.

Home Page: https://highscore1-1y492j9s.b4a.run/docs

License: MIT License

Shell 1.52% Python 85.99% Dockerfile 5.63% Makefile 6.86%
asgi elephantsql fastapi microservice postgresql sqlalchemy asyncio sqlmodel back4app docker

highscore-micro-two's Introduction

highscore-micro-two

Open in GitHub Codespaces

A FastAPI containerized microservice to create/query a high score list using SQLModel. Connects to a separate PostgreSQL database deployed on the ElephantSQL cloud, so I could try Python's asyncio functionality with latency.

I'm using this project to practice simple microservice design with Python.

How to set up your own version of this project

  1. Fork this highscore-micro-two repo on your own GitHub account.
    • In other words, at the top right of this page, click the Fork button!
    • Check the box that says "Copy the main branch only" (my suggestion), leave the rest of the details the same, and click Create Fork.
  2. Create a PostgreSQL DB instance on the ElephantSQL cloud.
    • Visit the ElephantSQL website and choose the free Tiny Turtle plan.
    • Sign up for your account.
    • Select a name for your plan. (The name is not important, but I chose "highscore".)
    • Pick a region and data center that's close(st) to you. This will help your cloud service performance.
    • Review the final details, then click Create Instance.
    • On the Instances page, click the name of your newly created Instance.
    • On the resulting Details page, copy the connection URL to the clipboard.
    • Finally, make sure your system tests work correctly! In your forked repo on GitHub: find and click your Settings tab, then, on the left sidebar, expand the Secrets and Variables menu, and click Actions. Click the "New repository secret" button, type in ELEPHANTSQL_URL as the name, paste your connection URL into the "Secret" text box, and change the first part of the connection URL to read postgresql+asyncpg:// instead of postgres://
  3. Create a Back4App account and use their "Container as a Service" feature to deploy a database API microservice.
    • Visit the Back4App website and sign up for a free account.
    • At their opening menu, choose Back4App's "Container as a Service" feature.
    • Point to your forked highscore-micro-two repo on GitHub via their "Import GitHub repo" button. NOTE: You will need to give permission to GitHub to install and use "Back4App Containers" during this process.
    • Under the "Configure your initial deployment" settings, choose an App Name (I named mine "highscore") and, under the "Build and Deploy" menu, assign your Port as 443. NOTE: Back4App will automatically handle the configuration of certificates needed for Secure HTTP to work with your container.)
    • Under the "Environment Variables" menu, define a variable with a name of ELEPHANTSQL_URL, paste your connection URL into the Value text box (same as in step 1 above), and change the first part of the connection URL to read postgresql+asyncpg:// instead of postgres:// before you push the Save Settings button.
    • Check your email for a confirmation link to enable deployment.

Project startup

  • Clone the project to your system
  • At a command prompt in the project directory, type make devinstall to set up the project dependencies.

Also important:

  • To run Pytest locally, the FastAPI setup code in main.py looks for a .env file in your project directory. This file will need to contain a single line, which is the same ELEPHANTSQL_URL environment variable as defined in the final part of step 1 under "How to set up your own version" above, e.g. ELEPHANTSQL_URL=postgresql+asyncpg://{username}:{password}@{your_prefix_here}.db.elephantsql.com/{username}

Deployment

  • If you follow the How to set up your own version instructions above, Back4App will deploy your containerized microservice to its cloud each time you do a git push to your main branch.
  • Any app built using FastAPI also has a default /docs page that allows you to interact with your published web API. For example, my original HighScore microservice has its own docs page here. You will need to find the link to your own container on your Back4App dashboard; make sure you add the /docs suffix onto the end of your URL to bring up the page correctly.
  • If you set both environment variables correctly in the How to set up your own version instructions above, your microservice will connect to the ElephantSQL cloud and read/write from the PostgreSQL database there. If something goes wrong, make sure you've checked the connection URLs for correctness.
  • Check out (or fork) my Anvil-based app for a web frontend that provides a better user experience for the database microservice.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.