1
- using System ;
2
- using System . Collections . Generic ;
3
- using System . IO ;
4
- using System . Linq ;
5
- using System . Threading . Tasks ;
6
- using Microsoft . AspNetCore ;
1
+ using Google . Protobuf . WellKnownTypes ;
2
+ using Hangfire ;
7
3
using Microsoft . AspNetCore . Builder ;
8
- using Microsoft . AspNetCore . Hosting ;
9
- using Microsoft . AspNetCore . Http ;
10
- using Microsoft . Extensions . DependencyInjection ;
11
- using Microsoft . Extensions . Logging ;
4
+ using Microsoft . AspNetCore . Identity ;
12
5
using Microsoft . Extensions . Configuration ;
6
+ using Microsoft . Extensions . Hosting ;
7
+ using QueryTree . Managers ;
13
8
using QueryTree . Models ;
9
+ using QueryTree . Services ;
10
+ using QueryTree ;
11
+ using System ;
12
+ using System . Configuration ;
13
+ using Hangfire . SQLite ;
14
+ using Microsoft . AspNetCore . Http ;
15
+ using Microsoft . Extensions . DependencyInjection ;
14
16
using Microsoft . EntityFrameworkCore ;
15
17
16
- namespace QueryTree
18
+ var builder = WebApplication . CreateBuilder ( args ) ;
19
+ builder . Services . AddSingleton < IConfiguration > ( builder . Configuration ) ;
20
+
21
+ builder . Services . Configure < CustomizationConfiguration > ( builder . Configuration . GetSection ( "Customization" ) ) ;
22
+ builder . Services . Configure < PasswordsConfiguration > ( builder . Configuration . GetSection ( "Passwords" ) ) ;
23
+
24
+ switch ( builder . Configuration . GetValue < QueryTree . Enums . DataStoreType > ( "Customization:DataStore" ) )
17
25
{
18
- public class Program
19
- {
20
- public static void Main ( string [ ] args )
21
- {
22
- var host = GetBuilder ( args , true )
23
- . Build ( ) ;
24
-
25
- using ( var scope = host . Services . CreateScope ( ) )
26
- {
27
- var context = scope . ServiceProvider . GetService < ApplicationDbContext > ( ) ;
28
- context . Database . Migrate ( ) ;
29
- }
30
-
31
- host . Run ( ) ;
32
- }
33
-
34
- public static IWebHost BuildWebHost ( string [ ] args )
35
- {
36
- return GetBuilder ( args , false ) . Build ( ) ;
37
- }
38
-
39
- public static IWebHostBuilder GetBuilder ( string [ ] args , bool runHangfire )
40
- {
41
- var builder = WebHost . CreateDefaultBuilder ( args )
42
- . UseStartup < Startup > ( ) ;
43
-
44
- if ( runHangfire )
45
- {
46
- builder = builder . UseSetting ( "RunHangfire" , "true" ) ;
47
- }
48
- else
49
- {
50
- builder = builder . UseSetting ( "RunHangfire" , "false" ) ;
51
- }
52
-
53
- return builder . ConfigureAppConfiguration ( ( hostContext , config ) =>
26
+ case QueryTree . Enums . DataStoreType . MSSqlServer :
27
+ builder . Services . AddDbContext < ApplicationDbContext > ( options =>
28
+ options . UseSqlServer ( builder . Configuration . GetConnectionString ( "DefaultConnection" ) ) ) ;
29
+ builder . Services . AddHangfire ( x =>
30
+ x . UseSqlServerStorage ( builder . Configuration . GetConnectionString ( "DefaultConnection" ) )
31
+ ) ;
32
+ break ;
33
+
34
+ default :
35
+ builder . Services . AddDbContext < ApplicationDbContext > ( options =>
36
+ options . UseSqlite ( builder . Configuration . GetConnectionString ( "DefaultConnection" ) ) ) ;
37
+ builder . Services . AddHangfire ( x =>
38
+ x . UseSQLiteStorage ( builder . Configuration . GetConnectionString ( "DefaultConnection" ) )
39
+ ) ;
40
+ break ;
41
+ }
42
+
43
+ builder . Services . AddIdentity < ApplicationUser , IdentityRole > ( )
44
+ . AddEntityFrameworkStores < ApplicationDbContext > ( )
45
+ . AddDefaultTokenProviders ( ) ;
46
+
47
+ builder . Services . AddAuthentication ( )
48
+ . AddCookie ( options =>
54
49
{
55
- if ( hostContext . HostingEnvironment . IsDevelopment ( ) )
56
- {
57
- config . AddJsonFile ( $ "usersettings.json" , optional : true ) ;
58
- }
50
+ // Cookie settings
51
+ options . ExpireTimeSpan = TimeSpan . FromDays ( 150 ) ;
52
+ options . LoginPath = "/Account/LogIn" ;
53
+ options . LogoutPath = "/Account/LogOut" ;
59
54
} ) ;
60
- }
61
- }
62
- }
55
+
56
+ builder . Services . Configure < IdentityOptions > ( options =>
57
+ {
58
+ // Password settings
59
+ options . Password . RequiredLength = 8 ;
60
+
61
+ // Lockout settings
62
+ options . Lockout . DefaultLockoutTimeSpan = TimeSpan . FromMinutes ( 30 ) ;
63
+ options . Lockout . MaxFailedAccessAttempts = 10 ;
64
+
65
+ // User settings
66
+ options . User . RequireUniqueEmail = true ;
67
+ } ) ;
68
+ // Add services to the container.
69
+ builder . Services . AddControllersWithViews ( ) ;
70
+
71
+ // Add application services.
72
+ builder . Services . AddTransient < IEmailSenderService , EmailSenderService > ( ) ;
73
+ builder . Services . AddTransient < IEmailSender , EmailSender > ( ) ;
74
+ builder . Services . AddTransient < IPasswordManager , PasswordManager > ( ) ; // Allows controllers to set/get/delete database credentials
75
+ builder . Services . AddTransient < IScheduledEmailManager , ScheduledEmailManager > ( ) ;
76
+ builder . Services . AddMemoryCache ( ) ;
77
+ var app = builder . Build ( ) ;
78
+
79
+ using ( var scope = app . Services . CreateScope ( ) )
80
+ {
81
+ var context = scope . ServiceProvider . GetService < ApplicationDbContext > ( ) ;
82
+ context . Database . Migrate ( ) ;
83
+ }
84
+ // Configure the HTTP request pipeline.
85
+ if ( ! app . Environment . IsDevelopment ( ) )
86
+ {
87
+ app . UseExceptionHandler ( "/Home/Error" ) ;
88
+ // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
89
+ app . UseHsts ( ) ;
90
+ }
91
+
92
+ app . UseHttpsRedirection ( ) ;
93
+ app . UseStaticFiles ( ) ;
94
+
95
+ app . UseRouting ( ) ;
96
+ app . UseAuthentication ( ) ;
97
+ app . UseAuthorization ( ) ;
98
+ if ( builder . Configuration [ "RunHangfire" ] == "true" )
99
+ {
100
+ app . UseHangfireServer ( ) ;
101
+
102
+ var dashboardOptions = new DashboardOptions
103
+ {
104
+ Authorization = new [ ] { new HangfireAuthorizationFilter ( ) }
105
+ } ;
106
+ app . UseHangfireDashboard ( "/hangfire" , dashboardOptions ) ;
107
+ }
108
+ app . MapControllerRoute (
109
+ name : "default" ,
110
+ pattern : "{controller=Home}/{action=Index}/{id?}" ) ;
111
+ if ( ! String . IsNullOrWhiteSpace ( builder . Configuration . GetValue < string > ( "Customization:BaseUri" ) ) )
112
+ {
113
+ app . Use ( ( context , next ) => {
114
+ context . Request . PathBase = new PathString ( builder . Configuration . GetValue < string > ( "Customization:BaseUri" ) ) ;
115
+ return next ( ) ;
116
+ } ) ;
117
+ }
118
+ app . Run ( ) ;
0 commit comments