A Go based REST API built using native libraries. The API has been thoroughly worked through with Postman.
- User Route: These route includes the functionality of
fetchUser
andaddUser
. - Auth Route: Authentication for
user
is done usingjwt
. - FileHandler Route: Worked with
GridFS
to store files asfs.chunks
. Added functionality touploadFile
anddownloadFile
. - Post Route: Functionality to create a post having functionality to
uploadFile, desctiption, title
.
MongoDB
is serving as the required database for all the storage functionality of this api.
To access the MongoDB locally we used a docker container to run an instance of it binding the same to localhost port 27017 using -p 27017:27017
.
docker run --name MongoREST -p 27017:27017 -d mongo:latest
Clone the project and navigate to the cloned repo.
git clone https://github.com/bhavyagoel/GoRestAPI.git
cd GoRestAPI
Install go dependencies by initialising a local env
go mod init
go mod tidy
Run the API Server
go run main.go
GET /user/getUserById
Parameter | Type | Description |
---|---|---|
id |
string |
Required UserID to fetch details |
Dummy Response
{
"Email": "[email protected]",
"Password": "$2a$14$vJSwx9sOr2e9KMLiVji9tOfB5AbMjro69R/D1wN5Yqa/IpFMCr2Tq",
"_id": "61617e08b11882299ca8a3fa",
"id": "393d0be9-1091-4bd0-822b-915980d94b6d",
"name": "Jane Dow",
"post": [
"56f18233-07bc-4472-9f71-4192f3cc2a3f.jpg",
"927d4eef-7a77-4865-9cb3-0907fa44a871.jpg"
]
}
POST /user/addUser
Parameter | Type | Description |
---|---|---|
Name |
string |
Your Name as query Parameter |
Email |
string |
Your Email as query Parameter |
Password |
string |
Your Password as query Parameter. Hashed of the same will be saved in db |
Note: The string password from the POST
request will be automatically hashed using golang.org/x/crypto/bcrypt
.
Dummy Response
{
"message": "User added successfully",
"result": {
"InsertedID": "6161e51740c7852d0e45a540"
}
}
To add A post Under a user with a given Id following are the query parma
Parameter | Type | Description |
---|---|---|
Id |
string |
UUID Generated By github.com/google/uuid package |
Title |
string |
Title to Your Post passed as a String |
Description |
string |
A paragraph long Description of the post |
urlToImage |
string |
URI to the local image location |
publishedAt |
string |
TimeStamp Generated using time.now() |
fileName |
string |
Unique fileName Generated github.com/google/uuid |
userId |
string |
userId that links the post and the user |
{
"fileName": "3b59e4c8-9c75-4e4d-a716-4d18f1e5d22e.jpg",
"fileSize": 171959,
"id": "6161eb3a2318903450b28e0e",
"message": "Successfully uploaded",
"post": {},
"user": {
"Email": "\"[email protected]\"",
"Password": "$2a$14$r/yybaVD9/JgjSY8sP5.XezjyBLzHrARar175YBuneKAYvn5r26hO",
"_id": "6161eb1c2318903450b28e0a",
"id": "d3e1fd98-0348-429f-9963-de9433960276",
"name": "\"Jane Doe\"\n"
}
}
To run tests, run the following command The unit tests can be found nested inside the test directory
cd test/
go test -v
- Containerising the API using docker
- Deploying the API to Postman
Screenshots for the same can be found under the img
sub-folder.