-
Notifications
You must be signed in to change notification settings - Fork 0
/
Startup.cs
124 lines (106 loc) · 4.59 KB
/
Startup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
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.EntityFrameworkCore;
using Swashbuckle.AspNetCore;
using Swashbuckle.AspNetCore.Swagger;
using gnv_back.Business;
using gnv_back.Business.Implementations;
using gnv_back.Models.Context;
using gnv_back.Repository;
using gnv_back.Repository.Generic;
using Microsoft.AspNetCore.Rewrite;
using gnv_back.Repository.Implementations;
namespace gnv_back
{
public class Startup
{
private readonly ILogger _logger;
public IConfiguration _configuration { get; }
public IHostingEnvironment _environment { get; }
public Startup(IConfiguration configuration,
IHostingEnvironment environment,
ILogger<Startup> logger
)
{
_configuration = configuration;
_environment = environment;
_logger = logger;
}
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)
{
var configurationString = _configuration["PostgresConnection:PostgresConnectionString"];
services.AddDbContext<PostgresContext>(options => options.UseNpgsql(configurationString));
if (_environment.IsDevelopment()) {
try
{
// var evolveConnection = new MySql.Data.MySqlClient.MySqlConnection(configurationString);
var evolveConnection = new Npgsql.NpgsqlConnection(configurationString);
var evolve = new Evolve.Evolve("evolve.json", evolveConnection, msg => _logger.LogInformation(msg))
{
Locations = new List<string> {"db/migrations"},
IsEraseDisabled = true,
};
evolve.Migrate();
}
catch(Exception ex)
{
_logger.LogCritical("Data base migration failed", ex);
throw;
}
}
services.AddCors(options => options.AddPolicy("AllowAll",
p => p.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()));
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddSwaggerGen(c => {
c.SwaggerDoc("v1",
new Microsoft.OpenApi.Models.OpenApiInfo
{
Title = "API - GNV no APP",
Version = "v1"
});
});
services.AddScoped<IStationBusiness, StationBusinessImpl>();
services.AddScoped<INotificationBusiness, NotificationBusinessImpl>();
services.AddScoped(typeof(IRepository<>), typeof(GenericRepository<>));
services.AddScoped(typeof(IStationRepository), typeof(StationRepository));
// services.AddApiVersioning();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
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.UseCors("AllowAll");
app.UseSwagger();
app.UseSwaggerUI(c => {
c.SwaggerEndpoint("/swagger/v1/swagger.json", "API - GNV no APP");
});
var option = new RewriteOptions();
option.AddRedirect("^$", "swagger");
app.UseRewriter(option);
app.UseHttpsRedirection();
app.UseMvc();
}
}
}