Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEsaulenko/hw20. Проектирование API #626

Open
wants to merge 1 commit into
base: DEsaulenko/main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
.env
/.ssh/**
/mysql/*/data/**
!/mysql/*/data/.gitkeep
/mysql/*/logs/**
/redis/data/**
!/redis/data/.gitkeep
/nginx/logs/**
/php-8-workspace/logs/**

Expand All @@ -14,3 +9,8 @@
# Другие файлы
.idea/
.DS_Store

vendor/*
mysql/data
mysql/logs
mysql/dump
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# PHP_2023
# HW20

https://otus.ru/lessons/razrabotchik-php/?utm_source=github&utm_medium=free&utm_campaign=otus
## Проектирование API
Необходимо реализовать Rest API с использованием очередей.
Ваши клиенты будут отправлять запросы на обработку, а вы будете складывать их в очередь и возвращать номер запроса.
В фоновом режиме вы будете обрабатывать запросы, а ваши клиенты периодически, используя номер запроса, будут проверять статус его обработки.
Разрешается

Использование Composer-зависимостей
Использование микрофреймворков (Lumen, Silex и т.п.)

## Swagger
http://localhost:9080/api/documentation
15 changes: 15 additions & 0 deletions app/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[*.{yml,yaml}]
indent_size = 2
19 changes: 19 additions & 0 deletions app/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
APP_NAME=Lumen
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost
APP_TIMEZONE=UTC

LOG_CHANNEL=stack
LOG_SLACK_WEBHOOK_URL=

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

CACHE_DRIVER=file
QUEUE_CONNECTION=sync
6 changes: 6 additions & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/vendor
/.idea
Homestead.json
Homestead.yaml
.env
.phpunit.result.cache
6 changes: 6 additions & 0 deletions app/.styleci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
php:
preset: laravel
disabled:
- unused_use
js: true
css: true
26 changes: 26 additions & 0 deletions app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Lumen PHP Framework

[![Build Status](https://travis-ci.org/laravel/lumen-framework.svg)](https://travis-ci.org/laravel/lumen-framework)
[![Total Downloads](https://img.shields.io/packagist/dt/laravel/lumen-framework)](https://packagist.org/packages/laravel/lumen-framework)
[![Latest Stable Version](https://img.shields.io/packagist/v/laravel/lumen-framework)](https://packagist.org/packages/laravel/lumen-framework)
[![License](https://img.shields.io/packagist/l/laravel/lumen)](https://packagist.org/packages/laravel/lumen-framework)

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.

> **Note:** In the years since releasing Lumen, PHP has made a variety of wonderful performance improvements. For this reason, along with the availability of [Laravel Octane](https://laravel.com/docs/octane), we no longer recommend that you begin new projects with Lumen. Instead, we recommend always beginning new projects with [Laravel](https://laravel.com).

## Official Documentation

Documentation for the framework can be found on the [Lumen website](https://lumen.laravel.com/docs).

## Contributing

Thank you for considering contributing to Lumen! The contribution guide can be found in the [Laravel documentation](https://laravel.com/docs/contributions).

## Security Vulnerabilities

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.

## License

The Lumen framework is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).
Empty file.
29 changes: 29 additions & 0 deletions app/app/Console/Kernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Laravel\Lumen\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
//
];

/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
//
}
}
10 changes: 10 additions & 0 deletions app/app/Events/Event.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Events;

use Illuminate\Queue\SerializesModels;

abstract class Event
{
use SerializesModels;
}
16 changes: 16 additions & 0 deletions app/app/Events/ExampleEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Events;

class ExampleEvent extends Event
{
/**
* Create a new event instance.
*
* @return void
*/
public function __construct()
{
//
}
}
54 changes: 54 additions & 0 deletions app/app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace App\Exceptions;

use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Validation\ValidationException;
use Laravel\Lumen\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Throwable;

class Handler extends ExceptionHandler
{
/**
* A list of the exception types that should not be reported.
*
* @var array
*/
protected $dontReport = [
AuthorizationException::class,
HttpException::class,
ModelNotFoundException::class,
ValidationException::class,
];

/**
* Report or log an exception.
*
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
* @param \Throwable $exception
* @return void
*
* @throws \Exception
*/
public function report(Throwable $exception)
{
parent::report($exception);
}

/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Throwable $exception
* @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse
*
* @throws \Throwable
*/
public function render($request, Throwable $exception)
{
return parent::render($request, $exception);
}
}
49 changes: 49 additions & 0 deletions app/app/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\JsonResponse;
use Laravel\Lumen\Routing\Controller as BaseController;
use OpenApi\Annotations as OA;

class Controller extends BaseController
{
public const STATUS_MESSAGE_404 = 'not found';
public const STATUS_404 = 404;
/**
* @OA\Info(
* title="Example API",
* version="1.0",
* @OA\Contact(
* email="[email protected]",
* name="Support Team"
* )
* )
*/

protected function errorResponse(string $message = '', int $code = 400, array $errors = null): JsonResponse
{
return response()->json([
'errors' => $errors,
'data' => null,
'message' => $message,
'status' => 'error'
], $code);
}

/**
* @param array $data
* @param string $message
* @param int $code
* @return JsonResponse
*/
protected function successResponse(array $data, string $message = '', int $code = 200): JsonResponse
{
return response()->json([
'errors' => null,
'data' => $data,
'message' => $message,
'status' => 'success'
], $code);
}
}
71 changes: 71 additions & 0 deletions app/app/Http/Controllers/ReportController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers;

use App\Http\Responses\MegaReportResponse;
use App\Jobs\MegaReportJob;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use OpenApi\Annotations as OA;

class ReportController extends Controller
{
/**
* Add request to make mega report
*
* @OA\Post(
* path="/api/v1/report/mega_report",
* tags={"mega_report"},
* operationId="/api/v1/report/mega_report",
* @OA\Parameter(
* name="data1",
* in="query",
* description="Parameter data1",
* required=true,
* @OA\Schema(type="string")
* ),
* @OA\Parameter(
* name="data2",
* in="query",
* description="Parameter data2",
* required=true,
* @OA\Schema(type="string")
* ),
* @OA\Response(
* response="200",
* description="Returns job_status_id in data array",
* @OA\JsonContent(
* @OA\Examples(
* example="result",
* value={"errors":null, "data":{"job_status_id":123456}, "message":"success", "status":"success"},
* summary="some description"
* )
* )
* ),
* @OA\Response(
* response=400,
* description="Invalid input",
* @OA\JsonContent(
* @OA\Examples(
* example="result",
* value={"errors":null, "data":null, "message":"something wrong", "status":"error"},
* summary="some description"
* )
* )
* ),
* )
*/
public function megaReport(Request $request): JsonResponse
{
try {
$job = new MegaReportJob($request->all());
$this->dispatch($job);
$response = new MegaReportResponse($job->getJobStatusId());
} catch (\Throwable $th) {
return $this->errorResponse($th->getMessage(), $th->getCode());
}
return $this->successResponse($response->toArray());
}
}
Loading