A basic API for a blog page.
- MongoDB Driver
- Viper for .env files.
- Chi-router
- Validation
All variables in the .env
file are optional.
variable | description | default | default-.env |
---|---|---|---|
PORT | the port number this server will listen on | 4000 |
4000 |
ENVIRONMENT | has no impact whatsoever yet but can be seen in the server status | dev |
dev |
DSN | mongodb connection string | mongodb://localhost:27017 |
mongodb://db:27017 |
JWT_SECRET | secret phrase for encrypting the passwords | wonderfulsecretphrase |
wonderfulsecretphrase |
CORS_ALLOWED_ORIGINS | allowed domains for CORS requests separated by spaces | http://* https://* |
http://* https://* |
COOKIE_NAME | cookie name which gets set in the browser | uwu-blog-cookie |
uwu-blog-cookie |
COOKIE_SAME_SITE | sets same site attribute of the cookie | lax |
none |
For testing you can use the provided docker-compose. This will spin up the API along side a mongodb and uses the .env file as defaults. The api will then run on port 4000. The db will be run on port 27017.
For this you just need to run the Dockerfile and adjust the .env
-file in the root directory.
Run:
docker run schattenbrot/mini-blog-api
If you want to change the defaults of the .env file:
docker run -v /absolute/path/to/.env:./run/.env
apiUrl
is the url to the api.
Get request on:
apiURL/
The app status gets returned as a json object:
{
"status": "Available",
"up_since": "2022-02-17T18:35:17.894497612Z",
"current_uptime": 5916280315,
"environment": "development",
"version": "1.0.0"
}
Base URL:
apiURL/v1/posts[/option]
REQUEST | option | middlewares | description |
---|---|---|---|
GET |
/ |
- | Gets a list of all posts in a jsonarray |
GET |
/paging?limit=%X&page=%Y |
- | Gets a list of all posts by paging. |
GET |
/{id} |
- | Gets a single post by its ID. |
POST |
/ |
Auth | Adds a new POST |
PATCH |
/{id} |
Auth & IsPostCreatorOrAdmin | Patches a single post by its ID. |
DELETE |
/{id} |
Auth & IsPostCreatorOrAdmin | Deletes a single post by its ID. |
- Doesn't take arguments
Example Response:
Status: 200 OK
Header: Content-Type application/json
[
{
"id": "62019c31ef131e8cd42847ab",
"title": "title",
"text": "this is the text",
"created_at": "2022-02-07T22:24:49.869Z",
"updated_at": "2022-02-07T22:24:49.869Z"
}
]
If no document is found it will return status 200 and an empty array.
%X
needs to be an integer%Y
needs to be an integer
Example Response:
Status: 200 OK
Header: Content-Type application/json
[
{
"id": "62019c31ef131e8cd42847ab",
"title": "title",
"text": "this is the text",
"created_at": "2022-02-07T22:24:49.869Z",
"updated_at": "2022-02-07T22:24:49.869Z"
}
]
If no document is found it will return status 200 and an empty array.
- Doesn't take any arguments
Example Response:
Status: 200 OK
Header: Content-Type application/json
{
"id": "62019c31ef131e8cd42847ab",
"title": "title",
"text": "this is the text",
"created_at": "2022-02-07T22:24:49.869Z",
"updated_at": "2022-02-07T22:24:49.869Z"
}
If no document is found it will return Status 404 Not Found.
Example Request Body:
{
"name": "Username",
"email": "email@email.com",
"password": "12345aA;"
}
Example Response:
Status: 201 Created
Header: Content-Type application/json
{
"ok": true
}
Base URL:
apiURL/v1/users[/option]
REQUEST | option | middlewares | description |
---|---|---|---|
GET |
/logout |
Auth | Logs a user out |
GET |
/{id} |
Auth | Gets a user by its ID. |
POST |
/ |
- | Adds a new user |
POST |
/login |
- | Logs a user in |
PATCH |
/{id} |
Auth & IsUserOrAdmin | Patches a user by its ID. |
DELETE |
/{id} |
Auth & IsUserOrAdmin | Deletes a user by its ID. |
Allows authenticated people with a valid jwt token to access the specificied path.
Allows only the creator of the post or admin to modify and delete the post.
Allows only the user himself or admin to modify and delete the user.
Even though this project is made for private learning purposes I would never decline recommendations for improvements.
- pagination (might want to revisit later for a "better" response)
- graphql?! why is this a thing?! D:
- still cba'd
- cba completing the readme for now D:
This project is licensed under the MIT License.