-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
131 lines (114 loc) · 3.7 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
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using YourNamespace.Services;
using Microsoft.OpenApi.Models;
using System.Threading;
using System.Net;
using Microsoft.AspNetCore.Cors.Infrastructure;
using System;
using System.IO;
using Swashbuckle.AspNetCore.SwaggerGen;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
ThreadPool.SetMinThreads(100, 100);
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
Args = args,
WebRootPath = "wwwroot",
ContentRootPath = Directory.GetCurrentDirectory()
});
builder.Services.Configure<HostOptions>(options =>
{
options.ShutdownTimeout = TimeSpan.FromSeconds(30);
});
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAllOrigins",
builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
});
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Data Analysis API", Version = "v1" });
c.OperationFilter<FileUploadOperation>();
});
builder.Services.AddScoped<IDataAnalysisService, DataAnalysisService>();
var app = builder.Build();
app.UseCors("AllowAllOrigins");
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Data Analysis API v1");
c.RoutePrefix = string.Empty;
});
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllers();
app.Run();
public class FileUploadOperation : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
var fileUploadAttributes = context.MethodInfo.GetCustomAttributes(typeof(HttpPostAttribute), false).Any();
if (fileUploadAttributes)
{
operation.Parameters.Clear();
operation.RequestBody = new OpenApiRequestBody
{
Content =
{
["multipart/form-data"] = new OpenApiMediaType
{
Schema = new OpenApiSchema
{
Type = "object",
Properties =
{
["File1"] = new OpenApiSchema
{
Type = "string",
Format = "binary"
},
["File2"] = new OpenApiSchema
{
Type = "string",
Format = "binary"
},
["File1FieldsJson"] = new OpenApiSchema
{
Type = "string"
},
["File2FieldsJson"] = new OpenApiSchema
{
Type = "string"
},
["ConfigurationJson"] = new OpenApiSchema
{
Type = "string"
}
},
Required = new HashSet<string> { "File1", "File2" }
}
}
}
};
}
}
}