Skip to content

Commit 57bd600

Browse files
committed
feat: atualizada opções de configurações utilizando IOptions
1 parent 4a4d7ed commit 57bd600

File tree

30 files changed

+143
-237
lines changed

30 files changed

+143
-237
lines changed

.vscode/launch.json

+17-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
2-
// Use IntelliSense to find out which attributes exist for C# debugging
3-
// Use hover for the description of the existing attributes
4-
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
5-
"version": "0.2.0",
6-
"configurations": [
2+
// Use IntelliSense to find out which attributes exist for C# debugging
3+
// Use hover for the description of the existing attributes
4+
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
5+
"version": "0.2.0",
6+
"configurations": [
77
{
88
"name": "Demo.API",
99
"type": "coreclr",
@@ -30,7 +30,16 @@
3030
}
3131
},
3232
"env": {
33-
"ASPNETCORE_ENVIRONMENT": "Development"
33+
"ASPNETCORE_ENVIRONMENT": "Development",
34+
"GoogleApiConfiguration:GeoCodeURI": "https://maps.googleapis.com/maps/api/geocode",
35+
"GoogleApiConfiguration:MapsKey": "AIzaSyD6DxZlq_qWFSYs640jw9k_IvFltjM-Uew",
36+
"TokenConfigurations:Audience": "http://localhost:5000/api/signup",
37+
"TokenConfigurations:Issuer": "demo.api",
38+
"TokenConfigurations:Seconds": "1800",
39+
"ConnectionStrings:MongoDB": "mongodb://localhost:27017/demodb",
40+
"ConnectionStrings:MYSQL": "Server=localhost;Database=dbz;User=root;Password=root;",
41+
"ConnectionStrings:RabbitMQ": "amqp://guest:guest@localhost",
42+
"ConnectionStrings:Redis": "localhost,name=Demo.Api,defaultDatabase=1"
3443
},
3544
"sourceFileMap": {
3645
"/Views": "${workspaceFolder}/Views"
@@ -52,6 +61,6 @@
5261
"cwd": "${workspaceFolder}/Demo.Consumer.HandleMessages",
5362
"stopAtEntry": false,
5463
"console": "internalConsole"
55-
}
56-
,]
64+
},
65+
]
5766
}

Demo.API/.env

+12-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
JWT_ISS=httpmo://localhost:5000/api/signup
2-
JWT_AUD=demo.api
3-
JWT_EXPIRATION=1800
4-
MONGO_URI=mongodb://localhost:27017/demodb
5-
MYSQL_CONNECTION=Server=localhost;Database=dbz;User=root;Password=root;
6-
GOOGLE_GEOCODE_URI=https://maps.googleapis.com/maps/api/geocode
7-
GOOGLE_MAPS_KEY=AIzaSyD6DxZlq_qWFSYs640jw9k_IvFltjM-Uew
8-
REDIS_URI=localhost,name=Demo.Api,defaultDatabase=1
9-
RABBITMQ_URI=amqp://guest:guest@localhost
1+
GoogleApiConfiguration__GeoCodeURI=https://maps.googleapis.com/maps/api/geocode
2+
GoogleApiConfiguration__MapsKey=AIzaSyD6DxZlq_qWFSYs640jw9k_IvFltjM-Uew
3+
4+
TokenConfigurations__Audience=http://localhost:5000/api/signup
5+
TokenConfigurations__Issuer=demo.api
6+
TokenConfigurations__Seconds=1800
7+
8+
ConnectionStrings__MongoDB=mongodb://localhost:27017/demodb
9+
ConnectionStrings__MYSQL=Server=localhost;Database=dbz;User=root;Password=root;
10+
ConnectionStrings__RabbitMQ=amqp://guest:guest@localhost
11+
ConnectionStrings__Redis=localhost,name=Demo.Api,defaultDatabase=1
12+

Demo.API/Controllers/v1/ValuesController.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
using Demo.Core.Contracts.Values;
33
using Demo.Core.ExternalServices.Google;
44
using Demo.Core.Services;
5-
using Framework.Core.Helpers;
65
using Framework.WebAPI;
76
using Microsoft.AspNetCore.Authentication.JwtBearer;
87
using Microsoft.AspNetCore.Authorization;
98
using Microsoft.AspNetCore.Mvc;
9+
using Microsoft.Extensions.Options;
1010

