-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
102 lines (84 loc) · 2.78 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
using System.Text.Json.Serialization;
using FluentValidation;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.EntityFrameworkCore;
using TaskManagement.Features.Files.Endpoints;
using TaskManagement.Features.Files.Filters;
using TaskManagement.Features.Tags.Endpoints;
using TaskManagement.Features.Tags.Models;
using TaskManagement.Features.Tags.Validations;
using TaskManagement.Features.Tasks.Endpoints;
using TaskManagement.Features.Tasks.Models;
using TaskManagement.Features.Tasks.Validations;
using TaskManagement.Persistences;
using TaskManagement.Persistences.Extensions;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.OperationFilter<FileUploadOperationFilter>();
});
builder.Services.AddDbContext<AppDbContext>(option =>
{
var connection = builder.Configuration.GetConnectionString("DbConnect");
option.UseSqlServer(connection);
});
builder.Services.ConfigureHttpJsonOptions(option =>
{
option.SerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
});
builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
builder.Services.AddScoped<IValidator<CreateTaskRequest>, CreateTaskValidator>();
builder.Services.AddScoped<IValidator<UpdateTaskRequest>, UpdateTaskValidator>();
builder.Services.AddScoped<IValidator<GetTasksRequest>, GetTaskValidator>();
builder.Services.AddScoped<IValidator<GetTagsRequest>, GetTagsValidator>();
builder.Services.AddProblemDetails();
builder.Services.AddAuthentication();
builder.Services.AddAuthorization();
builder.Services.AddAntiforgery();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseAuthentication();
app.UseAuthorization();
app.UseAntiforgery();
app.UseExceptionHandler(appError =>
{
appError.Run(async context =>
{
context.Response.StatusCode = 500;
context.Response.ContentType = "application/json";
var contextFeature = context.Features.Get<IExceptionHandlerFeature>();
if (contextFeature is not null)
{
await context.Response.WriteAsJsonAsync(new
{
context.Response.StatusCode,
contextFeature.Error.Message,
});
}
});
});
// Task endpoints
app.MapGetTaskList();
app.MapGetTaskDetail();
app.MapSearchTask();
app.MapCreateTask();
app.MapUpdateTask();
app.MapDeleteTask();
// Tags endpoints
app.MapGetTagList();
app.MapGetTagDetail();
app.MapDeleteTag();
// Upload file
app.MapUploadFile();
await app.Init();
app.Run();