/web-api

Simple API where an user can get a quick overview over all contacts resources like person, skills...

Primary LanguageGo

Web-Api

This is a simple API where an user can get a quick overview over all contacts resources like person, skills...

The following use cases are implemented :

  • UC1 : CRUD endpoint for managing contacts. A contact is composed of the following attributes :

    • Firstname
    • Lastname
    • Fullname
    • Address
    • Email
    • Mobile phone number
  • UC2 : CRUD endpoint for managing skills. A skill is composed of the following attributes :

    • Name
    • Level (expertise)
  • UC3 : Documentation using Swagger. More on that in Swagger section.

  • UC4 : Authentification/Authorization, where users can only change their contacts and skills.

Makefile

We use a makefile to easily setup the local environment. The following commands are available :

* make postgres : run a postgres12 instance
* make createdb : create a database named web-api
* make dropdb : remove the db in the docker instance
* make migrateforce : force database migration from sql files (used when migration failed using migrate CLI)
* make migrateup : migrate database schema to the docker instance
* make migratedown : remove database from the docker instance
* make sqlc : generate type-safe interfaces to query database
* make tests : run all the tests in the module
* make server : run the server 
* make mock : generate mocks for testing the API
* make swagger : create the swagger documentation for the API
* make docker-up : run docker compose up 
* make docker-down : run docker compose down

Setup local environment

Install tools

Setup infrastructure

  • Start a postgres container:

    make postgres
  • Create the web-api database:

    make createdb
  • Run database migration:

    make migrateup

Running the application

  • Run server:

    make server

Running the tests

  • Generate DB mock with gomock:

    make mock
  • Run test:

    make tests

Docker

If you don't want to setup manually everything you can just use the docker-compose provided.

Before that, change this line in app.env file:

DB_SOURCE=postgresql://root:secret@localhost:5432/web-api?sslmode=disable

by this one :

DB_SOURCE=postgresql://root:secret@postgres:5432/web-api?sslmode=disable
  • Run the application in a container:

    make docker-up
  • Remove the application :

    make docker-down

Swagger

If not already done run make swagger to generate the swagger documentation.

You can follow and use the application via the swagger. When the app is running go to this link and you will access the swagger documentation of the API. All the routes except for creating a new user are restricted. When you created a new user and logged in via the specific route, remember to add the token in the Authorize so that all the requests used the token to be identified.

  • Swagger documentation

Swagger

  • Create an user

Swagger

  • Login to retrieve a token

Swagger

Swagger

  • Use that token for the following requests

Swagger

Swagger