2020-10-25 16:15:06 +01:00
|
|
|
using AutoMapper;
|
2020-11-09 18:12:07 +01:00
|
|
|
using Birdmap.API.Extensions;
|
2020-10-25 16:15:06 +01:00
|
|
|
using Birdmap.API.Middlewares;
|
2020-11-09 18:12:07 +01:00
|
|
|
using Birdmap.API.Services.Hubs;
|
2020-10-24 23:23:22 +02:00
|
|
|
using Birdmap.BLL;
|
|
|
|
using Birdmap.DAL;
|
2020-10-21 17:04:54 +02:00
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
2020-10-21 11:05:17 +02:00
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
using Microsoft.AspNetCore.SpaServices.ReactDevelopmentServer;
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
using Microsoft.Extensions.Hosting;
|
2020-10-21 17:04:54 +02:00
|
|
|
using Microsoft.IdentityModel.Tokens;
|
2020-11-11 16:55:50 +01:00
|
|
|
using NSwag.Generation.Processors.Security;
|
2020-10-21 17:04:54 +02:00
|
|
|
using System.Text;
|
2020-10-21 11:05:17 +02:00
|
|
|
|
2020-11-08 23:29:50 +01:00
|
|
|
namespace Birdmap.API
|
2020-10-21 11:05:17 +02:00
|
|
|
{
|
|
|
|
public class Startup
|
|
|
|
{
|
|
|
|
public Startup(IConfiguration configuration)
|
|
|
|
{
|
|
|
|
Configuration = configuration;
|
|
|
|
}
|
|
|
|
|
|
|
|
public IConfiguration Configuration { get; }
|
|
|
|
|
|
|
|
// This method gets called by the runtime. Use this method to add services to the container.
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
|
|
{
|
2020-10-21 17:04:54 +02:00
|
|
|
services.AddControllersWithViews()
|
|
|
|
.AddJsonOptions(opt =>
|
|
|
|
{
|
2020-10-25 16:52:39 +01:00
|
|
|
opt.JsonSerializerOptions.Converters.Add(new System.Text.Json.Serialization.JsonStringEnumConverter());
|
|
|
|
opt.JsonSerializerOptions.IgnoreNullValues = true;
|
2020-10-21 17:04:54 +02:00
|
|
|
//opt.JsonSerializerOptions.PropertyNamingPolicy = new JsonNamingPolicy()
|
|
|
|
});
|
|
|
|
|
2020-10-24 23:23:22 +02:00
|
|
|
services.ConfigureBLL(Configuration);
|
|
|
|
services.ConfigureDAL(Configuration);
|
2020-10-21 17:04:54 +02:00
|
|
|
|
2020-10-25 16:15:06 +01:00
|
|
|
services.AddAutoMapper(typeof(Startup));
|
|
|
|
|
2020-11-09 18:12:07 +01:00
|
|
|
services.AddSignalR();
|
|
|
|
|
2020-10-24 23:23:22 +02:00
|
|
|
var key = Encoding.ASCII.GetBytes(Configuration["Secret"]);
|
2020-10-21 17:04:54 +02:00
|
|
|
services.AddAuthentication(opt =>
|
|
|
|
{
|
|
|
|
opt.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
|
|
opt.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
|
|
})
|
|
|
|
.AddJwtBearer(opt =>
|
|
|
|
{
|
2020-11-11 16:55:50 +01:00
|
|
|
//opt.RequireHttpsMetadata = false;
|
2020-10-21 17:04:54 +02:00
|
|
|
opt.SaveToken = true;
|
|
|
|
opt.IncludeErrorDetails = true;
|
|
|
|
opt.TokenValidationParameters = new TokenValidationParameters
|
|
|
|
{
|
|
|
|
ValidateIssuerSigningKey = true,
|
|
|
|
IssuerSigningKey = new SymmetricSecurityKey(key),
|
|
|
|
ValidateIssuer = false,
|
|
|
|
ValidateAudience = false
|
|
|
|
};
|
|
|
|
});
|
2020-10-21 11:05:17 +02:00
|
|
|
|
2020-11-09 18:12:07 +01:00
|
|
|
services.AddMqttClientServiceWithConfig(opt =>
|
|
|
|
{
|
|
|
|
var mqtt = Configuration.GetSection("Mqtt");
|
|
|
|
|
|
|
|
var mqttClient = mqtt.GetSection("ClientSettings");
|
|
|
|
var clientSettings = new
|
|
|
|
{
|
|
|
|
Id = mqttClient.GetValue<string>("Id"),
|
|
|
|
Username = mqttClient.GetValue<string>("Username"),
|
|
|
|
Password = mqttClient.GetValue<string>("Password"),
|
|
|
|
Topic = mqttClient.GetValue<string>("Topic"),
|
|
|
|
};
|
|
|
|
|
|
|
|
var mqttBrokerHost = mqtt.GetSection("BrokerHostSettings");
|
|
|
|
var brokerHostSettings = new
|
|
|
|
{
|
|
|
|
Host = mqttBrokerHost.GetValue<string>("Host"),
|
|
|
|
Port = mqttBrokerHost.GetValue<int>("Port"),
|
|
|
|
};
|
|
|
|
|
|
|
|
opt
|
|
|
|
.WithTopic(clientSettings.Topic)
|
|
|
|
.WithCredentials(clientSettings.Username, clientSettings.Password)
|
|
|
|
.WithClientId(clientSettings.Id)
|
|
|
|
.WithTcpServer(brokerHostSettings.Host, brokerHostSettings.Port);
|
|
|
|
});
|
|
|
|
|
2020-10-21 11:05:17 +02:00
|
|
|
// In production, the React files will be served from this directory
|
|
|
|
services.AddSpaStaticFiles(configuration =>
|
|
|
|
{
|
|
|
|
configuration.RootPath = "ClientApp/build";
|
|
|
|
});
|
2020-11-11 16:55:50 +01:00
|
|
|
|
|
|
|
services.AddSwaggerDocument(opt =>
|
|
|
|
{
|
|
|
|
opt.Title = "Birdmap";
|
|
|
|
opt.OperationProcessors.Add(new OperationSecurityScopeProcessor("Jwt Token"));
|
|
|
|
opt.AddSecurity("Jwt Token", new string[] { },
|
|
|
|
new NSwag.OpenApiSecurityScheme
|
|
|
|
{
|
|
|
|
Type = NSwag.OpenApiSecuritySchemeType.ApiKey,
|
|
|
|
Name = "Authorization",
|
|
|
|
In = NSwag.OpenApiSecurityApiKeyLocation.Header,
|
|
|
|
Description = "Bearer {token}",
|
|
|
|
});
|
|
|
|
});
|
2020-10-21 11:05:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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())
|
|
|
|
{
|
|
|
|
app.UseDeveloperExceptionPage();
|
|
|
|
}
|
2020-11-11 16:55:50 +01:00
|
|
|
|
|
|
|
app.UseMiddleware<ExceptionHandlerMiddleware>();
|
|
|
|
|
|
|
|
app.UseOpenApi();
|
|
|
|
app.UseSwaggerUi3();
|
2020-10-21 11:05:17 +02:00
|
|
|
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
app.UseStaticFiles();
|
|
|
|
app.UseSpaStaticFiles();
|
|
|
|
|
|
|
|
|
2020-10-21 17:04:54 +02:00
|
|
|
app.UseAuthentication();
|
2020-11-11 16:55:50 +01:00
|
|
|
app.UseRouting();
|
|
|
|
app.UseAuthorization();
|
2020-10-21 17:04:54 +02:00
|
|
|
|
2020-10-21 11:05:17 +02:00
|
|
|
app.UseEndpoints(endpoints =>
|
|
|
|
{
|
2020-10-25 16:52:39 +01:00
|
|
|
endpoints.MapHealthChecks("/health");
|
2020-10-21 17:04:54 +02:00
|
|
|
endpoints.MapControllers();
|
2020-11-09 18:12:07 +01:00
|
|
|
endpoints.MapHub<DevicesHub>("/hubs/devices");
|
2020-10-21 11:05:17 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
app.UseSpa(spa =>
|
|
|
|
{
|
|
|
|
spa.Options.SourcePath = "ClientApp";
|
|
|
|
|
|
|
|
if (env.IsDevelopment())
|
|
|
|
{
|
|
|
|
spa.UseReactDevelopmentServer(npmScript: "start");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|