1 | using backend.Data;
|
---|
2 | using backend.Email;
|
---|
3 | using backend.Helpers;
|
---|
4 | using backend.Jobs;
|
---|
5 | using backend.Services;
|
---|
6 | using Microsoft.EntityFrameworkCore;
|
---|
7 | using Microsoft.OpenApi.Models;
|
---|
8 | using Quartz;
|
---|
9 | using WebApi.Helpers;
|
---|
10 |
|
---|
11 | var builder = WebApplication.CreateBuilder(args);
|
---|
12 |
|
---|
13 | // Add services to the container.
|
---|
14 | builder.Services.Configure<AppSettings>(builder.Configuration.GetSection("AppSettings"));
|
---|
15 | builder.Services.AddControllers().AddNewtonsoftJson();
|
---|
16 | // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
---|
17 | builder.Services.AddEndpointsApiExplorer();
|
---|
18 | builder.Services.AddSwaggerGen(c =>
|
---|
19 | {
|
---|
20 | c.SwaggerDoc("v1", new OpenApiInfo() { Title = "resTools backend", Version = "v1" });
|
---|
21 | c.AddSecurityDefinition("Bearer",
|
---|
22 | new OpenApiSecurityScheme
|
---|
23 | {
|
---|
24 | In = Microsoft.OpenApi.Models.ParameterLocation.Header,
|
---|
25 | Description = "Please enter into field the word 'Bearer' following by space and JWT",
|
---|
26 | Name = "Authorization",
|
---|
27 | Type = Microsoft.OpenApi.Models.SecuritySchemeType.ApiKey,
|
---|
28 | Scheme="Bearer"
|
---|
29 | });
|
---|
30 | c.AddSecurityRequirement(new OpenApiSecurityRequirement()
|
---|
31 | {
|
---|
32 | {
|
---|
33 | new OpenApiSecurityScheme
|
---|
34 | {
|
---|
35 | Reference = new OpenApiReference
|
---|
36 | {
|
---|
37 | Type = ReferenceType.SecurityScheme,
|
---|
38 | Id = "Bearer"
|
---|
39 | },
|
---|
40 | Name = "Bearer",
|
---|
41 | In = ParameterLocation.Header,
|
---|
42 |
|
---|
43 | },
|
---|
44 | new List<string>()
|
---|
45 | }
|
---|
46 | });
|
---|
47 | });
|
---|
48 | builder.Services.AddScoped<IUserService, UserService>();
|
---|
49 | builder.Services.AddScoped<IRestaurantService, RestaurantService>();
|
---|
50 | builder.Services.AddScoped<IReservationService, ReservationService>();
|
---|
51 | builder.Services.AddScoped<IMenuService, MenuService>();
|
---|
52 | builder.Services.AddScoped<IReviewService, ReviewService>();
|
---|
53 | builder.Services.AddScoped<ISmsService, SmsService>();
|
---|
54 | builder.Services.AddScoped<IToDoService, ToDoService>();
|
---|
55 |
|
---|
56 | builder.Services.AddTransient<IEmailSender, EmailSender>();
|
---|
57 |
|
---|
58 | builder.Services.AddDbContext<DataContext>(p => p.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
|
---|
59 |
|
---|
60 | builder.Services.AddQuartz(q =>
|
---|
61 | {
|
---|
62 | q.UseMicrosoftDependencyInjectionScopedJobFactory();
|
---|
63 | var jobKey = new JobKey("QueueJob");
|
---|
64 | q.AddJob<QueueJob>(opts => opts.WithIdentity(jobKey));
|
---|
65 |
|
---|
66 | q.AddTrigger(opts => opts
|
---|
67 | .ForJob(jobKey)
|
---|
68 | .WithIdentity("QueueJob-trigger")
|
---|
69 | .WithCronSchedule("0 0/1 * * * ?"));
|
---|
70 |
|
---|
71 | });
|
---|
72 |
|
---|
73 | IServiceCollection serviceCollection = builder.Services.AddQuartzHostedService(q => q.WaitForJobsToComplete = true);
|
---|
74 |
|
---|
75 | var app = builder.Build();
|
---|
76 |
|
---|
77 | app.UseCors(x => x
|
---|
78 | .AllowAnyOrigin()
|
---|
79 | .AllowAnyMethod()
|
---|
80 | .AllowAnyHeader());
|
---|
81 |
|
---|
82 | // custom jwt auth middleware
|
---|
83 | app.UseMiddleware<JwtMiddleware>();
|
---|
84 |
|
---|
85 | // Configure the HTTP request pipeline.
|
---|
86 | if (app.Environment.IsDevelopment())
|
---|
87 | {
|
---|
88 | app.UseSwagger();
|
---|
89 | app.UseSwaggerUI();
|
---|
90 | }
|
---|
91 |
|
---|
92 | app.UseHttpsRedirection();
|
---|
93 |
|
---|
94 | app.UseAuthorization();
|
---|
95 |
|
---|
96 | app.MapControllers();
|
---|
97 |
|
---|
98 | app.Run();
|
---|