Skip to content

Commit 3b1d251

Browse files
authored
Merge pull request #27 from secultce/develop
Atualizações da develop
2 parents 7c79e07 + 207a90f commit 3b1d251

58 files changed

Lines changed: 1764 additions & 515 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.editorconfig

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,3 @@ trim_trailing_whitespace = false
1313

1414
[*.{yml,yaml}]
1515
indent_size = 2
16-
17-
[docker-compose.yml]
18-
indent_size = 4

.env.testing

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
APP_NAME=Laravel
2-
APP_ENV=local
2+
APP_ENV=testing
33
APP_KEY=base64:Bq7kBLzjHsoonNahT0xa08HqG4jQD62dw1LRXMCNdCE=
4-
APP_DEBUG=true
4+
APP_DEBUG=false
55
APP_TIMEZONE=UTC
66
APP_URL=http://localhost
77

@@ -20,11 +20,11 @@ LOG_DEPRECATIONS_CHANNEL=null
2020
LOG_LEVEL=debug
2121

2222
DB_CONNECTION=pgsql
23-
DB_HOST=172.19.18.161
24-
DB_PORT=5433
25-
DB_DATABASE=api_email
26-
DB_USERNAME=postgres
27-
DB_PASSWORD=12345678
23+
DB_HOST=db-email
24+
DB_PORT=5432
25+
DB_DATABASE=api_email_test
26+
DB_USERNAME=api_email
27+
DB_PASSWORD=pass
2828

2929
SESSION_DRIVER=database
3030
SESSION_LIFETIME=120
@@ -55,7 +55,6 @@ MAIL_ENCRYPTION=null
5555
MAIL_FROM_ADDRESS="hello@example.com"
5656
MAIL_FROM_NAME="${APP_NAME}"
5757

58-
5958
AWS_ACCESS_KEY_ID=
6059
AWS_SECRET_ACCESS_KEY=
6160
AWS_DEFAULT_REGION=us-east-1
@@ -83,7 +82,7 @@ NGINX_PHP_UPSTREAM_CONTAINER=php-fpm
8382
NGINX_PHP_UPSTREAM_PORT=9000
8483
NGINX_SSL_PATH=./docker/nginx/ssl/
8584
### PARA AS FILAS
86-
MAPA_URL=http://localhost:8088/
85+
MAPA_URL=http://172.19.18.213:8088/
8786

8887
#### RABBITMW ####
8988
RABBITMQ_DEFAULT_HOST=rabbitmq

