2024-06-11 20:12:24 +02:00
|
|
|
|
/*
|
|
|
|
|
* This file is part of FirmTracker - Server.
|
|
|
|
|
*
|
|
|
|
|
* FirmTracker - Server is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* FirmTracker - Server is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with FirmTracker - Server. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2024-04-15 22:15:29 +02:00
|
|
|
|
using NHibernate;
|
|
|
|
|
using NHibernate.Cfg;
|
|
|
|
|
using NHibernate.Dialect;
|
|
|
|
|
using NHibernate.Driver;
|
|
|
|
|
using FirmTracker_Server.Controllers;
|
|
|
|
|
using FirmTracker_Server.nHibernate.Products;
|
2024-04-26 21:24:17 +02:00
|
|
|
|
using FirmTracker_Server.nHibernate;
|
2024-06-15 18:13:16 +02:00
|
|
|
|
using FirmTracker_Server.Utilities.Converters;
|
|
|
|
|
using FirmTracker_Server.Utilities.Swagger;
|
2024-10-23 23:16:01 +02:00
|
|
|
|
using FluentValidation;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.HttpOverrides;
|
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
|
using AutoMapper;
|
|
|
|
|
using Microsoft.AspNetCore.Authentication;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using FirmTracker_Server.Entities;
|
|
|
|
|
using FirmTracker_Server.Middleware;
|
|
|
|
|
using FirmTracker_Server.Services;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using FirmTracker_Server.Mappings;
|
|
|
|
|
|
2024-04-15 22:15:29 +02:00
|
|
|
|
|
|
|
|
|
namespace FirmTracker_Server
|
|
|
|
|
{
|
2024-10-23 23:16:01 +02:00
|
|
|
|
internal static class Program
|
2024-04-15 22:15:29 +02:00
|
|
|
|
{
|
2024-04-26 21:24:17 +02:00
|
|
|
|
|
2024-10-23 23:16:01 +02:00
|
|
|
|
public static async Task Main(string[] args)
|
2024-04-15 22:15:29 +02:00
|
|
|
|
{
|
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
2024-05-05 22:08:27 +02:00
|
|
|
|
string appDirectory = Directory.GetCurrentDirectory();
|
2024-04-26 21:24:17 +02:00
|
|
|
|
string configFilePath = Path.Combine(appDirectory, "appsettings.json");
|
|
|
|
|
string connectionString = "";
|
|
|
|
|
if (File.Exists(configFilePath))
|
|
|
|
|
{
|
|
|
|
|
var config = new ConfigurationBuilder()
|
|
|
|
|
.AddJsonFile(configFilePath)
|
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
var connectionstringsection = config.GetSection("AppSettings:ConnectionString");
|
|
|
|
|
|
|
|
|
|
connectionString = connectionstringsection.Value;
|
|
|
|
|
|
|
|
|
|
SessionFactory.Init(connectionString);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"The configuration file '{configFilePath}' was not found.");
|
|
|
|
|
}
|
2024-04-15 22:15:29 +02:00
|
|
|
|
|
|
|
|
|
TestClass test = new TestClass();
|
|
|
|
|
test.AddTestProduct();
|
2024-10-23 23:16:01 +02:00
|
|
|
|
|
2024-05-16 00:42:06 +02:00
|
|
|
|
builder.Services.AddCors(options =>
|
|
|
|
|
{
|
|
|
|
|
options.AddPolicy("AllowSpecificOrigin",
|
2024-11-06 22:01:08 +01:00
|
|
|
|
policy => policy.WithOrigins("http://localhost:3000", "https://localhost:7039", "https://localhost:5075", "https://localhost:3000")
|
2024-05-16 00:42:06 +02:00
|
|
|
|
.AllowAnyHeader()
|
2024-11-06 22:01:08 +01:00
|
|
|
|
.AllowAnyMethod()
|
|
|
|
|
.AllowCredentials());
|
2024-05-16 00:42:06 +02:00
|
|
|
|
});
|
2024-10-23 23:16:01 +02:00
|
|
|
|
builder.Services.ConfigureAutoMapper();
|
|
|
|
|
builder.Services.ConfigureServiceInjection();
|
2024-06-15 18:13:16 +02:00
|
|
|
|
builder.Services.AddControllers()
|
|
|
|
|
.AddJsonOptions(options =>
|
|
|
|
|
{
|
|
|
|
|
options.JsonSerializerOptions.Converters.Add(new DateTimeConverter());
|
|
|
|
|
});
|
|
|
|
|
;
|
2024-10-23 23:16:01 +02:00
|
|
|
|
builder.ConfigureAuthentication();
|
|
|
|
|
builder.Services.AddAuthorization();
|
2024-04-15 22:15:29 +02:00
|
|
|
|
builder.Services.AddEndpointsApiExplorer();
|
2024-06-15 18:13:16 +02:00
|
|
|
|
builder.Services.AddSwaggerGen(c =>
|
|
|
|
|
{
|
|
|
|
|
c.SchemaFilter<SwaggerDateTimeSchemaFilter>();
|
|
|
|
|
});
|
2024-04-15 22:15:29 +02:00
|
|
|
|
|
2024-10-23 23:16:01 +02:00
|
|
|
|
|
|
|
|
|
|
2024-04-15 22:15:29 +02:00
|
|
|
|
var app = builder.Build();
|
2024-04-26 21:24:17 +02:00
|
|
|
|
var configSwagger = new ConfigurationBuilder()
|
|
|
|
|
.SetBasePath(Directory.GetCurrentDirectory())
|
|
|
|
|
.AddJsonFile("appsettings.json")
|
|
|
|
|
.Build();
|
2024-04-15 22:15:29 +02:00
|
|
|
|
|
2024-04-26 21:24:17 +02:00
|
|
|
|
|
|
|
|
|
var port = configSwagger.GetValue<int>("Port", 5075);
|
|
|
|
|
var port2 = configSwagger.GetValue<int>("Port", 7039);
|
|
|
|
|
app.Urls.Add($"http://*:{port}");
|
|
|
|
|
app.Urls.Add($"https://*:{port2}");
|
|
|
|
|
|
|
|
|
|
try
|
2024-04-15 22:15:29 +02:00
|
|
|
|
{
|
|
|
|
|
app.UseSwagger();
|
2024-04-26 21:24:17 +02:00
|
|
|
|
app.UseSwaggerUI(c =>
|
|
|
|
|
{
|
|
|
|
|
c.SwaggerEndpoint($"/swagger/v1/swagger.json", "FirmTracker - TEST");
|
|
|
|
|
c.RoutePrefix = "swagger";
|
|
|
|
|
});
|
|
|
|
|
Console.WriteLine("uruchomiono swaggera");
|
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Nie uda<64>o si<73> uruchomi<6D> swaggera");
|
2024-04-15 22:15:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-06 22:01:08 +01:00
|
|
|
|
app.UseRouting();
|
2024-05-16 00:42:06 +02:00
|
|
|
|
app.UseCors("AllowSpecificOrigin");
|
2024-04-15 22:15:29 +02:00
|
|
|
|
|
|
|
|
|
|
2024-10-23 23:16:01 +02:00
|
|
|
|
app.UseAuthentication();
|
2024-05-16 00:42:06 +02:00
|
|
|
|
app.UseAuthorization();
|
2024-04-15 22:15:29 +02:00
|
|
|
|
|
2024-05-16 00:42:06 +02:00
|
|
|
|
|
2024-04-15 22:15:29 +02:00
|
|
|
|
app.MapControllers();
|
|
|
|
|
|
|
|
|
|
var configuration = new Configuration();
|
2024-04-26 21:24:17 +02:00
|
|
|
|
|
2024-04-15 22:15:29 +02:00
|
|
|
|
|
|
|
|
|
app.Run();
|
|
|
|
|
}
|
2024-10-23 23:16:01 +02:00
|
|
|
|
private static void ConfigureAuthentication(this WebApplicationBuilder builder)
|
|
|
|
|
{
|
|
|
|
|
var authenticationSettings = new Authentication.AuthenticationSettings();
|
|
|
|
|
builder.Configuration.GetSection("TokenConfig").Bind(authenticationSettings);
|
|
|
|
|
builder.Services.AddAuthentication(option => {
|
|
|
|
|
option.DefaultAuthenticateScheme = "Bearer";
|
|
|
|
|
option.DefaultScheme = "Bearer";
|
|
|
|
|
option.DefaultChallengeScheme = "Bearer";
|
|
|
|
|
}).AddJwtBearer(options => {
|
|
|
|
|
options.RequireHttpsMetadata = false;
|
|
|
|
|
options.SaveToken = true;
|
|
|
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
|
|
|
|
{
|
|
|
|
|
ValidIssuer = authenticationSettings.JwtIssuer,
|
|
|
|
|
ValidAudience = authenticationSettings.JwtIssuer,
|
|
|
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(authenticationSettings.JwtSecKey)),
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
builder.Services.AddSingleton(authenticationSettings);
|
|
|
|
|
}
|
|
|
|
|
private static void ConfigureAutoMapper(this IServiceCollection services)
|
|
|
|
|
{
|
|
|
|
|
var mapperConfig = new MapperConfiguration(mc => {
|
|
|
|
|
mc.AddProfile<LicenseMappingProfile>();
|
|
|
|
|
// mc.AddProfile<PayLinkerMappingProfile>();
|
|
|
|
|
});
|
|
|
|
|
var mapper = mapperConfig.CreateMapper();
|
|
|
|
|
services.AddSingleton(mapper);
|
|
|
|
|
services.AddAutoMapper(Assembly.GetExecutingAssembly());
|
|
|
|
|
}
|
|
|
|
|
private static void ConfigureServiceInjection(this IServiceCollection services)
|
|
|
|
|
{
|
|
|
|
|
services.AddScoped<IUserService, UserService>();
|
|
|
|
|
services.AddScoped<ErrorHandling>();
|
|
|
|
|
services.AddScoped<IPasswordHasher<User>, PasswordHasher<User>>();
|
|
|
|
|
services.AddMvc();
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-15 22:15:29 +02:00
|
|
|
|
}
|
2024-04-26 21:24:17 +02:00
|
|
|
|
}
|