-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
152 lines (111 loc) · 4.06 KB
/
Program.cs
File metadata and controls
152 lines (111 loc) · 4.06 KB
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
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using SmartAgroAPI.Contexts;
using SmartAgroAPI.Interfaces;
using SmartAgroAPI.Mappings;
using SmartAgroAPI.Repositories;
using SmartAgroAPI.Services;
using SmartAgroAPI.Services.EmailService;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(x => x.TokenValidationParameters = new TokenValidationParameters
{
IssuerSigningKey = new SymmetricSecurityKey("SmartAgroSecurityKey"u8.ToArray()),
ValidIssuer = "SmartAgro",
ValidAudience = "SmartAgroAudience"
});
//AutoMapper
//Api Services
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
//Swagger
builder.Services.AddSwaggerGen(options =>
{
//Swagger documentation
options.SwaggerDoc("v1", new OpenApiInfo
{
Version = "v1",
Title = "Smart Agro API",
Description = "An ASP.NET Core Web API for managing the Smart Agro system, which is an application for agriculture sensor data tracking",
Contact = new OpenApiContact
{
Name = "Wender de Castro (API admin)",
Url = new Uri("https://github.com/wenderdecastro")
},
License = new OpenApiLicense
{
Name = "MIT LICENSE",
Url = new Uri("https://github.com/wenderdecastro/SmartAgroAPI/blob/main/LICENSE.txt")
}
}
);
var xmlFile = $"{System.Reflection.Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
options.IncludeXmlComments(xmlPath);
//Jwt Authorization and Authentication for Swagger with Bearer Auth
options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Description = "JWT Authorization Header - set up with Bearer Authentication.\r\n\r\n" +
"Use 'Bearer' [space] <yourtoken> in the field below.\r\n\r\n",
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.ApiKey,
Scheme = "Bearer",
BearerFormat = "JWT",
});
options.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
Array.Empty<string>()
}
});
});
//Dependency Injections
builder.Services.AddSingleton<IEmailService, EmailService>();
builder.Services.AddScoped<EmailSendingService>();
builder.Services.AddHostedService<DataGenerationService>();
builder.Services.AddScoped<IUserRepository, UserRepository>();
builder.Services.AddScoped<INotificationRepository, NotificationRepository>();
builder.Services.AddScoped<ISensorRepository, SensorRepository>();
builder.Services.AddDbContext<SmartAgroDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("SmartAgroDB"))
);
//Configurations
builder.Services.AddAutoMapper(typeof(SensorProfile));
builder.Services.AddAutoMapper(typeof(NotificationProfile));
builder.Services.AddAutoMapper(typeof(UserProfile));
builder.Services.Configure<EmailSettings>(builder.Configuration.GetSection("EmailSettings"));
builder.Services.AddAuthorization(options =>
{
options.AddPolicy("AdminOnly", policy => policy.RequireClaim("IsAdmin", "true"));
});
builder.Environment.IsDevelopment();
//azure
builder.Logging.ClearProviders();
builder.Logging.AddConsole();
var app = builder.Build();
app.UseSwagger();
app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/swagger/v1/swagger.json", "Smart Agro API v1");
options.RoutePrefix = string.Empty;
});
app.UseHttpsRedirection();
//azure
app.UseDeveloperExceptionPage();
//auth
app.UseAuthorization();
app.UseAuthentication();
app.MapControllers();
app.Run();