.github/workflows/cicd.yaml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: CICD
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
types:
8+
- closed
9+
10+
workflow_dispatch:
11+
12+
jobs:
13+
BUILD:
14+
if: github.event.pull_request.merged == true
15+
runs-on: ubuntu-latest
16+
steps:
17+
18+
- uses: actions/checkout@v4.1.1
19+
20+
- name: Read version file
21+
id: get_version
22+
run: |
23+
VERSION=$(cat version.txt)
24+
echo "app_version=$VERSION" >> $GITHUB_ENV
25+
26+
- name: Docker Login
27+
uses: docker/login-action@v3.0.0
28+
with:
29+
username: ${{ secrets.DOCKERHUB_USER }}
30+
password: ${{ secrets.DOCKERHUB_PASSWORD }}
31+
32+
- name: Criação da Imagem docker
33+
uses: docker/build-push-action@v5.0.0
34+
with:
35+
context: ./
36+
file: ./Dockerfile
37+
push: true
38+
tags: |
39+
secultceara/api-email:latest
40+
secultceara/api-email:${{ env.app_version }}
41+
42+
#DEPLOY:
43+
# needs: BUILD
44+
# runs-on: mapahomolog
45+
# steps:
46+
# - name: Pull da imagem do dockerhub
47+
# run: sudo docker pull secultceara/mapasculturais:homolog
48+
# - name: Restart do docker-compose para atualizar o container com a nova imagem
49+
# run: cd /opt/docker/mapa5 && sudo docker-compose down && sudo docker-compose up -d

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,7 @@ docker-data
2121
data
2222
storage
2323
/docker/logs/
24+
docker/logs/nginx/error.log
25+
26+
xdebug.log
27+
tests/coverage-html

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,14 @@ Instruções [aqui](https://github.com/secultce/api-email/blob/develop/docs/TASK
5252
### JWT Auth
5353

5454
É usado o pacote jwt-auth (https://jwt-auth.readthedocs.io/en/develop/) para criar tokens de acesso que são enviados nas requisições, para garantir que pessoas não autorizadas acessem os dados retornados nos endpoints usados na api.
55+
56+
### Workers
57+
58+
Para cada novo componente que precise trabalhar com as tarefas de filas por exemplo, deve ser adicionar o comando no arquivo *supervisord.conf* , que se encontra no caminho ***docker/php-fpm/*** e escrever algo como:
59+
60+
[program:name_command]
61+
command=php artisan command:command
62+
autostart=true
63+
autorestart=true
64+
stderr_logfile=/dev/stderr
65+
stdout_logfile=/dev/stdout

app/Console/Commands/ConsumePublishedRecourseEmails.php

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
namespace App\Console\Commands;
44

55
use App\Mail\PublishedRecourse;
6+
use Exception;
67
use Illuminate\Console\Command;
78
use Illuminate\Support\Facades\Mail;
89
use PhpAmqpLib\Connection\AMQPStreamConnection;
10+
use PhpAmqpLib\Message\AMQPMessage;
911

1012
class ConsumePublishedRecourseEmails extends Command
1113
{
@@ -25,28 +27,24 @@ class ConsumePublishedRecourseEmails extends Command
2527

2628
/**
2729
* Execute the console command.
30+
*
31+
* @throws Exception
2832
*/
29-
public function handle()
33+
public function handle(): void
3034
{
31-
// Conectar ao RabbitMQ
32-
$connection = new AMQPStreamConnection(env('RABBITMQ_DEFAULT_HOST'), env('RABBITMQ_DEFAULT_PORT'), env('RABBITMQ_DEFAULT_USER'), env('RABBITMQ_DEFAULT_PASS'));
35+
$queue = config('app.rabbitmq.queues.published_recourses_queue');
36+
$connection = new AMQPStreamConnection(
37+
config('app.rabbitmq.host'),
38+
config('app.rabbitmq.port'),
39+
config('app.rabbitmq.user'),
40+
config('app.rabbitmq.pass'),
41+
);
3342
$channel = $connection->channel();
43+
$channel->queue_declare($queue, false, true, false, false);
3444

35-
$channel->queue_declare('published_recourses_queue', false, true, false, false);
3645
$this->info('🎯 Aguardando e-mails para envio...');
3746

38-
$callback = function ($msg) {
39-
$data = json_decode($msg->body, true);
40-
41-
if ($this->sendEmail($data)) {
42-
$msg->ack(); // Confirma o processamento
43-
$this->info("📧 E-mail enviado para: {$data['email']}");
44-
} else {
45-
$this->error("❌ Falha ao enviar e-mail para: {$data['email']}");
46-
}
47-
};
48-
49-
$channel->basic_consume('published_recourses_queue', '', false, false, false, false, $callback);
47+
$channel->basic_consume(queue: $queue, callback: $this->processMessage(...));
5048

5149
while ($channel->is_consuming()) {
5250
$channel->wait();
@@ -62,8 +60,24 @@ private function sendEmail($data): bool
6260
Mail::to($data['email'])->send(new PublishedRecourse($data));
6361

6462
return true;
65-
} catch (\Exception $e) {
63+
} catch (Exception $e) {
64+
logger($e->getMessage());
65+
6666
return false;
6767
}
6868
}
69+
70+
protected function processMessage(AMQPMessage $msg): void
71+
{
72+
$data = json_decode($msg->body, true);
73+
74+
if ($this->sendEmail($data)) {
75+
$msg->ack(); // Confirma o processamento
76+
$this->info("📧 E-mail enviado para: {$data['email']}");
77+
78+
return;
79+
}
80+
81+
$this->error("❌ Falha ao enviar e-mail para: {$data['email']}");
82+
}
6983
}

app/Console/Commands/ConsumerCommand.php

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
namespace App\Console\Commands;
44

55
use App\Mail\AnswerNotification;
6-
use Illuminate\Support\Facades\Mail;
76
use App\Mail\EmailRegistrationOpp;
87
use Illuminate\Console\Command;
8+
use Illuminate\Support\Facades\Mail;
99
use PhpAmqpLib\Connection\AMQPStreamConnection;
10+
use PhpAmqpLib\Message\AMQPMessage;
1011

1112
class ConsumerCommand extends Command
1213
{
@@ -22,51 +23,57 @@ class ConsumerCommand extends Command
2223
*
2324
* @var string
2425
*/
25-
protected $description = 'Cosumidor das filas do Rabbitmq para as diligências';
26+
protected $description = 'Consumidor das filas do Rabbitmq para as diligências';
2627

2728
/**
2829
* Execute the console command.
30+
*
31+
* @throws \Exception
2932
*/
30-
public function handle()
33+
public function handle(): int
3134
{
32-
$queue = env('RABBITMQ_QUEUE_PC');
35+
$queue = config('app.rabbitmq.queues.accountability');
3336
$connection = new AMQPStreamConnection(
34-
env('RABBITMQ_DEFAULT_HOST'),
35-
env('RABBITMQ_DEFAULT_PORT'),
36-
env('RABBITMQ_DEFAULT_USER'),
37-
env('RABBITMQ_DEFAULT_PASS'),
38-
'/'
37+
config('app.rabbitmq.host'),
38+
config('app.rabbitmq.port'),
39+
config('app.rabbitmq.user'),
40+
config('app.rabbitmq.pass'),
41+
'/',
3942
);
4043
$channel = $connection->channel();
4144
$channel->queue_declare($queue, false, true, false, false);
4245

43-
$callback = function ($msg) {
44-
$data = json_decode($msg->body);
45-
if( $msg->getRoutingKey() == env('RABBITMQ_QUEUE_PC_ROUTE_KEY_PROP') )
46-
{
47-
Mail::to($data->email)->send(new EmailRegistrationOpp(
48-
$data->name,
49-
$data->number,
50-
$data->days
51-
));
52-
}
53-
if($msg->getRoutingKey() == env('RABBITMQ_QUEUE_PC_ROUTE_KEY_ADM'))
54-
{
55-
Mail::to($data->comission)->cc($data->owner)->send(new AnswerNotification(
56-
$data->registration,
57-
));
58-
};
59-
$msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
60-
};
6146
$channel->basic_qos(null, 1, null);
62-
$channel->basic_consume($queue, '', false, false, false, false, $callback);
47+
$channel->basic_consume(queue: $queue, callback: $this->processMessage(...));
48+
6349
while ($channel->is_consuming()) {
50+
$this->info('🎯 Aguardando e-mails para envio...');
6451
$channel->wait();
6552
}
6653

6754
$channel->close();
6855

6956
return Command::SUCCESS;
57+
}
58+
59+
protected function processMessage(AMQPMessage $msg): void
60+
{
61+
$data = json_decode($msg->body);
62+
63+
if ($msg->getRoutingKey() == config('app.rabbitmq.route_key_prop')) {
64+
Mail::to($data->email)->send(new EmailRegistrationOpp(
65+
$data->name,
66+
$data->number,
67+
$data->days
68+
));
69+
}
70+
71+
if ($msg->getRoutingKey() == config('app.rabbitmq.route_key_adm')) {
72+
Mail::to($data->comission)->cc($data->owner)->send(new AnswerNotification(
73+
$data->registration
74+
));
75+
}
7076

77+
$msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
7178
}
7279
}

app/Http/Controllers/UserController.php

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -9,78 +9,22 @@
99

1010
class UserController extends Controller
1111
{
12-
/**
13-
* Display a listing of the resource.
14-
*/
15-
public function index()
16-
{
17-
//
18-
}
19-
20-
/**
21-
* Show the form for creating a new resource.
22-
*/
23-
public function create()
24-
{
25-
//
26-
}
27-
2812
/**
2913
* Handle an incoming registration request.
3014
*
3115
* @throws \Illuminate\Validation\ValidationException
3216
*/
3317
public function store(Request $request): JsonResponse
3418
{
35-
36-
// $request->validate([
37-
// 'name' => ['required', 'string', 'max:255'],
38-
// 'email' => ['required', 'string', 'lowercase', 'email', 'max:255', 'unique:'.User::class],
39-
// 'password' => ['required', 'confirmed', Rules\Password::defaults()],
40-
// ]);
41-
4219
$user = User::create([
4320
'name' => $request->name,
4421
'email' => $request->email,
4522
'password' => Hash::make($request->password),
4623
]);
47-
// event(new Registered($user));
4824

4925
$token = $user->createToken('here-token-name');
5026

51-
return response()->json( ['token' => $token->plainTextToken]);
52-
}
53-
54-
/**
55-
* Display the specified resource.
56-
*/
57-
public function show(string $id)
58-
{
59-
//
60-
}
61-
62-
/**
63-
* Show the form for editing the specified resource.
64-
*/
65-
public function edit(string $id)
66-
{
67-
//
68-
}
69-
70-
/**
71-
* Update the specified resource in storage.
72-
*/
73-
public function update(Request $request, string $id)
74-
{
75-
//
76-
}
77-
78-
/**
79-
* Remove the specified resource from storage.
80-
*/
81-
public function destroy(string $id)
82-
{
83-
//
27+
return response()->json(['token' => $token->plainTextToken]);
8428
}
8529

8630
public function draft(Request $request)

0 commit comments

Comments
 (0)