159 lines
6.1 KiB
C#
159 lines
6.1 KiB
C#
//System
|
|
using System;
|
|
using System.IO;
|
|
using System.Reflection;
|
|
//Microsoft
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.OpenApi.Models;
|
|
//Others
|
|
using MassTransit;
|
|
//FTP
|
|
using LCU.Api.Internal.FTP.Configuration;
|
|
using LCU.Api.Internal.FTP.Security;
|
|
using LCU.Api.Internal.FTP.Services.FtpOpertionServices;
|
|
using LCU.Api.Internal.FTP.Services;
|
|
using LCU.Api.Internal.FTP.Consumers;
|
|
using LCU.Library.Internal.LcuMassTransitModels;
|
|
using LCU.Library.Internal.LcuMassTransit;
|
|
namespace LCU.Api.Internal.FTP;
|
|
|
|
public class Startup
|
|
{
|
|
public Startup(IConfiguration configuration)
|
|
{
|
|
Configuration = configuration;
|
|
}
|
|
|
|
public IConfiguration Configuration { get; }
|
|
readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
|
|
|
|
// This method gets called by the runtime. Use this method to add services to the container.
|
|
public void ConfigureServices(IServiceCollection services)
|
|
{
|
|
services.AddControllers();
|
|
services.AddCors(options =>
|
|
{
|
|
options.AddPolicy(MyAllowSpecificOrigins,
|
|
builder =>
|
|
{
|
|
builder.WithOrigins("http://localhost:3000", "https://localhost:5001",
|
|
"http://lcu-internal.com", "https://lcu-internal.com").AllowAnyHeader()
|
|
.AllowAnyMethod();
|
|
});
|
|
});
|
|
|
|
//Configurations
|
|
FtpApiConfiguration ftpApiConfiguration = new FtpApiConfiguration();
|
|
Configuration.GetSection("Primary").Bind(ftpApiConfiguration);
|
|
services.AddSingleton(ftpApiConfiguration);
|
|
services.AddHttpClient();
|
|
|
|
var connectionConfiguration = MassTransitConfigurationSetUp.AddConnectionConfiguration(ref services, Configuration);
|
|
var receiveEndpointConfiguration = MassTransitConfigurationSetUp.GetReceiveEndpointConfiguration(Configuration,
|
|
"FtpReceiveEndpoint");
|
|
services.AddSingleton(receiveEndpointConfiguration);
|
|
AddMassTransitServices(ref services, connectionConfiguration, receiveEndpointConfiguration);
|
|
|
|
//Services
|
|
services.AddScoped<IEnvironmentView, EnvironmentView>();
|
|
services.AddScoped<IQueueFtpService, QueueFtpService>();
|
|
services.AddScoped<IQueueSshFtpService, QueueSshFtpService>();
|
|
services.AddScoped<IFtpService, FtpService>();
|
|
services.AddScoped<IWinScpSftpUploadFileService, WinScpSftpUploadFileService>();
|
|
services.AddScoped<IWinScpSftpDownloadFilesService, WinScpSftpDownloadFilesService>();
|
|
services.AddScoped<IWinScpListDirectoryService, WinScpListDirectoryService>();
|
|
services.AddScoped<ISshFtpService, SshFtpService>();
|
|
services.AddScoped<IFileOperationsService, FileOperationsService>();
|
|
|
|
// Register the Swagger generator, defining 1 or more Swagger documents
|
|
services.AddSwaggerGen(c =>
|
|
{
|
|
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Ftp API", Version = "v1" });
|
|
// For this to work, I also had to check the generate xml box in build options.
|
|
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.XML";
|
|
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
|
|
c.IncludeXmlComments(xmlPath);
|
|
});
|
|
|
|
services.Configure<ApiBehaviorOptions>(options =>
|
|
{
|
|
options.SuppressModelStateInvalidFilter = true;
|
|
});
|
|
|
|
//Set up Authentication
|
|
services
|
|
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
// AzureAD config
|
|
.AddAzureAdBearer(options => Configuration.GetSection("AzureActiveDirectory").Bind(options));
|
|
|
|
services.AddIdentity<IdentityUser, IdentityRole>()
|
|
.AddDefaultTokenProviders();
|
|
}
|
|
|
|
public void AddMassTransitServices(ref IServiceCollection services,
|
|
ConnectionConfiguration connectionConfiguration,
|
|
ReceiveEndpointConfiguration receiveEndpointConfiguration)
|
|
{
|
|
services.AddMassTransit(x =>
|
|
{
|
|
x.AddConsumer<SftpConsumer>();
|
|
|
|
x.UsingRabbitMq((context, cfg) =>
|
|
{
|
|
cfg.Host(connectionConfiguration.HostUrl, "/", host =>
|
|
{
|
|
host.Username(connectionConfiguration.UserName);
|
|
host.Password(connectionConfiguration.Password);
|
|
});
|
|
|
|
cfg.ReceiveEndpoint(receiveEndpointConfiguration.ReceiveEndpoint, e =>
|
|
{
|
|
e.ConfigureConsumer<SftpConsumer>(context);
|
|
e.SetQuorumQueue();
|
|
});
|
|
});
|
|
});
|
|
services.AddMassTransitHostedService();
|
|
}
|
|
|
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
|
{
|
|
// Enable middleware to serve generated Swagger as a JSON endpoint.
|
|
app.UseSwagger();
|
|
|
|
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
|
|
// specifying the Swagger JSON endpoint.
|
|
app.UseSwaggerUI(c =>
|
|
{
|
|
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Ftp API");
|
|
});
|
|
|
|
if (env.IsDevelopment())
|
|
{
|
|
app.UseDeveloperExceptionPage();
|
|
}
|
|
else
|
|
{
|
|
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
|
|
app.UseHsts();
|
|
}
|
|
|
|
app.UseAuthentication();
|
|
app.UseCors(MyAllowSpecificOrigins);
|
|
app.UseHttpsRedirection();
|
|
app.UseRouting();
|
|
app.UseEndpoints(endpoints =>
|
|
{
|
|
endpoints.MapControllers();
|
|
}
|
|
);
|
|
}
|
|
} |