-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into check-duplicate
- Loading branch information
Showing
8 changed files
with
131 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/Shortener/Extensions/WebApplicationBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using OpenTelemetry.Metrics; | ||
using OpenTelemetry.Resources; | ||
|
||
namespace Shortener.Extensions; | ||
|
||
public static class WebApplicationBuilderExtensions | ||
{ | ||
public static void ConfigureObservability(this WebApplicationBuilder builder) | ||
{ | ||
builder.Services.AddOpenTelemetry() | ||
.ConfigureResource(builder => builder.AddService(serviceName: "ShortenService", | ||
serviceVersion: "v1.0.1")) | ||
.WithMetrics(builder => | ||
{ | ||
builder.AddPrometheusExporter(); | ||
var meters = new string[] { ShortenDiagnostic.MeterName }; | ||
builder.AddMeter(meters); | ||
}); | ||
|
||
builder.Services.AddSingleton<ShortenDiagnostic>(); | ||
} | ||
|
||
public static void ConfigureAppSettings(this WebApplicationBuilder builder) | ||
{ | ||
builder.Services.Configure<AppSettings>(builder.Configuration); | ||
} | ||
|
||
public static void ConfigureDbContext(this WebApplicationBuilder builder) | ||
{ | ||
var settings = builder.Configuration.Get<AppSettings>(); | ||
builder.Services.AddDbContext<ShortenerDbContext>(options => | ||
{ | ||
if (settings is null) | ||
throw new ArgumentNullException(nameof(settings)); | ||
|
||
options.UseMongoDB(settings.MongoDbSetting.Host, | ||
settings.MongoDbSetting.DatabaseName); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,29 @@ | ||
using Shortener.Diagnostics; | ||
|
||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
builder.ConfigureObservability(); | ||
builder.ConfigureAppSettings(); | ||
builder.ConfigureDbContext(); | ||
|
||
builder.Configuration.AddEnvironmentVariables(); | ||
|
||
builder.Services.AddEndpointsApiExplorer(); | ||
builder.Services.AddSwaggerGen(); | ||
|
||
builder.Services.AddMemoryCache(); | ||
|
||
builder.Services.AddScoped<ShortenUrlService>(); | ||
builder.Services.AddSingleton<ShortenDiagnostic>(); | ||
|
||
var settings = builder.Configuration.Get<AppSettings>(); | ||
builder.Services.Configure<AppSettings>(builder.Configuration); | ||
|
||
builder.Services.AddDbContext<ShortenerDbContext>(options => | ||
{ | ||
if (settings is null) | ||
{ | ||
// TODO: create custom excetion | ||
throw new Exception("Invalid settings!"); | ||
} | ||
options.UseMongoDB(settings.MongoDbSetting.Host, | ||
settings.MongoDbSetting.DatabaseName); | ||
}); | ||
|
||
|
||
var app = builder.Build(); | ||
|
||
app.UseSwagger(); | ||
app.UseSwaggerUI(); | ||
|
||
app.UseHttpsRedirection(); | ||
|
||
app.MapShortenEndpoint(); | ||
app.MapRedirectEndpoint(); | ||
|
||
|
||
|
||
app.MapPrometheusScrapingEndpoint(); | ||
|
||
app.Run(); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters