Code Monkey home page Code Monkey logo

laravel-exa's Introduction

ExA

Opinionated Modularized API Skeleton for Laravel

Packagist PHP from Packagist Laravel Version

Features | Usage | Configuration | Structure | ExA Classes | Credits | Contributing

Features

  • Your API running on the latest version of Laravel and PHP
  • Docker config with PHP, Nginx, MySQL, Redis and Mailpit
  • API Documentation with Swagger
  • Laravel Pint configuration (very opinionated)
  • Pest v2 for Tests
  • Type Coverage Tests with 100% type coverage
  • Base classes to speed up the development
  • DTOs with Laravel Validated DTO
  • Slack Client for notifications
  • API structured in modules
  • JWT for Authentication
  • Users management out-of-the-box with simple roles system
  • Logs on DB for user logins and for actions made on models
  • Strictus for enforcing local variable types
  • Models extending from BaseModel use soft deletes by default
  • Log actions made by users with the created_by, updated_by and deleted_by fields. Use the $table->userActions() in your migrations to add these fields.

Using the Template

There are three ways of using this template:

Composer (Recommended)

composer create-project --prefer-dist wendelladriel/laravel-exa my-app

GitHub Template

Click the Use this template button in the GitHub repository page.

Git Clone

git clone [email protected]:WendellAdriel/laravel-exa.git my-app && cd my-app && rm -rf .git

Configuring the Application

Build the docker services with

make build

Run this command for the initial app configuration

make configure

Database Config

Start the DB container with

make db-start

Run the migrations

make art ARGS="migrate"

Update the admin user in the database/seeders/DatabaseSeeder.php file and run the seeds

make art ARGS="db:seed"

Stop the DB container with

make db-stop

M1/2 Processor Config

If you're using a Mac with M1/2 processor, you need to update the M1_PROCESSOR env variable to true

XDebug Config

By default, XDebug will be installed, if you want to disable it, update the XDEBUG_ENABLED env variable to false

You can also configure XDebug by updating the docker/app/config/xdebug.ini file

Updating Services Ports

You can update which ports the services will connect to your machine by updating these variables in the .env file

  • APP_EXTERNAL_PORT
  • APP_EXTERNAL_PORT_SSL
  • SWAGGER_EXTERNAL_PORT
  • DB_EXTERNAL_PORT
  • REDIS_EXTERNAL_PORT
  • MAILPIT_EXTERNAL_PORT_SMTP
  • MAILPIT_EXTERNAL_PORT_HTTP

Running the Application

Run this command to start the application

make start

After completion, you can access the application at

http://localhost:APP_EXTERNAL_PORT

By default, the APP_EXTERNAL_PORT is 8000

http://localhost:8000

You can check the Swagger docs at

http://localhost:SWAGGER_EXTERNAL_PORT

By default, the SWAGGER_EXTERNAL_PORT is 8080

http://localhost:8080

Application Structure

The app folder contains only the files of a default Laravel installation.

The exa folder contains all the base classes provided by this skeleton to help you to develop your API.

The modules folder contains the code for your application. By default, you have an Auth module for Authentication, and User management out-of-the-box. It also provides a Common module that you can put shared logic for your application.

Creating Modules

To create new modules you can use this command

make art ARGS="make:module NAME"

This will create a new module inside the modules folder with the same structure of the other modules. It will create the module disabled by default. To enable it, add the new module name to the config/modules.php file.

Commands Available

For running Pint in the whole codebase use

make lint

For running the test suite use

make test

Use this command to see all the commands available

make

ExA Classes

Inside the exa folder, there are a lot of classes provided by this skeleton to help you to develop your API.

DTOs

  • DatatableDTO - This DTO provides basic filters for fetching data for datatables.
  • DateRangeDTO - This is an extension of the DatatableDTO providing additional parameters for date filters.

Exceptions

  • ExaException - Base class that all your custom exceptions should extend, so it can be handled properly by the app/Exceptions/Handler.
  • AccessDeniedException - Exception used for actions that the user is not allowed to perform.

Http

Middlewares

  • BlockViewerUsers - This middleware is a middleware applied to all routes that blocks any users with the role VIEWER to access any routes that are not GET routes.
  • HasRole - This middleware can be applied to routes that can be accessed only by users with a specific role or ADMINS that have full access.

Responses

  • ApiErrorResponse - The class to be used to return any error responses, configured to be used by the app/Exceptions/Handler.
  • ApiSuccessResponse - The class to be used to return any success responses, configured to be used with JSON Resources.
  • NoContentResponse - The class to be used to return empty responses.

Models

  • BaseModel - Base class that all your models should extend, already configured with the CommonQueries, LogChanges, SoftDeletes and UserActions Traits.
  • ChangeLog - Model for the table that logs all changes made on other models.
  • CommonQueries - This Trait provides a lot of methods for common queries that you can use with your models.
  • HasUuidField - This Trait provides UUID field support for models that don't want the UUID to be the primary key.
  • LogChanges - This Trait provides listeners for logging changes on the models. Check the class to know how you can customize your models with the properties of this Trait.
  • UserActions - This Trait provides listeners for logging changes made by users on the models populating the created_by, updated_by and deleted_by fields. Check the class to know how you can customize your models with the properties of this Trait.

Services

  • SlackClient - Class to send notifications to Slack. You need to add the needed configuration in your env, check the config/services.php file for the slack service to know how to configure it.

Support

  • Datatable - Class that provides functions for paginating, sorting and filtering data.
  • Formatter - Class that provides common values in constants and methods to format data in your application.
  • ChangeAction - Enum used by the LogChanges Trait.
  • SortOption - Enum for the sort order used by the DatatableDTO.

Credits

Contributing

