A dead simple blog API created with Lumen.
Lumen is a micro-framework by Laravel optimized for APIs as it is lighter and faster than Laravel.More about Lumen at https://lumen.laravel.com/.
- Clone the repo on your local machine
git clone https://github.com/fgiannar/h1blog.git
- Move to project's directory
cd h1blog
- Copy .env_sample to .env and update all MAIL_* envs to match your smtp credentials and ADMIN_MAIL to your email.
cp .env_sample .env
- Build image and start containers:
docker-compose build
docker-compose up -d
- Install project dependencies:
docker-compose exec app composer install
- Create database tables and populate them with dummy data:
docker-compose exec app php artisan migrate --seed
API's endpoints can be now accessed at baseurl: http://0.0.0.0:8009/api/v1
Optional parameters:
author_id: int. Fetches the blog posts of a given user, eg GET api/v1/posts?author_id=1
order: enum, 'asc' or 'desc'. Fetches the blog posts sorted by publication date asc or desc, eg GET api/v1/posts?order=desc
published: boolean. Fetches only the posts that have a publication date (aka are published), eg GET api/v1/posts?published=true
tag: String. Fetches the posts that have a given tag/tags, GET api/v1/posts?tag=sports or GET api/v1/posts?tag=sports,social
title: String, required.
body: String, required (accepts html tags)
tags: Array, required
author_id: Integer, required, a valid user id
published_at: Optional, date, format: 'Y-m-d H:i:s', should be before or equal to now.
Sample POST request body:
{
"title": "Lorem Ipsum",
"body": "Magni fuga veniam explicabo ex rerum dolorum voluptatibus. Officiis quia labore atque natus at. Et sed maiores consequuntur perferendis maxime mollitia odit.",
"tags": [
"sport",
"social"
],
"author_id": 1,
"published_at": "2012-03-12 07:14:26"
}
Sample response:
{
"title": "Lorem Ipsum",
"body": "Magni fuga veniam explicabo ex rerum dolorum voluptatibus. Officiis quia labore atque natus at. Et sed maiores consequuntur perferendis maxime mollitia odit.",
"tags": [
"sport",
"social"
],
"published_at": "2012-03-12 07:14:26",
"author_id": 1,
"updated_at": "2018-01-16 13:37:09",
"created_at": "2018-01-16 13:37:09",
"id": 71
}
title: String, required.
body: String, required (accepts html tags)
tags: Array, required
author_id: Integer, required, a valid user id
published_at: Optional, date, format: 'Y-m-d H:i:s', should be before or equal to now.
Sample PUT request body:
{
"title": "Lorem Ipsum",
"body": "Magni fuga veniam explicabo ex rerum dolorum voluptatibus. Officiis quia labore atque natus at. Et sed maiores consequuntur perferendis maxime mollitia odit.",
"tags": [
"sport",
"social"
],
"author_id": 1,
"published_at": "2012-03-12 07:14:26"
}
- POST requests may be a bit slow, as an event is fired in order to send an info email to admin. The event uses queues so in production envs this would be no issue, since the QUEUE_DRIVER would be anything but sync.
- All string inputs are sanitized, especially the body property (which allows html tags) is sanitized using HTML_Purifier.
- In compliance with KISS, tags are not kept in a separate table using many-to-many relationship but instead saved as comma-separated strings with a start and trailing comma (kind of hack to facilate DB queries using LIKE). Example: ['sports', 'social'] would be stored as: ',sports,social,'. This is accomplished using Laravel's accessors and mutators.
Run unit tests with:
docker-compose run app vendor/bin/phpunit