2024-09-07 12:14:58 +00:00
|
|
|
|
2024-09-07 12:13:38 +00:00
|
|
|
using Serilog;
|
|
|
|
using Serilog.Exceptions;
|
2024-09-07 10:59:48 +00:00
|
|
|
using System.Text;
|
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
|
|
using Microsoft.IdentityModel.Tokens;
|
2024-09-07 14:24:12 +00:00
|
|
|
using Renis.Database;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
using Renis.Repositories;
|
|
|
|
using Renis.Services.Jwt;
|
|
|
|
using renis_backend.Services.Polis;
|
|
|
|
using renis_backend.Services;
|
|
|
|
using renis_backend.Services.UserPolises;
|
|
|
|
using renis_backend.Services.Users;
|
2024-09-07 12:13:38 +00:00
|
|
|
|
2024-09-07 08:06:51 +00:00
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
|
2024-09-07 14:24:12 +00:00
|
|
|
// Database context
|
|
|
|
builder.Services.AddDbContext<ApplicationContext>(x => {
|
|
|
|
var Hostname=Environment.GetEnvironmentVariable("DB_HOSTNAME") ?? "localhost";
|
|
|
|
var Port=Environment.GetEnvironmentVariable("DB_PORT") ?? "5432";
|
|
|
|
var Name=Environment.GetEnvironmentVariable("DB_NAME") ?? "postgres";
|
|
|
|
var Username=Environment.GetEnvironmentVariable("DB_USERNAME") ?? "postgres";
|
|
|
|
var Password=Environment.GetEnvironmentVariable("DB_PASSWORD") ?? "postgres";
|
|
|
|
x.UseNpgsql($"Server={Hostname}:{Port};Database={Name};Uid={Username};Pwd={Password};");
|
|
|
|
});
|
|
|
|
|
|
|
|
// Repos
|
|
|
|
builder.Services.AddScoped<IUserRepository, UserRepository>();
|
|
|
|
builder.Services.AddScoped<IPolisRepository, PolisRepository>();
|
|
|
|
builder.Services.AddScoped<IUserPolisRepository, UserPolisRepository>();
|
|
|
|
builder.Services.AddScoped<ICarRepository, CarRepository>();
|
|
|
|
|
|
|
|
// Services
|
|
|
|
builder.Services.AddScoped<IJwtService, JwtService>();
|
|
|
|
builder.Services.AddScoped<IPolisService, PolisService>();
|
|
|
|
builder.Services.AddScoped<IUserPolisService, UserPolisService>();
|
|
|
|
builder.Services.AddScoped<IUserService, UserService>();
|
|
|
|
|
2024-09-07 08:06:51 +00:00
|
|
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
|
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
|
|
builder.Services.AddSwaggerGen();
|
2024-09-07 09:13:28 +00:00
|
|
|
configureLogging();
|
2024-09-07 08:06:51 +00:00
|
|
|
var app = builder.Build();
|
|
|
|
|
|
|
|
// Configure the HTTP request pipeline.
|
|
|
|
if (app.Environment.IsDevelopment())
|
|
|
|
{
|
|
|
|
app.UseSwagger();
|
|
|
|
app.UseSwaggerUI();
|
|
|
|
}
|
|
|
|
|
2024-09-07 10:59:48 +00:00
|
|
|
// Authorization
|
|
|
|
builder.Services.AddAuthorization();
|
|
|
|
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
|
|
.AddJwtBearer(options =>
|
|
|
|
{
|
|
|
|
|
|
|
|
string issuer = Environment.GetEnvironmentVariable("JWT_ISSUER") ?? "renis";
|
|
|
|
string audience = Environment.GetEnvironmentVariable("JWT_AUDIENCE") ?? "renis";
|
|
|
|
string secret = Environment.GetEnvironmentVariable("JWT_SECRET") ?? "TopSecretKeyForTheProtectionOfChocolateCookiesAndOtherSweetThings";
|
|
|
|
|
|
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
|
|
|
{
|
|
|
|
ValidateIssuer = true,
|
|
|
|
ValidIssuer = issuer,
|
|
|
|
ValidateAudience = true,
|
|
|
|
ValidAudience = audience,
|
|
|
|
ValidateLifetime = true,
|
|
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secret)),
|
|
|
|
ValidateIssuerSigningKey = true
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2024-09-07 08:06:51 +00:00
|
|
|
app.UseHttpsRedirection();
|
|
|
|
|
|
|
|
app.Run();
|
|
|
|
|
2024-09-07 08:59:23 +00:00
|
|
|
void configureLogging(){
|
|
|
|
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production";
|
|
|
|
var configuration = new ConfigurationBuilder()
|
|
|
|
.AddJsonFile("appsettings.json",optional:false,reloadOnChange:true).Build();
|
|
|
|
Console.WriteLine(environment);
|
|
|
|
Console.WriteLine(configuration);
|
|
|
|
Log.Logger = new LoggerConfiguration()
|
|
|
|
.Enrich.FromLogContext()
|
|
|
|
.Enrich.WithExceptionDetails()
|
|
|
|
.WriteTo.Debug()
|
|
|
|
.WriteTo.Console()
|
|
|
|
.Enrich.WithProperty("Environment",environment)
|
|
|
|
.ReadFrom.Configuration(configuration)
|
|
|
|
.CreateLogger();
|
|
|
|
}
|