Check the Contributing Guide.

laravel-exa's People

Contributors

msoares94 avatar wendelladriel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

laravel-exa's Issues

App container cant be build

When using the command make configure to start the application and while the images are beeing build, seems that the following command isnt working:

RUN composer install --prefer-dist && rm -rf /root/.composer

After a while (>300s) it will fail.

Try to delete the built images on your system and run make configure.

Thanks

Laravel ExA use default database Postgresql

Firstly, thank you for the amazing work with Laravel ExA.

Following the recent changes, I noticed that the SoftDeletes concept was added and this is great for auditing and data recovery. From what I noticed, UserActions was also added, this made the template more robust.

However, I would like to raise the following logical question, thinking about making the temple more robust.

Given that when using SoftDelete the records are now marked as logically deleted, that is, the "deleted_at" field is different from null, I suggest defining the Postgresql Database as the default for the template, considering that Postgresql supports uniqueness restrictions composed disregarding records marked as deleted by softDeletes, and this meets some business need to have, for example, a table with compound uniqueness.

Example:

TableA with $table->unique(['colunaA', 'colunaB']

Event flow

  • Register a record with the values: ColumnValueA1, ColumnValue2
  • Delete the record (This will make the record marked as deleted)
  • I try to create a new record with the same values ​​ColumnValueA1, ColumnValue2
  • MySQL will throw an exception as it does not support uniqueness, disregarding softDeletes records

Application container cannot be compiled with debug mode

In the `docker/app/Dockerfile file, it contains the instruction below, but when building the stack using the docker compose build command, it was observed that the flow is not executed even when the variables are defined.

# XDebug
RUN if [ ${XDEBUG_ENABLED} = true ]; then \
  pecl install xdebug && docker-php-ext-enable xdebug \
;fi

Arquivo: .env
XDEBUG_ENABLED=true

Arquivo: docker-compose.yml

            args:
                - M1_PROCESSOR=${M1_PROCESSOR}
                - XDEBUG_ENABLED=${XDEBUG_ENABLED}

I performed a test with the code:

# XDebug
RUN pecl install xdebug && docker-php-ext-enable xdebug

And I received in return:


 => ERROR [app  5/15] RUN pecl install xdebug && docker-php-ext-enable xdebug                                                                                                                                                     11.4s
------
 > [app  5/15] RUN pecl install xdebug && docker-php-ext-enable xdebug:
#0 6.743 downloading xdebug-3.2.2.tgz ...
#0 6.743 Starting to download xdebug-3.2.2.tgz (246,966 bytes)
#0 7.054 ....................................................done: 246,966 bytes
#0 11.14 96 source files, building
#0 11.14 running: phpize
#0 11.15 Configuring for:
#0 11.15 PHP Api Version:         20220829
#0 11.15 Zend Module Api No:      20220829
#0 11.15 Zend Extension Api No:   420220829
#0 11.16 Cannot find autoconf. Please check your autoconf installation and the
#0 11.16 $PHP_AUTOCONF environment variable. Then, rerun this script.
#0 11.16 
#0 11.16 ERROR: `phpize' failed
------
failed to solve: process "/bin/sh -c pecl install xdebug && docker-php-ext-enable xdebug" did not complete successfully: exit code: 1

runtime: failed to create new OS thread (have 2 already; errno=22)

Getting issue while running on the Mac M1 chip

santosh.js@192 laravel-exa % make configure        
cp .env.example .env \
	&& make install \
	&& make art ARGS="key:generate --ansi"
docker compose run --rm app composer install
WARN[0000] Found orphan containers ([laravel-exa-redis laravel-exa-mailpit]) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up. 
[+] Running 1/0
 ⠿ Container laravel-exa-db  Created                                                                                                                                                0.0s
[+] Running 1/1
 ⠿ Container laravel-exa-db  Started                                                                                                                                                0.3s
runtime: failed to create new OS thread (have 2 already; errno=22)
fatal error: newosproc

runtime stack:
runtime.throw(0x6430dc, 0x9)
	/usr/local/Cellar/go/1.8.1/libexec/src/runtime/panic.go:596 +0x95
runtime.newosproc(0xc42002c000, 0xc42003c000)
	/usr/local/Cellar/go/1.8.1/libexec/src/runtime/os_linux.go:163 +0x18c
runtime.newm(0x650dd0, 0x0)
	/usr/local/Cellar/go/1.8.1/libexec/src/runtime/proc.go:1628 +0x137
runtime.main.func1()
	/usr/local/Cellar/go/1.8.1/libexec/src/runtime/proc.go:126 +0x36
runtime.systemstack(0x75bb00)
	/usr/local/Cellar/go/1.8.1/libexec/src/runtime/asm_amd64.s:327 +0x79
runtime.mstart()
	/usr/local/Cellar/go/1.8.1/libexec/src/runtime/proc.go:1132

goroutine 1 [running]:
runtime.systemstack_switch()
	/usr/local/Cellar/go/1.8.1/libexec/src/runtime/asm_amd64.s:281 fp=0xc420028788 sp=0xc420028780
runtime.main()
	/usr/local/Cellar/go/1.8.1/libexec/src/runtime/proc.go:127 +0x6c fp=0xc4200287e0 sp=0xc420028788
runtime.goexit()
	/usr/local/Cellar/go/1.8.1/libexec/src/runtime/asm_amd64.s:2197 +0x1 fp=0xc4200287e8 sp=0xc4200287e0
make[1]: *** [install] Error 2
make: *** [configure] Error 2

API Documentation using Swagger

The documentation of an API is like a road code manual for a driver.
It guides users of the application, allowing developers to understand how the application works without having to read the entire implementation.

Would it be possible to provide documentation for the API using Swagger?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.