mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 17:51:20 +00:00
256 lines
9.2 KiB
C#
256 lines
9.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Text.Json.Serialization;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Diagnostics;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.HttpsPolicy;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using MyCore.Framework.Models;
|
|
using MyCore.Interfaces.Models;
|
|
using MyCore.Service;
|
|
using MyCore.Services;
|
|
using MyCore.Services.MyControlPanel;
|
|
using NSwag;
|
|
using NSwag.Generation.AspNetCore;
|
|
using NSwag.Generation.Processors.Security;
|
|
using MyCore.Framework.Business;
|
|
using MyCore.Service.Services;
|
|
using MQTTnet;
|
|
using MQTTnet.AspNetCore;
|
|
using MQTTnet.AspNetCore.Extensions;
|
|
using MyCore.Service.Extensions;
|
|
using Mqtt.Client.AspNetCore.Services;
|
|
using Mqtt.Client.AspNetCore.Settings;
|
|
using MyCore.Services.Devices;
|
|
|
|
namespace MyCore
|
|
{
|
|
public class Startup
|
|
{
|
|
public Startup(IConfiguration configuration)
|
|
{
|
|
Configuration = configuration;
|
|
|
|
MapConfiguration();
|
|
}
|
|
|
|
public IConfiguration Configuration { get; }
|
|
|
|
private void MapConfiguration()
|
|
{
|
|
MapBrokerHostSettings();
|
|
MapClientSettings();
|
|
}
|
|
|
|
private void MapBrokerHostSettings()
|
|
{
|
|
BrokerHostSettings brokerHostSettings = new BrokerHostSettings();
|
|
Configuration.GetSection(nameof(BrokerHostSettings)).Bind(brokerHostSettings);
|
|
AppSettingsProvider.BrokerHostSettings = brokerHostSettings;
|
|
|
|
/*BrokerHostSettings brokerOnlineHostSettings = new BrokerHostSettings();
|
|
Configuration.GetSection(nameof(BrokerHostSettings)).Bind(brokerOnlineHostSettings);
|
|
AppSettingsOnlineProvider.BrokerHostOnlineSettings = brokerOnlineHostSettings;*/
|
|
}
|
|
|
|
private void MapClientSettings()
|
|
{
|
|
ClientSettings clientSettings = new ClientSettings();
|
|
Configuration.GetSection(nameof(ClientSettings)).Bind(clientSettings);
|
|
AppSettingsProvider.ClientSettings = clientSettings;
|
|
|
|
/*ClientSettings clientOnlineSettings = new ClientSettings();
|
|
Configuration.GetSection(nameof(ClientSettings)).Bind(clientOnlineSettings);
|
|
AppSettingsOnlineProvider.ClientOnlineSettings = clientOnlineSettings;*/
|
|
}
|
|
|
|
// This method gets called by the runtime. Use this method to add services to the container.
|
|
public void ConfigureServices(IServiceCollection services)
|
|
{
|
|
|
|
// Swagger
|
|
services.AddControllers()
|
|
.AddJsonOptions(options =>
|
|
{
|
|
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
|
|
});
|
|
|
|
services.AddOpenApiDocument(config =>
|
|
{
|
|
ConfigureSwagger(config);
|
|
});
|
|
|
|
services.AddCors(o => o.AddPolicy("AllowAll", builder =>
|
|
{
|
|
builder.AllowAnyOrigin()
|
|
.AllowAnyMethod()
|
|
.AllowAnyHeader();
|
|
}));
|
|
|
|
// Authentication
|
|
|
|
var tokensConfiguration = Configuration.GetSection("Tokens");
|
|
var tokenSettings = tokensConfiguration.Get<TokensSettings>();
|
|
|
|
services.Configure<TokensSettings>(tokensConfiguration);
|
|
|
|
foreach (var policy in Security.PoliciesConfiguration)
|
|
services.AddAuthorization(options =>
|
|
{
|
|
options.AddPolicy(policy.Name, policyAdmin =>
|
|
{
|
|
foreach (var claim in policy.Claims)
|
|
policyAdmin.RequireClaim(Security.ClaimTypes.Permission, claim);
|
|
});
|
|
});
|
|
|
|
services
|
|
.AddAuthentication(x =>
|
|
{
|
|
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
x.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
})
|
|
.AddJwtBearer(x =>
|
|
{
|
|
x.RequireHttpsMetadata = false;
|
|
x.SaveToken = true;
|
|
x.TokenValidationParameters = new TokenValidationParameters
|
|
{
|
|
ValidateIssuerSigningKey = true,
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(tokenSettings.Secret)),
|
|
ValidateIssuer = false,
|
|
ValidateAudience = false,
|
|
RequireExpirationTime = false,
|
|
ValidateLifetime = true
|
|
};
|
|
});
|
|
|
|
|
|
//services.AddMqttClientOnlineHostedService();
|
|
|
|
// Comment this line when dev
|
|
//services.AddMerossClientHostedService(); // Todo client files (a lot are useless)
|
|
|
|
services.AddScoped<YeelightService>(); // To clarify if needed.. ?
|
|
|
|
services.AddScoped<TokensService>();
|
|
services.AddScoped(typeof(ProfileLogic));
|
|
|
|
services.AddScoped<BookService>();
|
|
services.AddScoped<IoTDeviceService>();
|
|
services.AddScoped<UserDatabaseService>();
|
|
services.AddScoped<HomeDatabaseService>();
|
|
services.AddScoped<ProviderDatabaseService>();
|
|
services.AddScoped<DeviceDatabaseService>();
|
|
services.AddScoped<GroupDatabaseService>();
|
|
services.AddScoped<RoomDatabaseService>();
|
|
services.AddScoped<AutomationDatabaseService>();
|
|
services.AddScoped<ActionService>();
|
|
services.AddScoped<AlarmDatabaseService>();
|
|
services.AddScoped<EventDatabaseService>();
|
|
|
|
services.AddMqttClientHostedService(); // Todo client files (a lot are useless)
|
|
}
|
|
|
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
|
{
|
|
|
|
/*app.UseCors(
|
|
options => options.WithOrigins("http://localhost:4200").AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().AllowCredentials()
|
|
);*/
|
|
|
|
if (env.IsDevelopment())
|
|
{
|
|
//app.UseDeveloperExceptionPage();
|
|
app.UseExceptionHandler(HandleError);
|
|
}
|
|
|
|
//app.UseHttpsRedirection(); // to tessst
|
|
|
|
app.UseRouting();
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
|
|
app.UseEndpoints(endpoints =>
|
|
{
|
|
endpoints.MapControllers();
|
|
});
|
|
|
|
app.UseOpenApi();
|
|
app.UseSwaggerUi3(configure =>
|
|
{
|
|
configure.OperationsSorter = "alpha";
|
|
configure.TagsSorter = "alpha";
|
|
});
|
|
}
|
|
|
|
private void ConfigureSwagger(AspNetCoreOpenApiDocumentGeneratorSettings config)
|
|
{
|
|
config.GenerateEnumMappingDescription = true;
|
|
config.AddSecurity("bearer", Enumerable.Empty<string>(), new OpenApiSecurityScheme
|
|
{
|
|
Type = OpenApiSecuritySchemeType.OAuth2,
|
|
Description = "MyCore Authentication",
|
|
Flow = OpenApiOAuth2Flow.Password,
|
|
Flows = new OpenApiOAuthFlows()
|
|
{
|
|
|
|
Password = new OpenApiOAuthFlow()
|
|
{
|
|
Scopes = new Dictionary<string, string>
|
|
{
|
|
{Security.Scope, "MyCore WebAPI"}
|
|
},
|
|
TokenUrl = "/api/authentication/Token",
|
|
AuthorizationUrl = "/authentication/Token",
|
|
}
|
|
}
|
|
});
|
|
config.OperationProcessors.Add(new AspNetCoreOperationSecurityScopeProcessor("bearer"));
|
|
|
|
config.PostProcess = document =>
|
|
{
|
|
document.Info.Title = "MyCore Service";
|
|
document.Info.Description = "API description";
|
|
document.Info.Version = "Version Pre-Alpha";
|
|
};
|
|
}
|
|
|
|
|
|
private void HandleError(IApplicationBuilder error)
|
|
{
|
|
error.Run(async context =>
|
|
{
|
|
var exceptionHandlerPathFeature = context.Features.Get<IExceptionHandlerPathFeature>();
|
|
var exception = exceptionHandlerPathFeature?.Error as RequestException;
|
|
|
|
if (exception != null)
|
|
{
|
|
var json = exception.GetJson();
|
|
context.Response.ContentType = "application/json";
|
|
context.Response.StatusCode = exception.StatusCode;
|
|
await context.Response.WriteAsync(json);
|
|
}
|
|
});
|
|
}
|
|
|
|
|
|
}
|
|
}
|