https://github.com/souris-dev/appointytaskgo
Task for internship recruitment at Appointy for Vellore Institute of Technology, 2021.
https://github.com/souris-dev/appointytaskgo
api backend golang mongodb
Last synced: 2 months ago
JSON representation
Task for internship recruitment at Appointy for Vellore Institute of Technology, 2021.
- Host: GitHub
- URL: https://github.com/souris-dev/appointytaskgo
- Owner: souris-dev
- Created: 2021-10-08T19:20:42.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2021-10-10T02:46:09.000Z (over 4 years ago)
- Last Synced: 2025-01-23T05:14:22.616Z (over 1 year ago)
- Topics: api, backend, golang, mongodb
- Language: Go
- Homepage: https://souris-dev.github.io/AppointyTaskGo/
- Size: 36.1 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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.
```sh
git clone
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:
```sh
go build
```
## Running the API
To run the API, first set the environment variables `MONGODB_URI`, `MONGODB_DBNAME` and `APPYINSTA_PORT`.
### Linux
```sh
export MONGODB_URI=
export MONGODB_DBNAME=
export APPYINST_PORT=
```
### Windows (Powershell)
```ps
$Env:MONGODB_URI = "<...>"
$Env:MONGODB_DBNAME = "<...>"
$Env:APPYINSTA_PORT = ""
```
You can also set these environment variables using other methods.
After this, run the executable created after building it.
### Linux
```sh
./appyinsta
```
You may have to change permissions for executing this.
### Windows (Powershell)
```ps
./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:
```sh
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