Giter Club home page Giter Club logo

training's Introduction

A Simple User RESTful API

A simple User RESTful API developed using Flask(python) and PostgresSQL, using a clean architecture.

Setup development environment

Requirements

Python3.10

Poetry1.2.1

PostgresSQL >= 14

Setup

Use the script setup_dev_env.sh to setup a virtual environment and activate it on your working terminal, then it will install dependencies using poetry and setup the pre-commit pipeline that will check code integrity and format using pylint, isort and black.

To use the script run

    source setup_dev_env.sh

copy the .env.dist to .env

    cp .env.dist .env

and add the postgressql uri

Run Locally

Using flask server directly from the terminal

Once dependencies installed and PostgresSQL database running you can run the app using in normal mode

    flask run

or in debug mode

    flask --debug run

When using this method the app will be accessible using http://localhost:5000

Using containers

If you do not want to bother with dependencies and database issues you can build and run the app directly using docker

    docker compose up --build

When using this method the app will be accessible using http://localhost:8080 (nginx http server)

PS: If you are in production environment use the docker compose methode after setting the variable inside .env to "production" so that it uses a dedicated WSGI server(gunicorn) best suited for production deployement.

TODO

Tests !!!!!!!!!!

Even though I have designed this API to be easy to test, I think some modification maybe needed to implement unit tests.

Add roles for user. Add creation and update date in model to keep history of changes. Add a JWT token creation and verification to restrict access to the api.

Notes

You can run the postgres container independently if you don't wont to install it locally

If you run the app using docker you need to use the name of the postgres container as the host

You may encounter sometime problems when running postgres due to some malfunctioning that keeps the port 5432 in use you can run these commands to tackle this issues.

    sudo lsof -i:5432 # copy the process id
    sudo kill -9 process_id

This may be helpful if you installed postgres locally

After installing:

Initialize a database storage area on disk :

    sudo mkdir /usr/local/pgsql/data
    sudo chown -v postgres /usr/local/pgsql

add these lines as sudoer to /var/lib/postgresql/.bash_profile to be able to use postgres command line in any user terminal:

    PATH=/usr/local/pgsql/bin:$PATH
    export PATH
    PATH=/usr/lib/postgresql/{version}/bin:$PATH
    export PATH

then run

sudo su - postgres
initdb  -D /usr/local/pgsql/data

Running Server:

from your user

    sudo su - postgres -c 'pg_ctl start -D /usr/local/pgsql/data -l logfile'

from postgres user:

    pg_ctl start -D /usr/local/pgsql/data -l logfile

Shutting down the server :

from your user

    sudo su - postgres -c 'pg_ctl stop -D /usr/local/pgsql/data'
    #or
    sudo kill -INT `sudo head -1 /usr/local/pgsql/data/postmaster.pid`

from postgres user:

    pg_ctl -D stop
    #or
    kill -INT `head -1 /usr/local/pgsql/data/postmaster.pid`

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.