This repository contains the code of CRUD operations on FerretDB written in Golang. Using HTTP requests, you can create, read, update, and delete users from the FerretDB instance.
Note: This is not a production-ready application. I was just trying out FerretDB.
Development environment needs:
Make sure docker is installed and running. And the Task tool is installed.
First, Clone the repository
git clone https://github.com/thecaffeinedev/rest-api-ferretdb.git
Next, change the current directory to the repository:
cd rest-api-ferretdb
Create file .env
with content copied from .env.sample
. There won't be any need of changing the values. ****
Next, run the application:
task run
After the build is successful, You can access the application on port 8080
| Name | HTTP Method | Route |
|-------------|-------------|----------------- |
| Health | GET | /alive |
| | | |
| Get User | GET | /api/user/{email}|
| Create User | POST | /api/user |
| Update User | PUT | /api/user/{email}|
| Delete User | DELETE | /api/user/{email}|
This endpoint inserts a document in the users
collection of the users
database.Send a POST
request to /api/user
:
curl -X POST \
'http://127.0.0.1:8080/api/user' \
--header 'Accept: */*' \
--header 'Content-Type: application/json' \
--data-raw '{
"name": "Anakin",
"email": "anakin@gmail.com",
"password": "skywalker"
}'
Response:
{
"id": "ObjectID(\"6420090d2b2859af4fc17b4d\")",
"name": "Anakin",
"email": "anakin@gmail.com",
"password": "skywalker"
}
This endpoint retrieves a user given the email. Send a GET
request to /api/user/{email}
:
curl -X GET \
'http://127.0.0.1:8080/api/user/anakin@gmail.com' \
--header 'Accept: */*'
Response:
{
"id": "ObjectID(\"6420090d2b2859af4fc17b4d\")",
"name": "Anakin",
"email": "anakin@gmail.com",
"password": "skywalker"
}
This endpoint updates the provided fields within the specified document filtered by email. Send a PUT
request to /api/user/{email
}:
curl -X PUT \
'http://127.0.0.1:8080/api/user/anakin@gmail.com' \
--header 'Accept: */*' \
--header 'Content-Type: application/json' \
--data-raw '{"password": "padme"}'
Response:
{
"id": "",
"name": "",
"email": "anakin@gmail.com",
"password": "padme"
}
This endpoint deletes the user from the database given the email.Send a DELETE
request to /api/user/{email}
:
curl -X DELETE \
'http://127.0.0.1:8080/api/user/anakin@gmail.com' \
--header 'Accept: */*'
Response:
{
"message": "Successfully Deleted"
}
- Docker + Docker-Compose
- FerretDB
- Go (Of course)
- Task
I would recommend checking the FerretDB Github Repo. They are doing some awesome work.