Code Monkey home page Code Monkey logo

rock-paper-scissors's Introduction

Rock paper scissors app

FastAPI-asyncpg application to play rock paper scissors.

Description

REST API to play rock paper scissors.

Swagger docs available at /docs.

There are two resources (and two DB tables), games and turns.

A game can be created with a POST request (/games) providing player_one and player_two name or only player_one to play against computer.

A turn can be created with a POST request (/games/{game_id}/turns) providing the game id.

Then a turn can be play with a PATCH request (/games/{game_id}/turns/{turn_id}) providing game id, turn id, and one or both of the player moves (ROCK, PAPER or SCISSORS). Only when both player moves are stored the game score is updated (available at GET /games/{game_id}) and the turn result can be computed (available at GET /games/{game_id}/turns/{turn_id}/result)

Run

docker-compose up --build

This command starts a PostgreSQL DB (exposed on port 5434), run database Alembic migrations and start the API exposed on port 8000

Local Development

This project was developed with Python 3.9 and Poetry. Install Poetry (configure local virtualenv is recommended too) and then run

poetry install

to install all dependencies.

Activate poetry virtualenv, configure .env file (.example-env provided) and then run

uvicorn app.main:app

to start application.

Run tests with command:

pytest

Pre commit config is provided for git hook to keep format (black) and basic Python quality standards.

Implementation details

This API is a fully asynchronous FastAPI (ASGI) application which uses asyncpg as database driver. It also uses SQLAlchemy as ORM.

This means every HTTP request runs on an asyncio coroutine and database queries are awaited. This provides a huge performance and efficiency gain over WSGI applications (where every request is processed on a thread) since this API is I/O bounded.

In terms of Database design, something to keep in mind is since player_one and player_two are columns of game table, extending the system to support multiple players would be more difficult. It was designed this way for simplicity.

Future work

  • First more testing is required. There are currently only 2 unit tests just for the sake of setting up pytest.

  • Authentication layer. Identify and authenticate players to protect them from cheaters ;)

  • Observability could be improved adding more logging and perhaps Sentry, for error monitoring.

  • Continuous integration, a Jenkinsfile file for instance to build and test.

  • Semantic versioning.

  • There is already a Dockerfile for deployment, but a helm chart could be provided to define a Kubernetes deployment.

  • Terraform module could be added to provision infrastructure.

rock-paper-scissors's People

Contributors

nicolastomeo avatar

Stargazers

ma__kiri avatar

Watchers

 avatar

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.