noud / bumbal

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

API Server

I was asked to develop a API server with these Specifications.

To get me a Laravel Docker project environment i executed

curl -s "https://laravel.build/bumbal?with=mariadb,redis" | bash

Git clone

After you have git cloned this repository you have to set the .env file and install the php packages. For this you have to have php >= 8.2.

cd bumbal && cp .env.example .env
cd bumbal && composer install

Start containers

Be sure to have no local Apache running:

sudo /etc/init.d/apache2 stop
sudo systemctl stop apache2.service

To start the dockered application type

cd bumbal && ./vendor/bin/sail up

If you are on Windows WSL2 Ubuntu follow the steps in Unsupported operating system with Docker on windows 10 with wsl2 or Unsupported operating system Laravel 8 with Sail on Windows 10 (WSL2).

If it's the first time you did build the containers you have to add the JWT secret to your .env file and run the database migrations.

cd bumbal && ./vendor/bin/sail artisan jwt:secret
cd bumbal && ./vendor/bin/sail artisan migrate

Finally, you can access the application in your web browser at: http://localhost.

I get an error, see this browser screenshot: Error

after a while i did open another browser tab to http://localhost.

I get Laravel up and running in a docker container with a mariadb container and redis container .

Stop containers

cd bumbal && ./vendor/bin/sail stop

to stop the docker container services.

Database

There are 2 database migrations for the Employees and the Devices tables.

Database migrations can ben run by:

cd bumbal && .vendor/bin/sail artisan migrate

Models

There is the given User model and 2 models for our Employee and Device.

JWT authentication

For JWT authentication i add package php-open-source-saver/jwt-auth.

cd bumbal && .vendor/bin/sail composer require php-open-source-saver/jwt-auth

I added AuthController.php and made a middelware class JwtMiddleware.php.

API

There are controllers for the API endpoints.

-DeviceController.php

-EmployeeController.php

so i have the following API endpoints:

$ ./vendor/bin/sail  artisan route:list --path=api

  POST      api/v1/device generated::rfbPTr21n5k9VdbM › api\DeviceController@…
  GET|HEAD  api/v1/device/{id} generated::xrgLU0DiQJGA912j › api\DeviceContro…
  PUT       api/v1/device/{id} generated::3jyx9E0WN0Ft5TRO › api\DeviceContro…
  DELETE    api/v1/device/{id} generated::Y66VGT20it2lEz0p › api\DeviceContro…
  GET|HEAD  api/v1/devices generated::2SKAf0q5pFkzDl7Z › api\DeviceController…
  POST      api/v1/employee generated::Oyto2t82NRC4Hkn5 › api\EmployeeControl…
  GET|HEAD  api/v1/employee/{id} generated::kg7mdAHwcOC2RAtI › api\EmployeeCo…
  PUT       api/v1/employee/{id} generated::VeGPSn1YQC3vXzei › api\EmployeeCo…
  DELETE    api/v1/employee/{id} generated::DftlC7SYvoDkyU3g › api\EmployeeCo…
  GET|HEAD  api/v1/employees generated::Mjh2kBh4LxgQgr7a › api\EmployeeContro…
  POST      api/v1/login .................... login › api\AuthController@login
  POST      api/v1/logout generated::saN84nweRG7Jj3tN › api\AuthController@lo…
  POST      api/v1/refresh generated::ZICMVOtuPDzeAIQs › api\AuthController@r…
  POST      api/v1/register generated::2eiXG4nVh19v18qL › api\AuthController@…

                                                           Showing [14] routes

Routes

The API routes are defined in api.php.

PHPUnit tests

The project has PHPUnit test code, this can be ran like

cd bumbal && ./vendor/bin/sail test

For the tests i have 3 classes:

-AuthControllerTest.php

-EmployeeControllerTest.php

-DeviceControllerTest.php

reference

I did read the following articles as refference:

-REST API with Laravel 8 using JWT Token

-Implementing JWT authentication in Laravel 10

About


Languages

Language:PHP 73.4%Language:Blade 26.4%Language:JavaScript 0.2%