|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Reflection; |
| 6 | +using System.Reflection.Emit; |
| 7 | +using System.Reflection.Metadata; |
| 8 | +using System.Reflection.PortableExecutable; |
| 9 | +using System.Runtime.Serialization; |
| 10 | +using System.Runtime.Serialization.Formatters.Binary; |
| 11 | +using System.Security.Cryptography.X509Certificates; |
| 12 | +using System.Threading.Tasks; |
| 13 | +using Microsoft.AspNetCore.Builder; |
| 14 | +using Microsoft.AspNetCore.Hosting; |
| 15 | +using Microsoft.AspNetCore.Http; |
| 16 | +using Microsoft.EntityFrameworkCore; |
| 17 | +using Microsoft.EntityFrameworkCore.Metadata.Internal; |
| 18 | +using Microsoft.Extensions.Configuration; |
| 19 | +using Microsoft.Extensions.DependencyInjection; |
| 20 | +using Microsoft.Extensions.Hosting; |
| 21 | +using Microsoft.Extensions.Logging; |
| 22 | +using Serilog; |
| 23 | +using Wissance.WeatherControl.Data; |
| 24 | +using Wissance.WeatherControl.Data.Extensions; |
| 25 | +using Wissance.WeatherControl.Common.Config; |
| 26 | +using Wissance.WeatherControl.Data.Entity; |
| 27 | +using Wissance.WebApiToolkit.Core.Data; |
| 28 | +using Wissance.WebApiToolkit.Core.Managers; |
| 29 | +using Wissance.WebApiToolkit.Ef.Factories; |
| 30 | +using Wissance.WebApiToolkit.Ef.Managers; |
| 31 | +using Wissance.WebApiToolkit.Ef.Extensions; |
| 32 | +using Wissance.WebApiToolkit.Ef.Generators; |
| 33 | + |
| 34 | +namespace Wissance.WeatherControl.SemiAuto.WebApi |
| 35 | +{ |
| 36 | + public class Startup |
| 37 | + { |
| 38 | + public Startup(IConfiguration configuration, IWebHostEnvironment env) |
| 39 | + { |
| 40 | + Configuration = configuration; |
| 41 | + Environment = env; |
| 42 | + Settings = configuration.GetSection("Application").Get<ApplicationSettings>(); |
| 43 | + } |
| 44 | + |
| 45 | + // This method gets called by the runtime. Use this method to add services to the container. |
| 46 | + // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 |
| 47 | + public void ConfigureServices(IServiceCollection services) |
| 48 | + { |
| 49 | + ConfigureLogging(services); |
| 50 | + ConfigureDatabase(services); |
| 51 | + ConfigureWebApi(services); |
| 52 | + } |
| 53 | + |
| 54 | + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. |
| 55 | + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) |
| 56 | + { |
| 57 | + if (env.IsDevelopment()) |
| 58 | + { |
| 59 | + app.UseDeveloperExceptionPage(); |
| 60 | + } |
| 61 | + |
| 62 | + app.UseSwagger(); |
| 63 | + app.UseSwaggerUI(c => |
| 64 | + { |
| 65 | + c.SwaggerEndpoint("/swagger/v1/swagger.json", AppName); |
| 66 | + }); |
| 67 | + |
| 68 | + app.UseRouting(); |
| 69 | + |
| 70 | + app.UseEndpoints(endpoints => |
| 71 | + { |
| 72 | + endpoints.MapControllers(); |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + private void ConfigureLogging(IServiceCollection services) |
| 77 | + { |
| 78 | + Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(Configuration).CreateLogger(); |
| 79 | + services.AddLogging(loggingBuilder => loggingBuilder.AddConfiguration(Configuration).AddConsole()); |
| 80 | + services.AddLogging(loggingBuilder => loggingBuilder.AddConfiguration(Configuration).AddDebug()); |
| 81 | + services.AddLogging(loggingBuilder => loggingBuilder.AddConfiguration(Configuration).AddSerilog(dispose: true)); |
| 82 | + } |
| 83 | + |
| 84 | + private void ConfigureDatabase(IServiceCollection services) |
| 85 | + { |
| 86 | + services.ConfigureSqlServerDbContext<ModelContext>(Settings.Database.ConnStr); |
| 87 | + IServiceProvider serviceProvider = services.BuildServiceProvider(); |
| 88 | + ModelContext modelContext = serviceProvider.GetRequiredService<ModelContext>(); |
| 89 | + modelContext.Database.Migrate(); |
| 90 | + } |
| 91 | + |
| 92 | + private void ConfigureWebApi(IServiceCollection services) |
| 93 | + { |
| 94 | + services.AddSwaggerGen(); |
| 95 | + ServiceProvider provider = services.BuildServiceProvider(); |
| 96 | + Assembly stationControllerAssembly = services.AddSimplifiedAutoController<StationEntity, Guid, EmptyAdditionalFilters>( |
| 97 | + provider.GetRequiredService<ModelContext>(), "Station", |
| 98 | + ControllerType.FullCrud, null, provider.GetRequiredService<ILoggerFactory>()); |
| 99 | + Assembly measureUnitControllerAssembly = services.AddSimplifiedAutoController<MeasureUnitEntity, Guid, EmptyAdditionalFilters>( |
| 100 | + provider.GetRequiredService<ModelContext>(), "MeasureUnit", |
| 101 | + ControllerType.ReadOnly, null, provider.GetRequiredService<ILoggerFactory>()); |
| 102 | + Assembly sensorControllerAssembly = services.AddSimplifiedAutoController<SensorEntity, Guid, EmptyAdditionalFilters>( |
| 103 | + provider.GetRequiredService<ModelContext>(), "Sensor", |
| 104 | + ControllerType.FullCrud, null, provider.GetRequiredService<ILoggerFactory>()); |
| 105 | + Assembly measurementControllerAssembly = services.AddSimplifiedAutoController<MeasurementEntity, Guid, EmptyAdditionalFilters>( |
| 106 | + provider.GetRequiredService<ModelContext>(), "Measurement", |
| 107 | + ControllerType.Bulk, null, provider.GetRequiredService<ILoggerFactory>()); |
| 108 | + |
| 109 | + services.AddControllers().AddApplicationPart(sensorControllerAssembly).AddControllersAsServices(); |
| 110 | + services.AddControllers().AddApplicationPart(stationControllerAssembly).AddControllersAsServices(); |
| 111 | + services.AddControllers().AddApplicationPart(measureUnitControllerAssembly).AddControllersAsServices(); |
| 112 | + services.AddControllers().AddApplicationPart(measurementControllerAssembly).AddControllersAsServices(); |
| 113 | + } |
| 114 | + |
| 115 | + public ApplicationSettings Settings { get; set; } |
| 116 | + private IConfiguration Configuration { get; } |
| 117 | + public IWebHostEnvironment Environment { get; } |
| 118 | + |
| 119 | + private const string AppName = "Wissance.WeatherControl"; |
| 120 | + } |
| 121 | +} |
0 commit comments