eLibrary Backend

By: Reza Hadi Fairuztama

An implementation of the backend for the eLibrary project

Installation

The easiest way to deploy the API from the ground up is to use:

docker-compose up

Usage

/auth/login:

body -

{
    "email": "username@example.co.id",
    "password": "P4sswordꦏꦤ꧀"
}

response - 200 OK

{
    "token": "a.b.c",
    "scheme": "Bearer",
    "expires_at": "2010-07-28T12:54:27+09:00"
}

response - 400 Bad Request; 401 Unauthorized Request; 422 Validation Failed

{
    "error_type": "Bad Request",
    "message": "error-message"
}

/auth/google:

body -

{
    "token": "aa.bb.cc",
}

response - 200 OK

{
    "token": "a.b.c",
    "scheme": "Bearer",
    "expires_at": "2010-07-28T12:54:27+09:00"
}

response - 400 Bad Request; 401 Unauthorized Request; 422 Validation Failed

{
    "error_type": "Bad Request",
    "message": "error-message"
}

/auth/register:

body -

{
    "email": "username@example.co.id",
    "name": "Joko",
    "password": "P4sswordꦏꦤ꧀"
}

response - 200 OK

{
    "new_id": "username@example.co.id"
}

response - 400 Bad Request

{
    "error_type": "Bad Request",
    "message": "error-message"
}

/auth/register/google:

body -

{
    "token": "aa.bb.cc",
}

response - 200 OK

{
    "new_id": "username@example.co.id"
}

response - 400 Bad Request; 422 Validation Failed

{
    "error_type": "Bad Request",
    "message": "error-message"
}

/books?criteria=new:

header -

Authorization: Bearer ...

response - 200 OK

{
    "data": [
        {
            "id": "01234567-89ab-cdef-0123-456789abcdef",
            "title": "Chäos;HEĀd",
            "author": "Hayashi Naotaka",
            "cover_url": "https://s2.vndb.org/cv/59/49759.jpg",
            "readers": 0,
            "rating": 3.5,
            "is_favorite": false
        },
    ]
}

response - 422 Validation Failed

{
    "error_type": "Validation Failed",
    "message": "error-message"
}

/books?criteria=popular:

header -

Authorization: Bearer ...

response - 200 OK

{
    "data": [
        {
            "id": "01234567-89ab-cdef-0123-456789abcdef",
            "title": "Chäos;HEĀd",
            "author": "Hayashi Naotaka",
            "cover_url": "https://s2.vndb.org/cv/59/49759.jpg",
            "readers": 0,
            "rating": 3.5,
            "is_favorite": false
        },
    ]
}

response - 422 Validation Failed

{
    "error_type": "Validation Failed",
    "message": "error-message"
}

/books?criteria=newHomepage&page=0:

header -

Authorization: Bearer ...

response - 200 OK

{
    "data": [
        {
            "id": "01234567-89ab-cdef-0123-456789abcdef",
            "title": "Chäos;HEĀd",
            "author": "Hayashi Naotaka",
            "cover_url": "https://s2.vndb.org/cv/59/49759.jpg",
            "readers": 0,
            "rating": 3.5,
            "is_favorite": false
        },
    ]
}

response - 422 Validation Failed

{
    "error_type": "Validation Failed",
    "message": "error-message"
}

/books?criteria=popularHomepage&page=0:

header -

Authorization: Bearer ...

response - 200 OK

{
    "data": [
        {
            "id": "01234567-89ab-cdef-0123-456789abcdef",
            "title": "Chäos;HEĀd",
            "author": "Hayashi Naotaka",
            "cover_url": "https://s2.vndb.org/cv/59/49759.jpg",
            "readers": 0,
            "rating": 3.5,
            "is_favorite": false
        },
    ]
}

response - 422 Validation Failed

{
    "error_type": "Validation Failed",
    "message": "error-message"
}

/books?criteria=search&query=...:

header -

Authorization: Bearer ...

query -

query= text

response - 200 OK

{
    "data": [
        {
            "id": "01234567-89ab-cdef-0123-456789abcdef",
            "title": "Chäos;HEĀd",
            "author": "Hayashi Naotaka",
            "cover_url": "https://s2.vndb.org/cv/59/49759.jpg",
            "readers": 0,
            "rating": 3.5,
            "is_favorite": false
        },
    ]
}

response - 400 Bad Request; 422 Validation Failed

{
    "error_type": "Bad Request",
    "message": "error-message"
}

Modules used:

Go Reference chi, for REST handling

Go Reference chi/render, for parsing HTTP request and response bodies

Go Reference chi/jwtauth module, for authentication via JWT tokens

Go reference crypto, for password hashing via bcrypt

Go reference godotenv, for enviroment variable loading

Go reference gomail, for sending account activation email

Go reference google/apo, for validating google sign-in's token

Go reference google/uuid, for session id's uuid generator

Go Reference pq, for connecting to PostgreSQL server

Go Reference sethvargo/go-envconfig, for structured env vars

Go reference sqldb-logger, for SQL logging

Go reference zerolog, for JSON structured logger

Testing

Go reference testify, for cleaner unit testing assertions

Go reference go-sqlmock, for mocking an *sql.SQL object in unit testing