-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
104 lines (87 loc) · 2.93 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
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using MongoDB.Driver;
var options = new WebApplicationOptions
{
Args = args,
WebRootPath = "wwwroot"
};
var builder = WebApplication.CreateBuilder(options);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddCors(options =>
{
options.AddDefaultPolicy(builder =>
{
builder.WithOrigins("http://localhost:5001")
.AllowAnyHeader()
.AllowAnyMethod();
});
});
builder.Services.Configure<MongoDbSettings>(
builder.Configuration.GetSection("MongoDbSettings"));
builder.Services.Configure<SmtpSettings>(
builder.Configuration.GetSection("SmtpSettings"));
builder.Services.AddSingleton<IMongoClient, MongoClient>(sp =>
new MongoClient(builder.Configuration.GetValue<string>("MongoDbSettings:ConnectionString")));
builder.Services.AddScoped<IItemRepository, ItemRepository>();
builder.Services.AddScoped<IUserRepository, UserRepository>();
builder.Services.AddScoped<IRoleRepository, RoleRepository>();
builder.Services.AddTransient<IEmailService, EmailService>();
builder.Services.AddTransient<UserService>();
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer(); // Needed for .NET 6 and later
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "ChrisAPI", Version = "v1" });
});
var app = builder.Build();
var projectRootPath = app.Environment.ContentRootPath;
var fileProvider = new PhysicalFileProvider(projectRootPath);
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Chris API V1");
});
}
app.UseCors();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = fileProvider,
RequestPath = ""
});
app.UseRouting();
app.UseEndpoints(endpoints =>
endpoints.MapControllers()
);
app.MapGet("/", async context =>
{
var filePath = Path.Combine(app.Environment.ContentRootPath, "views/index.html");
if (!File.Exists(filePath))
{
context.Response.StatusCode = 404;
await context.Response.WriteAsync("File not found");
return;
}
context.Response.ContentType = "text/html";
await context.Response.WriteAsync(File.ReadAllText(filePath));
});
app.MapGet("/admin", async context =>
{
var filePath = Path.Combine(app.Environment.ContentRootPath, "views/admin.html");
if (!File.Exists(filePath))
{
context.Response.StatusCode = 404;
await context.Response.WriteAsync("File not found");
return;
}
context.Response.ContentType = "text/html";
await context.Response.WriteAsync(File.ReadAllText(filePath));
});
app.Run();