1111
namespace Demo.API.Controllers
1212
{
@@ -21,11 +21,13 @@ public class ValuesController : BaseController
2121
{
2222
private readonly IValuesServices _services;
2323
private readonly IGoogleMapsAPI _mapsAPI;
24+
private readonly GoogleApiConfiguration _googleSettings;
2425

25-
public ValuesController(IValuesServices services, IGoogleMapsAPI mapsAPI)
26+
public ValuesController(IValuesServices services, IGoogleMapsAPI mapsAPI, IOptions<GoogleApiConfiguration> googleSettings)
2627
{
2728
_services = services;
2829
_mapsAPI = mapsAPI;
30+
_googleSettings = googleSettings.Value;
2931
}
3032

3133
[HttpGet]
@@ -47,7 +49,7 @@ public async Task<IActionResult> PostMessage([FromBody]PostMessageRequest reques
4749
[AllowAnonymous]
4850
public async Task<IActionResult> SearchCEP(string cep)
4951
{
50-
return Ok(await _mapsAPI.SearchAsync(cep, CommonHelpers.GetValueFromEnv<string>("GOOGLE_MAPS_KEY")));
52+
return Ok(await _mapsAPI.SearchAsync(cep, _googleSettings.MapsKey));
5153
}
5254
}
5355
}

Demo.API/Startup.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,21 @@ public override void AfterConfigureServices(IServiceCollection services)
3030
services.AddServices();
3131

3232
//Repositories
33-
services.AddMongoDB();
33+
services.AddMongoDB(Configuration);
3434
services.AddMongoRepositories();
3535

36-
services.AddMySql();
37-
services.AddExternalServices();
36+
services.AddMySql(Configuration);
37+
services.AddExternalServices(Configuration);
3838

3939
//GraphQL
4040
services.AddGraphQLTypes();
4141

4242
//Redis
43-
services.AddRedisCache();
43+
services.AddRedisCache(Configuration);
4444

4545

4646
//RabbitMQ
47-
services.AddRabbitBroker("demo.api");
47+
services.AddRabbitBroker("demo.api", Configuration);
4848
}
4949

5050
public override void BeforeConfigureApp(IApplicationBuilder app, IHostingEnvironment env)

Demo.Consumer.HandleMessages/.env

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
RABBITMQ_URI=amqp://guest:guest@localhost
2-
RABBIT_QOS=5
1+
ConnectionStrings__RabbitMQ=amqp://guest:guest@localhost
2+
RabbitMQSettings__QOS=5

Demo.Consumer.HandleMessages/Startup.cs

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.IO;
22
using Demo.Consumer.HandleMessages.Tasks;
3-
using dotenv.net;
43
using Framework.Core.Serializer;
54
using Framework.MessageBroker.RabbitMQ;
65
using Framework.WebAPI.HealthCheck;
@@ -17,9 +16,6 @@ public class Startup
1716

1817
public Startup(IConfiguration configuration)
1918
{
20-
if (File.Exists(".env"))
21-
DotEnv.Config();
22-
2319
Configuration = configuration;
2420
}
2521

@@ -28,7 +24,7 @@ public void ConfigureServices(IServiceCollection services)
2824
services.AddSingleton<JsonSerializerCommon>();
2925
services.AddHealthCheck();
3026

31-
services.AddRabbitBroker("demo.consumer");
27+
services.AddRabbitBroker("demo.consumer", Configuration);
3228
services.AddHostedService<TesteMessageHandlerTask>();
3329
}
3430

Demo.Consumer.HandleMessages/Tasks/TesteMessageHandlerTask.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public TesteMessageHandlerTask(IRabbitMQSubscriber subscriber, ILogger<TesteMess
1818
_logger = logger;
1919
}
2020

