-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
171 lines (142 loc) · 4.76 KB
/
Program.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using Microsoft.AspNetCore.Mvc.Versioning;
using Microsoft.EntityFrameworkCore;
using OfficeOpenXml;
using WorldCitiesAPI.Data;
using Serilog;
using Serilog.Events;
using Serilog.Sinks.MSSqlServer;
using WorldCitiesAPI.Data.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using Microsoft.AspNetCore.Cors;
using WorldCitiesAPI.Data.GraphQL;
using HotChocolate;
using HotChocolate.AspNetCore.Serialization;
var builder = WebApplication.CreateBuilder(args);
// Add support for Serilog
builder.Host.UseSerilog((ctx, lc) => lc
.ReadFrom.Configuration(ctx.Configuration)
.WriteTo.MSSqlServer(connectionString: ctx.Configuration.GetConnectionString("DefaultConnection"),
restrictedToMinimumLevel: LogEventLevel.Information,
sinkOptions: new MSSqlServerSinkOptions
{
TableName = "LogEvent",
AutoCreateSqlTable = true
})
.WriteTo.Console()
);
// Add services to the container.
builder.Services.AddApiVersioning(o =>
{
o.AssumeDefaultVersionWhenUnspecified = true;
o.DefaultApiVersion = new Microsoft.AspNetCore.Mvc.ApiVersion(1, 0);
o.ReportApiVersions = true;
o.ApiVersionReader = ApiVersionReader.Combine(
new QueryStringApiVersionReader("api-version"),
new HeaderApiVersionReader("X-Version"),
new MediaTypeApiVersionReader("ver"));
});
//builder.Services.AddVersionedApiExplorer(
// options =>
// {
// options.GroupNameFormat = "'v'VVV";
// options.SubstituteApiVersionInUrl = true;
// });
builder.Services.AddMvc();
builder.Services.AddControllers()
.AddJsonOptions(options =>
{
//options.JsonSerializerOptions.WriteIndented = true;
//options.JsonSerializerOptions.PropertyNamingPolicy = null;
});
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
//services cors
var corsapp = "AngularPolicy";
builder.Services.AddCors(options => options.AddPolicy(corsapp, cfg =>
{
cfg.AllowAnyMethod().AllowAnyHeader();
cfg.WithOrigins(builder.Configuration["AllowedCORS"]);
}));
// Add ApplicationDbContext and SQL Server support
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
builder.Configuration.GetConnectionString("DefaultConnection")));
// Add ASP.NET Core Identity support
builder.Services.AddIdentity<ApplicationUser, IdentityRole>(
options =>
{
options.SignIn.RequireConfirmedAccount = true;
options.Password.RequireDigit = true;
options.Password.RequireLowercase = true;
options.Password.RequireUppercase = true;
options.Password.RequireNonAlphanumeric = true;
options.Password.RequiredLength = 6;
}
).AddEntityFrameworkStores<ApplicationDbContext>();
builder.Services.AddScoped<JwtHandler>();
// Add support for GraphQL
builder.Services.AddGraphQLServer()
.AddAuthorization()
.AddQueryType<Query>()
.AddMutationType<Mutation>()
// .AddSubscriptionType<Object>()
.AddFiltering().
AddSorting();
// Add Authentication services & middlewares
builder.Services.AddAuthentication(opt =>
{
opt.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
opt.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
RequireExpirationTime = true,
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = builder.Configuration["JwtSettings:Issuer"],
ValidAudience = builder.Configuration["JwtSettings:Audience"],
IssuerSigningKey = new SymmetricSecurityKey(System.Text.Encoding.UTF8.
GetBytes(builder.Configuration["JwtSettings:SecurityKey"]))
};
});
var app = builder.Build();
// Log Http Request with Serilog
app.UseSerilogRequestLogging();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
//app.UseMvc();
app.UseRouting();
app.UseHttpsRedirection();
app.UseCors(corsapp);
app.UseAuthentication();
app.UseAuthorization();
//app.UseEndpoints(endpoints =>
//{
// endpoints.MapControllerRoute(
// name: "default",
// pattern: "{controller=Logs}/{action=Index}/{id?}");
//});
app.MapControllers();
app.MapGraphQL("/api/graphql");
// app.MapGraphQL();
// app.MapGraphQL("/api/graphql");
//app.UseRouting()
// .UseEndpoints(endpoints =>
// {
// endpoints.MapGraphQL();
// });
// Install-Package EPPlus
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
app.MapMethods("/api/heartbeat", new[] { "HEAD" },
() => Results.Ok());
app.Run();