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.
- mongo-driver
- godotenv (to handle secrets using environment variables, not required)
├── 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
creates a post using data from the POST request's body
fetches post details for the given id
fetches posts of the user with given id within a limit and with ids greater than lastid (if lastid is not null)
creates a user and encrypts the password using ciphers before storing
fetches user details of given id
The API employs an id-based pagination for the /api/posts/users/<id>
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.
- clone the repo
git clone https://github.com/VoidlessVoid7/Instagram-BackendAPI .
go build main.go
go run ./
- Creating a post
- Fetching a post
- Create a user
- Fetch a user
- Fetch posts created by a user