Skip to content

Commit

Permalink
Add base configuration for project
Browse files Browse the repository at this point in the history
  • Loading branch information
WendellAdriel committed Feb 15, 2023
1 parent b87930f commit 98e0b04
Show file tree
Hide file tree
Showing 49 changed files with 516 additions and 113 deletions.
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,8 @@ AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false

SLACK_NOTIFICATIONS_WEBHOOK=
SLACK_NOTIFICATIONS_CHANNEL="#general"
SLACK_NOTIFICATIONS_BOT_NAME="EXA-BOT"
SLACK_NOTIFICATIONS_BOT_ICON=:robot_face:
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ yarn-error.log
/.fleet
/.idea
/.vscode
cghooks.lock
4 changes: 1 addition & 3 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ protected function schedule(Schedule $schedule): void
*/
protected function commands(): void
{
$this->load(__DIR__.'/Commands');

require base_path('routes/console.php');
//
}
}
48 changes: 47 additions & 1 deletion app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace App\Exceptions;

use Exa\Exceptions\ExaException;
use Exa\Http\Responses\ApiErrorResponse;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Http\Response;
use Throwable;

class Handler extends ExceptionHandler
Expand All @@ -22,7 +25,12 @@ class Handler extends ExceptionHandler
* @var array<int, class-string<\Throwable>>
*/
protected $dontReport = [
//
\Illuminate\Auth\AuthenticationException::class,
\Illuminate\Auth\Access\AuthorizationException::class,
\Symfony\Component\HttpKernel\Exception\HttpException::class,
\Illuminate\Database\Eloquent\ModelNotFoundException::class,
\Illuminate\Session\TokenMismatchException::class,
\Illuminate\Validation\ValidationException::class,
];

/**
Expand All @@ -45,4 +53,42 @@ public function register(): void
//
});
}

public function render($request, Throwable $exception)
{
if ($exception instanceof \Illuminate\Auth\AuthenticationException) {
return $this->error($exception, Response::HTTP_UNAUTHORIZED, 'Unauthenticated');
}

if ($exception instanceof \Illuminate\Validation\ValidationException) {
return $this->error($exception, Response::HTTP_UNPROCESSABLE_ENTITY, $exception->errors());
}

if (
$exception instanceof \Illuminate\Database\Eloquent\ModelNotFoundException
|| $exception instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
) {
return $this->error($exception, Response::HTTP_NOT_FOUND, 'Resource not found');
}

if ($exception instanceof ExaException) {
return $this->error($exception);
}

return $this->error($exception, Response::HTTP_INTERNAL_SERVER_ERROR);
}

protected function unauthenticated($request, \Illuminate\Auth\AuthenticationException $exception)
{
return $this->error($exception, Response::HTTP_UNAUTHORIZED, 'Unauthenticated');
}

private function error(Throwable $exception, ?int $code = null, string|array|null $message = null): ApiErrorResponse
{
return new ApiErrorResponse(
$message ?? $exception->getMessage(),
$exception,
$code ?? $exception->getCode()
);
}
}
2 changes: 2 additions & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,7 @@ class Kernel extends HttpKernel
'signed' => \App\Http\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
'is_admin' => \Exa\Http\Middlewares\HasAdminRole::class,
'is_manager' => \Exa\Http\Middlewares\HasManagerRole::class,
];
}
16 changes: 15 additions & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Providers;

use Exa\Services\SlackClient;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
Expand All @@ -11,7 +12,7 @@ class AppServiceProvider extends ServiceProvider
*/
public function register(): void
{
//
$this->registerSlackClient();
}

/**
Expand All @@ -21,4 +22,17 @@ public function boot(): void
{
//
}

private function registerSlackClient(): void
{
$webhook = config('services.slack.webhook');
if (! is_null($webhook)) {
$this->app->bind(SlackClient::class, fn () => new SlackClient(
config('services.slack.bot.name'),
config('services.slack.bot.icon'),
$webhook,
config('services.slack.channel')
));
}
}
}
19 changes: 0 additions & 19 deletions app/Providers/BroadcastServiceProvider.php

This file was deleted.

41 changes: 33 additions & 8 deletions app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

namespace App\Providers;

use Exa\Support\Formatter;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;

Expand All @@ -17,7 +21,7 @@ class RouteServiceProvider extends ServiceProvider
*
* @var string
*/
public const HOME = '/home';
public const HOME = '/';

/**
* Define your route model bindings, pattern filters, and other route configuration.
Expand All @@ -26,14 +30,9 @@ public function boot(): void
{
$this->configureRateLimiting();

$this->routes(function () {
Route::middleware('api')
->prefix('api')
->group(base_path('routes/api.php'));
$this->configureIndexRoute();

Route::middleware('web')
->group(base_path('routes/web.php'));
});
$this->configureV1Routes();
}

/**
Expand All @@ -45,4 +44,30 @@ protected function configureRateLimiting(): void
return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
});
}

private function configureIndexRoute(): void
{
Route::get(self::HOME, function () {
return response()->json([
'application' => config('app.name'),
'environment' => config('app.env'),
'php_version' => phpversion(),
'laravel_version' => App::version(),
'status' => Response::HTTP_OK,
'datetime' => Carbon::now()->format(Formatter::API_DATETIME_FORMAT),
]);
})->name('login');
}

private function configureV1Routes(): void
{
$modules = config('modules');

foreach ($modules as $module) {
Route::prefix('v1')
->middleware('api')
->namespace("Modules\\{$module}\\Controllers")
->group(base_path("modules/{$module}/Routes/v1.php"));
}
}
}
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
],
"require": {
"php": "^8.2",
"ext-curl": "*",
"guzzlehttp/guzzle": "^7.5",
"laravel/framework": "^10.0",
"laravel/sanctum": "^3.2.1",
"laravel/tinker": "^2.8"
},
"require-dev": {
"brainmaestro/composer-git-hooks": "v3.0.0-alpha.1",
"fakerphp/faker": "^1.21",
"laravel/pint": "^1.5",
"laravel/sail": "^1.20.2",
Expand All @@ -51,12 +53,16 @@
}
},
"scripts": {
"post-install-cmd": [
"cghooks add --ignore-lock"
],
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"@php artisan package:discover --ansi"
],
"post-update-cmd": [
"@php artisan vendor:publish --tag=laravel-assets --ansi --force"
"@php artisan vendor:publish --tag=laravel-assets --ansi --force",
"cghooks update"
],
"post-root-package-install": [
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
Expand Down
82 changes: 79 additions & 3 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
|
*/

'name' => env('APP_NAME', 'Laravel'),
'name' => env('APP_NAME', 'ExA'),

/*
|--------------------------------------------------------------------------
Expand All @@ -28,7 +28,7 @@
|
*/

'env' => env('APP_ENV', 'production'),
'env' => env('APP_ENV', 'local'),

/*
|--------------------------------------------------------------------------
Expand Down Expand Up @@ -191,7 +191,6 @@
*/
App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
// App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,

Expand Down
Loading

0 comments on commit 98e0b04

Please sign in to comment.