2021-05-29 17:02:16 +00:00
|
|
|
using System;
|
2021-06-05 21:07:05 +00:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2021-06-05 16:39:47 +00:00
|
|
|
using System.IO;
|
2021-07-26 19:21:26 +00:00
|
|
|
using System.Text;
|
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
2021-05-28 20:52:56 +00:00
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
using Microsoft.Extensions.Logging;
|
2021-05-29 17:02:16 +00:00
|
|
|
using Microsoft.Extensions.Options;
|
2021-07-26 19:21:26 +00:00
|
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
using Microsoft.OpenApi.Models;
|
2021-05-30 18:16:01 +00:00
|
|
|
using MongoDB.Driver;
|
2021-07-16 17:49:46 +00:00
|
|
|
using Prometheus;
|
2021-06-21 14:46:44 +00:00
|
|
|
using Retroactiune.Core.Interfaces;
|
|
|
|
using Retroactiune.Core.Services;
|
|
|
|
using Retroactiune.Infrastructure;
|
2021-07-17 12:55:22 +00:00
|
|
|
using Sentry.AspNetCore;
|
2021-05-28 20:52:56 +00:00
|
|
|
|
|
|
|
namespace Retroactiune
|
|
|
|
{
|
2021-06-05 21:07:05 +00:00
|
|
|
[ExcludeFromCodeCoverage]
|
2021-05-28 20:52:56 +00:00
|
|
|
public class Startup
|
|
|
|
{
|
|
|
|
public Startup(IConfiguration configuration)
|
|
|
|
{
|
|
|
|
Configuration = configuration;
|
|
|
|
}
|
|
|
|
|
|
|
|
private IConfiguration Configuration { get; }
|
|
|
|
|
|
|
|
// This method gets called by the runtime. Use this method to add services to the container.
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
|
|
{
|
2021-05-29 17:02:16 +00:00
|
|
|
// Database Configuration
|
2021-06-11 20:17:03 +00:00
|
|
|
services.Configure<DatabaseSettings>(Configuration.GetSection(nameof(DatabaseSettings)));
|
|
|
|
services.AddSingleton<IDatabaseSettings>(sp =>
|
|
|
|
sp.GetRequiredService<IOptions<DatabaseSettings>>().Value);
|
2021-05-29 17:02:16 +00:00
|
|
|
|
2021-05-30 18:16:01 +00:00
|
|
|
// AutoMapper
|
2021-05-29 17:02:16 +00:00
|
|
|
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
|
|
|
|
|
2021-06-05 16:39:47 +00:00
|
|
|
|
2021-05-29 17:02:16 +00:00
|
|
|
// Services
|
2021-07-04 14:39:07 +00:00
|
|
|
services.AddSingleton<IFeedbackReceiversService, FeedbackReceiversService>();
|
2021-07-04 11:05:53 +00:00
|
|
|
services.AddSingleton<ITokensService, TokensService>();
|
2021-07-04 17:34:10 +00:00
|
|
|
services.AddSingleton<IFeedbacksService, FeedbacksService>();
|
2021-05-30 18:16:01 +00:00
|
|
|
services.AddSingleton<IMongoClient, MongoClient>(i =>
|
|
|
|
{
|
2021-06-11 20:17:03 +00:00
|
|
|
var settings = i.GetService<IOptions<DatabaseSettings>>();
|
2021-05-30 18:16:01 +00:00
|
|
|
return new MongoClient(settings.Value.ConnectionString);
|
|
|
|
});
|
2021-06-05 16:39:47 +00:00
|
|
|
|
2021-07-26 19:21:26 +00:00
|
|
|
services.AddAuthentication(options =>
|
|
|
|
{
|
|
|
|
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
|
|
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
|
|
}).AddJwtBearer(options =>
|
|
|
|
{
|
|
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
|
|
|
{
|
|
|
|
ValidIssuer = Configuration.GetSection("AuthorizationProvider:Domain").Value,
|
|
|
|
ValidAudience = Configuration.GetSection("AuthorizationProvider:Audience").Value,
|
|
|
|
IssuerSigningKey = new SymmetricSecurityKey(
|
|
|
|
Encoding.UTF8.GetBytes(Configuration.GetSection("AuthorizationProvider:SymmetricSecurityKey")
|
|
|
|
.Value))
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2021-05-29 17:02:16 +00:00
|
|
|
// WebAPI
|
2021-05-28 20:52:56 +00:00
|
|
|
services.AddControllers();
|
2021-06-05 16:39:47 +00:00
|
|
|
services.AddSwaggerGen(c =>
|
|
|
|
{
|
|
|
|
var filePath = Path.Combine(AppContext.BaseDirectory, "Retroactiune.WebAPI.xml");
|
|
|
|
c.IncludeXmlComments(filePath);
|
2021-07-26 19:21:26 +00:00
|
|
|
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
|
|
|
|
{
|
|
|
|
Type = SecuritySchemeType.Http,
|
|
|
|
BearerFormat = "JWT",
|
|
|
|
In = ParameterLocation.Header,
|
|
|
|
Scheme = "bearer",
|
|
|
|
Description = "Please insert JWT token into field"
|
|
|
|
});
|
|
|
|
|
|
|
|
c.AddSecurityRequirement(new OpenApiSecurityRequirement
|
|
|
|
{
|
|
|
|
{
|
|
|
|
new OpenApiSecurityScheme
|
|
|
|
{
|
|
|
|
Reference = new OpenApiReference
|
|
|
|
{
|
|
|
|
Type = ReferenceType.SecurityScheme,
|
|
|
|
Id = "Bearer"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
new string[] { }
|
|
|
|
}
|
|
|
|
});
|
2021-06-05 16:39:47 +00:00
|
|
|
});
|
2021-05-28 20:52:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
|
|
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<Startup> logger)
|
|
|
|
{
|
|
|
|
if (env.IsDevelopment())
|
|
|
|
{
|
|
|
|
app.UseDeveloperExceptionPage();
|
|
|
|
}
|
2021-05-29 17:02:16 +00:00
|
|
|
|
2021-07-16 17:49:46 +00:00
|
|
|
app.UseMetricServer();
|
2021-07-26 19:21:26 +00:00
|
|
|
|
2021-05-28 20:52:56 +00:00
|
|
|
app.UseSwagger();
|
|
|
|
app.UseSwaggerUI(c =>
|
|
|
|
{
|
|
|
|
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Retroactiune API");
|
|
|
|
c.RoutePrefix = "";
|
|
|
|
});
|
2021-05-29 17:02:16 +00:00
|
|
|
|
2021-05-28 20:52:56 +00:00
|
|
|
app.UseHttpsRedirection();
|
2021-07-26 19:21:26 +00:00
|
|
|
|
2021-07-17 12:55:22 +00:00
|
|
|
app.UseSentryTracing();
|
2021-05-28 20:52:56 +00:00
|
|
|
app.UseRouting();
|
|
|
|
|
2021-07-26 19:21:26 +00:00
|
|
|
app.UseAuthentication();
|
2021-05-28 20:52:56 +00:00
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
|
|
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
|
|
|
|
logger.LogInformation("Running");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|