11using System ;
2+ using System . Collections . Generic ;
23using Autofac ;
34using Microsoft . AspNetCore . Builder ;
45using Microsoft . AspNetCore . Hosting ;
@@ -69,8 +70,8 @@ namespace Resgrid.Web.ServicesCore
6970{
7071 public class Startup
7172 {
72- //public IConfiguration Configuration { get; }
73- public IConfigurationRoot Configuration { get ; private set ; }
73+ //public IConfiguration Configuration { get; }
74+ public IConfigurationRoot Configuration { get ; private set ; }
7475 public ILifetimeScope AutofacContainer { get ; private set ; }
7576 public AutofacServiceLocator Locator { get ; private set ; }
7677 public IServiceCollection Services { get ; private set ; }
@@ -233,7 +234,7 @@ public void ConfigureServices(IServiceCollection services)
233234 Title = "Resgrid API" ,
234235 Version = "v4" ,
235236 Description = "The Resgrid Computer Aided Dispatch (CAD) API reference. Documentation: https://resgrid-core.readthedocs.io/en/latest/api/index.html" ,
236- Contact = new OpenApiContact ( ) { Email = "[email protected] " , Name = "Resgrid Team" , Url = new Uri ( "https://resgrid.com" ) } , 237+ Contact = new OpenApiContact ( ) { Email = "[email protected] " , Name = "Resgrid Team" , Url = new Uri ( "https://resgrid.com" ) } , 237238 TermsOfService = new Uri ( "https://resgrid.com/Public/Terms" )
238239 }
239240 ) ;
@@ -246,7 +247,8 @@ public void ConfigureServices(IServiceCollection services)
246247 services . AddSignalR ( hubOptions =>
247248 {
248249 hubOptions . EnableDetailedErrors = true ;
249- } ) . AddStackExchangeRedis ( CacheConfig . RedisConnectionString , options => {
250+ } ) . AddStackExchangeRedis ( CacheConfig . RedisConnectionString , options =>
251+ {
250252 options . Configuration . ChannelPrefix = $ "{ Config . SystemBehaviorConfig . GetEnvPrefix ( ) } resgrid-evt-sr";
251253 } ) ;
252254
@@ -397,17 +399,6 @@ public void ConfigureServices(IServiceCollection services)
397399
398400 StripeConfiguration . ApiKey = Config . PaymentProviderConfig . IsTestMode ? PaymentProviderConfig . TestApiKey : PaymentProviderConfig . ProductionApiKey ;
399401
400- services . AddDbContext < AuthorizationDbContext > ( options =>
401- {
402- // Configure the context to use Microsoft SQL Server.
403- options . UseSqlServer ( OidcConfig . ConnectionString ) ;
404-
405- // Register the entity sets needed by OpenIddict.
406- // Note: use the generic overload if you need
407- // to replace the default OpenIddict entities.
408- options . UseOpenIddict < Guid > ( ) ;
409- } ) ;
410-
411402 // Register the Identity services.
412403 //services.AddIdentity<ApplicationUser, IdentityRole>()
413404 // .AddEntityFrameworkStores<ApplicationDbContext>()
@@ -435,15 +426,51 @@ public void ConfigureServices(IServiceCollection services)
435426 //// Register the Quartz.NET service and configure it to block shutdown until jobs are complete.
436427 //services.AddQuartzHostedService(options => options.WaitForJobsToComplete = true);
437428
429+ if ( OidcConfig . DatabaseType == DatabaseTypes . Postgres )
430+ {
431+ services . AddDbContext < AuthorizationDbContext > ( options =>
432+ {
433+ // Configure the context to use Microsoft SQL Server.
434+ options . UseNpgsql (
435+ Config . OidcConfig . ConnectionString ,
436+ opt =>
437+ {
438+ opt . EnableRetryOnFailure (
439+ maxRetryCount : 10 ,
440+ maxRetryDelay : TimeSpan . FromSeconds ( 30 ) ,
441+ errorCodesToAdd : new List < string > ( ) { } ) ;
442+
443+ opt . UseAdminDatabase ( "postgres" ) ;
444+ } ) ; //.UseLowerCaseNamingConvention();
445+
446+ options . UseOpenIddict < Guid > ( ) ;
447+ } ) ;
448+
449+ AppContext . SetSwitch ( "Npgsql.EnableLegacyTimestampBehavior" , true ) ;
450+ }
451+ else
452+ {
453+ services . AddDbContext < AuthorizationDbContext > ( options =>
454+ {
455+ // Configure the context to use Microsoft SQL Server.
456+ options . UseSqlServer ( OidcConfig . ConnectionString ) ;
457+
458+ // Register the entity sets needed by OpenIddict.
459+ // Note: use the generic overload if you need
460+ // to replace the default OpenIddict entities.
461+ options . UseOpenIddict < Guid > ( ) ;
462+ } ) ;
463+ }
464+
438465 services . AddOpenIddict ( )
439466 // Register the OpenIddict core components.
440467 . AddCore ( options =>
441468 {
442469 // Configure OpenIddict to use the Entity Framework Core stores and models.
443470 // Note: call ReplaceDefaultEntities() to replace the default OpenIddict entities.
444471 options . UseEntityFrameworkCore ( )
445- . UseDbContext < AuthorizationDbContext > ( )
446- . ReplaceDefaultEntities < Guid > ( ) ;
472+ . UseDbContext < AuthorizationDbContext > ( )
473+ . ReplaceDefaultEntities < Guid > ( ) ;
447474
448475 // Enable Quartz.NET integration.
449476 //options.UseQuartz();
@@ -469,7 +496,7 @@ public void ConfigureServices(IServiceCollection services)
469496 //options.AllowPasswordFlow()
470497 // .AllowRefreshTokenFlow();
471498 options //.AllowAuthorizationCodeFlow()
472- //.AllowHybridFlow()
499+ //.AllowHybridFlow()
473500 . AllowClientCredentialsFlow ( )
474501 . AllowPasswordFlow ( )
475502 . AllowRefreshTokenFlow ( ) ;
@@ -615,7 +642,8 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
615642 this . Locator = new AutofacServiceLocator ( this . AutofacContainer ) ;
616643 ServiceLocator . SetLocatorProvider ( ( ) => this . Locator ) ;
617644
618- app . Use ( async ( context , next ) => {
645+ app . Use ( async ( context , next ) =>
646+ {
619647 context . Response . Headers . Add ( "Strict-Transport-Security" , "max-age=31536000" ) ;
620648 context . Request . Scheme = "https" ;
621649 await next . Invoke ( ) ;
0 commit comments