AppointyTaskGo

View project on GitHub

Mock Instagram Backend

This is an API created for a mock Instagram-like application for the internship recruitment task of Appointy in September, 2021 for Vellore Institute of Technology. This API has been used using Golang and using net/http (along with other standard libraries) and no other external libraries or frameworks have been used as per the directions.

Technologies Used

This backend is written in Golang and uses standard libraries for JSON encoding/decoding and net/http for routing and serving. The API is designed to communicate in JSON with its client. MongoDB is used as the database as specified in the requirements of the task.

Building the source

Ensure that Golang is installed and set up in your system. First, clone this repository into a folder. Change directory to that folder.

git clone <this repository's URL.git>
cd AppointyTaskGo

Next, build the project. The API uses the Go driver for MongoDB. Building the project should automatically fetch the dependencies. From within this directory (project root), run:

go build

Running the API

To run the API, first set the environment variables MONGODB_URI, MONGODB_DBNAME and APPYINSTA_PORT.

Linux

export MONGODB_URI=<your connection string>
export MONGODB_DBNAME=<your database name>
export APPYINST_PORT=<on which port to run the server>

Windows (Powershell)

$Env:MONGODB_URI = "<...>"
$Env:MONGODB_DBNAME = "<...>"
$Env:APPYINSTA_PORT = "<port>"

You can also set these environment variables using other methods.

After this, run the executable created after building it.

Linux

./appyinsta

You may have to change permissions for executing this.

Windows (Powershell)

./appyinsta

The server will now run on the specified port (as specified in the APPYINSTA_PORT environment variable).

API Specification

A simple overview of the API is as follows. The API has been designed and created as per the requirements specified in the task.

Route Method Description Request Body (Sample) Response Body
/users POST Create a user
json
{
  "name": "(name)",
  "email": "(email)",
  "password": "(password)"
}
      
The password is hashed again at the server. All fields are compulsory.
json
{
  "id": "(user ID)"
}
    
The user ID (MongoDB object ID) of the new user is returned after user creation.
/users/<userID> GET Retrieve information about a user N/A
json
{
  "id": "(user ID)",
  "name": "(name)",
  "email": "(email)",
}
      
The id field has the same user ID as specified in the URL.
/posts POST Create a post
json
{
    "posted_by": "(user ID )",
    "caption": "(caption)",
    "img_url": "(image URL)"
}
    
The posted_by field contains the user ID of the user who created this post. While post creation, the timestamp of its creation is recorded at the server.
json
{
  "id": "(post ID)"
}
    
The post ID (MongoDB object ID) of the new post is returned after post creation.
/posts/<postID> GET Retrieve information about a post N/A
json
{
  "id": "(post ID)",
  "posted_by": "(user ID)",
  "caption": "(caption)",
  "img_url": "(image URL)",
  "posted_on": "(timestamp)"
}
    
The id field has the same post ID as specified in the URL.
/posts/users/<userID> GET Retrieve the posts created by the user, latest first. First Request

For the first request, the first_request must be set to true.
The last_id field can be set as the userID (or any post ID).
The last_posted_on should be any string of a valid timestamp format (for example: 2021-10-09T12:17:11.478Z).
The request body format for the first request is as follows:
json
{
  "last_id": "(user ID)",
  "last_posted_on": "(timestamp)",
  "n_new": 3,
  "first_request": true
}
    
The n_new field sets how many posts should be retrieved.

Subsequent Requests

For the subsequent requests, the request format is similar.
The last_id field should have the post ID of the last post
of the previous response.
The last_posted_on field should have the posted_on timestamp of
the last post of the previous response.
The first_request field must be set to false, or can be omitted.
json
[
    {
        "id": "(post ID)",
        "posted_by": "(user ID)",
        "caption": "(caption)",
        "img_url": "(image URL)",
        "posted_on": "(timestamp)"
    },
    ...
    {
        "id": "(post ID)",
        "posted_by": "(user ID)",
        "caption": "(caption)",
        "img_url": "(image URL)",
        "posted_on": "(timestamp)"
    }
]
    
This array will contain maximum n_new number of posts (as specified in the request body). The posts are returned in the most recent first order.

Running Unit Tests

Ensure that the required environment variables are set (see the “Running the API section”), and go to the project root directory and run:

go test appyinsta/api/handlers

(As of now, the tests depend on existing data in the database, and will fail if that data is not present. A mechanism to add test data will be added.)

Licence

Will be added soon.

2021, Souris Ash