162 lines
6.3 KiB
C#
162 lines
6.3 KiB
C#
using LiMan.APi.Data;
|
|
using LiMan.DB;
|
|
using LiMan.DbSync.Services;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Identity.UI.Services;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.FileProviders;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.OpenApi.Models;
|
|
using StackExchange.Redis;
|
|
using StackExchange.Redis.Extensions.Core.Configuration;
|
|
using StackExchange.Redis.Extensions.Newtonsoft;
|
|
using System;
|
|
using System.IO;
|
|
using System.Reflection;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace LiMan.Serv
|
|
{
|
|
public class Startup
|
|
{
|
|
#region Public Constructors
|
|
|
|
public Startup(IConfiguration configuration)
|
|
{
|
|
Configuration = configuration;
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Properties
|
|
|
|
public IConfiguration Configuration { get; }
|
|
|
|
#endregion Public Properties
|
|
|
|
#region Public Methods
|
|
|
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
|
{
|
|
if (env.IsDevelopment() || env.IsStaging())
|
|
{
|
|
app.UseDeveloperExceptionPage();
|
|
|
|
// valido solo in sviluppo
|
|
app.UseSwagger(c =>
|
|
{
|
|
c.RouteTemplate = "/swagger/{documentName}/swagger.json";
|
|
});
|
|
app.UseSwaggerUI(c =>
|
|
{
|
|
c.SwaggerEndpoint("v1/swagger.json", "LiMan.Api");
|
|
});
|
|
}
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
app.UseRouting();
|
|
|
|
app.UseAuthorization();
|
|
|
|
// verifico da conf se sia linux o windows x file da accedere...
|
|
if (Configuration["HostOs"] == "Win")
|
|
{
|
|
app.UseFileServer(new FileServerOptions
|
|
{
|
|
FileProvider = new PhysicalFileProvider(@"\\stor01\TEAM DRIVES\40_FileUpload\unsafe_uploads"),
|
|
RequestPath = new PathString("/unsafe_uploads"),
|
|
EnableDirectoryBrowsing = true
|
|
//EnableDirectoryBrowsing = false
|
|
});
|
|
app.UseFileServer(new FileServerOptions
|
|
{
|
|
FileProvider = new PhysicalFileProvider(@"\\stor01\TEAM DRIVES\40_FileUpload\AppBackup"),
|
|
RequestPath = new PathString("/app_backup"),
|
|
EnableDirectoryBrowsing = false
|
|
});
|
|
}
|
|
else
|
|
{
|
|
app.UseFileServer(new FileServerOptions
|
|
{
|
|
FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "unsafe_uploads")),
|
|
RequestPath = new PathString("/unsafe_uploads"),
|
|
EnableDirectoryBrowsing = true
|
|
//EnableDirectoryBrowsing = false
|
|
});
|
|
app.UseFileServer(new FileServerOptions
|
|
{
|
|
FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "app_backup")),
|
|
RequestPath = new PathString("/app_backup"),
|
|
EnableDirectoryBrowsing = false
|
|
});
|
|
}
|
|
|
|
app.UseEndpoints(endpoints =>
|
|
{
|
|
endpoints.MapControllers();
|
|
});
|
|
}
|
|
|
|
// This method gets called by the runtime. Use this method to add services to the container.
|
|
public void ConfigureServices(IServiceCollection services)
|
|
{
|
|
services.AddControllers();
|
|
services.AddSwaggerGen(c =>
|
|
{
|
|
c.SwaggerDoc("v1", new OpenApiInfo { Title = "LiMan.Api", Version = "v1" });
|
|
// Set the comments path for the Swagger JSON and UI.
|
|
var xmlPath = Path.Combine(AppContext.BaseDirectory, $"{Assembly.GetExecutingAssembly().GetName().Name}.xml");
|
|
c.IncludeXmlComments(xmlPath);
|
|
});
|
|
|
|
// abilitazione x email management con MailKit
|
|
services.AddTransient<IEmailSender, MailKitEmailSender>();
|
|
services.Configure<MailKitEmailSenderOptions>(options =>
|
|
{
|
|
options.Host_Address = Configuration["ExternalProviders:MailKit:SMTP:Address"];
|
|
options.Host_Port = Convert.ToInt32(Configuration["ExternalProviders:MailKit:SMTP:Port"]);
|
|
options.Host_Username = Configuration["ExternalProviders:MailKit:SMTP:Account"];
|
|
options.Host_Password = Configuration["ExternalProviders:MailKit:SMTP:Password"] ?? "";
|
|
options.Sender_EMail = Configuration["ExternalProviders:MailKit:SMTP:SenderEmail"];
|
|
options.Sender_Name = Configuration["ExternalProviders:MailKit:SMTP:SenderName"];
|
|
options.Host_SecureSocketOptions = MailKit.Security.SecureSocketOptions.Auto;
|
|
});
|
|
|
|
|
|
services.AddControllers()
|
|
.AddJsonOptions(c => c.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve);
|
|
|
|
services.AddSingleton<ApiDataService>();
|
|
|
|
services.AddStackExchangeRedisExtensions<NewtonsoftSerializer>((options) =>
|
|
{
|
|
return Configuration.GetSection("Redis").Get<RedisConfiguration>();
|
|
});
|
|
|
|
|
|
// REDIS setup
|
|
var redisConnString = Configuration.GetConnectionString("Redis");
|
|
string connStringRedis = redisConnString ?? "localhost:6379, DefaultDatabase=1, connectTimeout=5000, syncTimeout=5000, asyncTimeout=5000, abortConnect=false, ssl=false";
|
|
string redisSrvAddr = "127.0.0.1";
|
|
if (connStringRedis.IndexOf(":") >= 0)
|
|
{
|
|
redisSrvAddr = connStringRedis.Substring(0, connStringRedis.IndexOf(":"));
|
|
}
|
|
// avvio oggetto shared x redis...
|
|
var redisMultiplexer = ConnectionMultiplexer.Connect(connStringRedis);
|
|
// Add services x accesso dati
|
|
services.AddSingleton<IConnectionMultiplexer>(redisMultiplexer);
|
|
|
|
// aggiungo servizio sync DB
|
|
services.AddSingleton<DbSyncService>();
|
|
}
|
|
|
|
#endregion Public Methods
|
|
}
|
|
} |