- A simple webservice which can serve as an api to login/register user, add friends, create posts, & share posts
- The following project is implemented using Golang v1.9 and MongoDB.
-
Clone the project in your system
-
Set environment variable foe URL as a mongo db query string
-
Make sure you have docker installed to start the application
-
After cloning, open a terminal, go to the root folder of the project and perform the following commands.
- docker image build -t golang_api .
- docker run -p 8080:8080 golang_api
The first command will build a docker image that consists of go binary file of the project(e). The second command will run the image and it can be accessed locally in port 8080, using the same 8080 port from docker.
localhost:8080
- This api will register any new user in the server/db. It will create a
bearer
token usingjwt
which can be used for authorization purpose. Unique 10 million uid is generated which limits the number upto 10 million.
HOST_URL/register
HTTP POST
{
"username":"abhiyan",
"password":"abhiyan"
}
{
"error": "Invalid credentials"
}
Name | Description |
---|---|
code | 201 if success creating, else 401 on unauthorized |
username | type:string |
password | type:string |
- This api will login existing user in the server/db.
HOST_URL/login
HTTP POST
{
"username":"abhiyan",
"password":"abhiyan"
}
Name | Description |
---|---|
code | 200 if success |
username | type:string |
password | type:string |
- This api will register will create new post and store it in post collection of mongo DB. TimeStamp will be used to track number of post created in that particular day.
HOST_URL/:user/post
HTTP POST
{
"body":"abhiyan",
"user_id": ":user"
}
Name | Description |
---|---|
code | 201 if success creating |
body | type:string |
user_id | type:string, from:URL |
- This api will rbe used to create a link between two user objects, the link will be stored in db. If the friends array has less that 100 User document/node then new friend will be added to that list.
HOST_URL//:user/add/:friend_id
HTTP POST
Name | Description |
---|---|
:user | type:string |
:friend_id | type:string |
- This api will getch the relevantt post from post collection of mongo db. Every use has associated post in their object so that post will be shown for that specific user.
HOST_URL/:user/home
HTTP POST
{
"body":"abhiyan",
"user_id": ":user"
}
Name | Description |
---|---|
code | 200 |
message | type:string, success |
data | array of post data |
There are 3 high level packages used in this project
- Mongo Driver (" go.mongodb.org/mongo-driver")
- Gin-Gonic ("github.com/gin-gonic/gin")
- jwt-GO ("github.com/dgrijalva/jwt-go")