A simple implementation of Lumen as a 'products' API.
Build a RESTful web application with the following API endpoints. Data should persist to a sql database.
POST /products
[
{
"sku":"abc",
"attributes": {
"size": "small",
"grams":"100",
"foo":"bar"
}
}
]
"attributes" can contain any key value. "sku" is unique.
GET /products
Returns a list of products
[
{
"sku":"abc",
"attributes": {
"size": "small",
"grams":"100",
"foo":"bar"
}
},
...
]
- API structure implemented as outlined above.
- Data persists to a MySQL db while the db image is running.
- Database is seeded with the product/attributes per the example.
- No external libraries included outside of the base lumen setup.
While there's nothing too special, there are some items worth noting:
- I find the MVC approach to work exceptionally well in these data access layer type applications, as maintenance and extension is made easy with how each component is split into their own domains (routing, db access, formatting).
- Lumen was also a conscious choice over the typical Laravel setup as it is more lightweight and has faster response times.
- Database drivers are plug and play, with lumen supporting four out of the box (MySQL, PostgreSQL, SQLite, and SQL Server).
- As this is containerized we can build and deploy with ease on any host/cloud with a container runtime.
Build the app
image.
$ docker-compose build app
Start up the app
image.
$ docker-compose up app
...
ERROR 2002 (HY000): Can't connect to server on 'db' (111)
ERROR 2002 (HY000): Can't connect to server on 'db' (111)
INFO Dropped all tables successfully.
Dropping all tables ................................................................................................................... 370ms DONE
INFO Preparing database.
Creating migration table .............................................................................................................. 268ms DONE
INFO Running migrations.
2023_01_19_141213_create_attributes_table .............................................................................................. 89ms DONE
2023_01_19_141213_create_product_table ................................................................................................ 209ms DONE
INFO Seeding database.
Database\Seeders\ProductSeeder ........................................................................................................... RUNNING
Database\Seeders\ProductSeeder .................................................................................................... 197.05 ms DONE
Database\Seeders\AttributesSeeder ........................................................................................................ RUNNING
Database\Seeders\AttributesSeeder .................................................................................................. 30.60 ms DONE
[Tue Jan 24 15:44:31 2023] PHP 8.1.14 Development Server (http://0.0.0.0:8000) started
You should now be able to view the initially seeded product at http://localhost:8000/products
.
To cleanly shutdown, run docker-compose down
regardless of whether it was shut down with ^C
.
Similar to how you run the service, build the test
image.
$ docker-compose build test
Run the test
image.
$ docker-compose run test
Laravel Lumen is a stunningly fast PHP micro-framework for building web applications with expressive, elegant syntax. We believe development must be an enjoyable, creative experience to be truly fulfilling. Lumen attempts to take the pain out of development by easing common tasks used in the majority of web projects, such as routing, database abstraction, queueing, and caching.
Documentation for the framework can be found on the Lumen website.
Thank you for considering contributing to Lumen! The contribution guide can be found in the Laravel documentation.
If you discover a security vulnerability within Lumen, please send an e-mail to Taylor Otwell at [email protected]. All security vulnerabilities will be promptly addressed.
The Lumen framework is open-sourced software licensed under the MIT license.