21-
public override void Dispose(){
21+
public override void Dispose()
22+
{
2223
_subscriber.Dispose();
2324
base.Dispose();
2425
}

Demo.Core/Data/MySql/MySqlExtensions.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Demo.Core.Data.MySql.Repositories;
22
using Framework.Data.MySql;
3-
using Framework.Core.Helpers;
43
using Microsoft.EntityFrameworkCore;
4+
using Microsoft.Extensions.Configuration;
55
using Microsoft.Extensions.DependencyInjection;
66
using Pomelo.EntityFrameworkCore.MySql.Infrastructure;
77
using System;
@@ -10,9 +10,9 @@ namespace Demo.Core.Data.MySql
1010
{
1111
public static class MySqlExtensions
1212
{
13-
public static IServiceCollection AddMySql(this IServiceCollection services)
13+
public static IServiceCollection AddMySql(this IServiceCollection services, IConfiguration configuration)
1414
{
15-
var connection = CommonHelpers.GetValueFromEnv<string>("MYSQL_CONNECTION");
15+
var connection = configuration.GetConnectionString("MYSQL");
1616

1717
services.AddDbContextPool<DbzMySqlContext>(options =>
1818
{
@@ -26,7 +26,7 @@ public static IServiceCollection AddMySql(this IServiceCollection services)
2626
services.AddScoped<ICharacterRepository, CharacterRepository>();
2727
services.AddScoped<IFamilyRepository, FamilyRepository>();
2828

29-
services.AddMySqlHealthCheck(connection);
29+
services.AddMySqlHealthCheck(configuration);
3030

3131
return services;
3232
}

Demo.Core/ExternalServices/RegisterExternalServices.cs

+8-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
using System.Net;
33
using System.Net.Http;
44
using Demo.Core.ExternalServices.Google;
5-
using Framework.Core.Helpers;
5+
using Microsoft.Extensions.Configuration;
66
using Microsoft.Extensions.DependencyInjection;
7+
using Microsoft.Extensions.Options;
78
using Polly;
89
using Polly.Extensions.Http;
910
using Polly.Timeout;
@@ -17,13 +18,17 @@ public static class RegisterExternalServices
1718
/// Adiciona todas as dependências de requisições http
1819
/// </summary>
1920
/// <param name="services"></param>
21+
/// <param name="configuration"></param>
2022
/// <returns></returns>
21-
public static IServiceCollection AddExternalServices(this IServiceCollection services)
23+
public static IServiceCollection AddExternalServices(this IServiceCollection services, IConfiguration configuration)
2224
{
25+
services.Configure<GoogleApiConfiguration>(configuration.GetSection(nameof(GoogleApiConfiguration)));
26+
var configs = services.BuildServiceProvider().GetRequiredService<IOptions<GoogleApiConfiguration>>().Value;
27+
2328
var timeoutPolicy = Policy.TimeoutAsync<HttpResponseMessage>(10);
2429

2530
services.AddRefitClient<IGoogleMapsAPI>()
26-
.ConfigureHttpClient(c => c.BaseAddress = new Uri(CommonHelpers.GetValueFromEnv<string>("GOOGLE_GEOCODE_URI")))
31+
.ConfigureHttpClient(c => c.BaseAddress = new Uri(configs.GeoCodeURI))
2732
.AddPolicyHandler(GetRetryPolicy())
2833
.AddPolicyHandler(timeoutPolicy);
2934

Framework.Core.Tests/Helpers/CommonHelpersTest.cs

-77
This file was deleted.

Framework.Core/Helpers/CommonHelpers.cs

-34
This file was deleted.

Framework.Data/CacheProviders/Redis/RedisExtensions.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
using System;
2-
using Framework.Core.Helpers;
32
using Microsoft.Extensions.DependencyInjection;
43
using StackExchange.Redis;
4+
using Microsoft.Extensions.Configuration;
55

66
namespace Framework.Data.CacheProviders
77
{
88
public static class RedisExtensions
99
{
10-
public static IServiceCollection AddRedisCache(this IServiceCollection services)
10+
public static IServiceCollection AddRedisCache(this IServiceCollection services, IConfiguration configuration)
1111
{
12-
var redisUri = CommonHelpers.GetValueFromEnv<string>("REDIS_URI");
12+
var redisUri = configuration.GetConnectionString("Redis");
1313
var options = ConfigurationOptions.Parse(redisUri);
1414

1515
if (!options.DefaultDatabase.HasValue)

Framework.Data/Framework.Data.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
<PackageReference Include="AspNetCore.HealthChecks.Redis" Version="2.2.3" />
1111
<PackageReference Include="AspNetCore.HealthChecks.System" Version="2.2.1" />
1212
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="2.2.5" />
13+
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
14+
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="2.2.0" />
1315
<PackageReference Include="MongoDB.Driver" Version="2.8.1" />
1416
<PackageReference Include="MySql.Data" Version="8.0.16" />
1517
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.2.0" />

Framework.Data/MongoDB/MongoDBExtensions.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
using Framework.Core.Helpers;
1+
using Microsoft.Extensions.Configuration;
22
using Microsoft.Extensions.DependencyInjection;
33
using MongoDB.Driver;
44

55
namespace Framework.Data.MongoDB
66
{
77
public static class MongoDBExtensions
88
{
9-
public static IServiceCollection AddMongoDB(this IServiceCollection services)
9+
public static IServiceCollection AddMongoDB(this IServiceCollection services, IConfiguration configuration)
1010
{
11-
var mongoUri = CommonHelpers.GetValueFromEnv<string>("MONGO_URI");
11+
var mongoUri = configuration.GetConnectionString("MongoDB");
1212

1313
// MongoClient (Singleton)
1414
var mongoUrl = new MongoUrl(mongoUri);
@@ -26,4 +26,4 @@ public static IServiceCollection AddMongoDB(this IServiceCollection services)
2626
return services;
2727
}
2828
}
29-
}
29+
}

0 commit comments

Comments
 (0)