Instagram-BackendAPI
A Golang REST API to handle users and posts for a simple instagram backend. Uses MongoDB as the database. Tested using golang-testing and Postman.
External Dependencies
- mongo-driver
- godotenv (to handle secrets using environment variables, not required)
Directory Structure
├── go.mod
├── go.sum
├── helper
│ └── connect.go
├── main.go
├── main_test.go
└── models
└── models.go
go.mod, go.sum
Manages dependenciesconnect.go
Establishes a connection to the MongoDB clustersmodels.go
Defines the model objectsmain.go
Primary file to handle http requestsmain_test.go
Unit testing script
Endpoints
/api/posts
creates a post using data from the POST request's body
/api/posts/
fetches post details for the given id
/api/posts/users/
?limit={}&lastid={}
fetches posts of the user with given id within a limit and with ids greater than lastid (if lastid is not null)
/api/users
creates a user and encrypts the password using ciphers before storing
/api/users/
fetches user details of given id
Pagination
The API employs an id-based pagination for the /api/posts/users/
endpoint. On the initial call, the limit parameter must be passed along with the request. The API returns the requsted number of posts and the id of the last post. Subsequent calls from the front-end must also contain a lastid
parameter with the id that was returned on the previous call. This ensures that the second call returns posts starting with ids greater than the lastid
, and that the API response doesn't explode in size.
Testing
Usage
- clone the repo
git clone https://github.com/VoidlessVoid7/Instagram-BackendAPI .
go build main.go
go run ./
Screenshots
- Creating a post
- Fetching a post
- Create a user
- Fetch a user
- Fetch posts created by a user