-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move application and service configuration to Extension classes to cleanup Program.cs #7
Open
tarunganga
wants to merge
3
commits into
DidactHQ:main
Choose a base branch
from
tarunganga:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,117 +1,19 @@ | ||
using DidactEngine.Services.BackgroundServices; | ||
using DidactEngine.Services.Contexts; | ||
using Microsoft.Data.SqlClient; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.Caching.Memory; | ||
using Microsoft.OpenApi.Models; | ||
using System.Reflection; | ||
using DidactCore.Flows; | ||
using DidactEngine.Services.Extensions; | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
// Add services to the container. | ||
builder.Services.AddCors(options => | ||
{ | ||
options.AddPolicy(name: "DevelopmentCORS", | ||
policy => | ||
{ | ||
policy.WithOrigins("http://localhost:8080"); | ||
policy.AllowAnyMethod(); | ||
policy.AllowAnyHeader(); | ||
policy.AllowCredentials(); | ||
}); | ||
}); | ||
|
||
#region Configure DbContext and Gateway. | ||
|
||
var connStringFactory = (string name) => new SqlConnectionStringBuilder( | ||
builder.Configuration.GetConnectionString(name)) | ||
{ | ||
ApplicationName = "Didact", | ||
PersistSecurityInfo = true, | ||
MultipleActiveResultSets = true, | ||
WorkstationID = Environment.MachineName, | ||
TrustServerCertificate = true | ||
}.ConnectionString; | ||
|
||
builder.Services.AddDbContext<DidactDbContext>( | ||
(sp, opt) => | ||
{ | ||
opt.UseMemoryCache(sp.GetRequiredService<IMemoryCache>()); | ||
opt.UseSqlServer(connStringFactory("Didact"), opt => opt.CommandTimeout(110)); | ||
if (builder.Configuration.GetValue<bool?>("EnableSensitiveDataLogging").GetValueOrDefault()) | ||
{ | ||
opt.EnableDetailedErrors(); | ||
opt.EnableSensitiveDataLogging(); | ||
} | ||
}); | ||
|
||
#endregion Configure DbContext and Gateway. | ||
|
||
builder.Services.AddControllers(); | ||
builder.Services.AddMemoryCache(); | ||
|
||
// Register Swagger | ||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle | ||
string swaggerVersion = "v1"; | ||
builder.Services.AddSwaggerGen(options => | ||
{ | ||
options.SwaggerDoc(swaggerVersion, new OpenApiInfo | ||
{ | ||
Version = swaggerVersion, | ||
Title = "Didact REST API", | ||
Description = "The central REST API of the Didact Engine." | ||
}); | ||
|
||
var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; | ||
options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename)); | ||
}); | ||
builder.Services.AddEndpointsApiExplorer(); | ||
|
||
// Register BackgroundServices | ||
builder.Services.AddHostedService<WorkerBackgroundService>(); | ||
// Register Flow helper services from DidactCore. | ||
//builder.Services.AddSingleton<IFlowExecutor, FlowExecutor>(); | ||
// Register repositories from DidactCore. | ||
//builder.Services.AddScoped<IFlowRepository>(); | ||
// Configure Engine | ||
builder.ConfigureCors(); | ||
builder.ConfigureOpenApi(); | ||
builder.ConfigureDatabase(); | ||
builder.ConfigureApplicationServices(); | ||
|
||
// Build Engine | ||
var app = builder.Build(); | ||
|
||
// Configure the HTTP request pipeline. | ||
if (app.Environment.IsDevelopment()) | ||
{ | ||
app.UseCors("DevelopmentCORS"); | ||
} | ||
|
||
app.UseSwagger(); | ||
app.UseSwaggerUI(); | ||
app.UseHttpsRedirection(); | ||
app.UseAuthorization(); | ||
app.MapControllers(); | ||
|
||
var logger = app.Services.GetRequiredService<ILogger<Program>>(); | ||
|
||
try | ||
{ | ||
using (var scope = app.Services.CreateScope()) | ||
using (var dbContext = scope.ServiceProvider.GetRequiredService<DidactDbContext>()) | ||
try | ||
{ | ||
logger.LogInformation("Attempting to migrate the database on engine startup..."); | ||
dbContext.Database.Migrate(); | ||
logger.LogInformation("Database migrated successfully."); | ||
} | ||
catch (Exception ex) | ||
{ | ||
logger.LogError(ex, "Unhandled exception while applying migrations for {T}", typeof(DidactDbContext)); | ||
throw; | ||
} | ||
// Configure middleware | ||
app.ConfigureMiddleware(); | ||
|
||
app.Run(); | ||
return 0; | ||
} | ||
catch (Exception ex) | ||
{ | ||
logger.LogCritical(ex, "An unhandled exception occurred during bootstrapping"); | ||
return 1; | ||
} | ||
// Run Engine | ||
await app.StartEngine<DidactDbContext>(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
namespace DidactEngine.Services.Extensions | ||
{ | ||
public static class CorsExtensions | ||
{ | ||
public static void ConfigureCors(this IHostApplicationBuilder builder) | ||
{ | ||
builder.Services.AddCors(options => | ||
{ | ||
options.AddPolicy(name: "DevelopmentCORS", | ||
policy => | ||
{ | ||
policy.WithOrigins("http://localhost:8080"); | ||
policy.AllowAnyMethod(); | ||
policy.AllowAnyHeader(); | ||
policy.AllowCredentials(); | ||
}); | ||
}); | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
DidactEngine/Services/Extensions/DbConfigurationExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using DidactEngine.Services.Contexts; | ||
using Microsoft.Data.SqlClient; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.Caching.Memory; | ||
|
||
namespace DidactEngine.Services.Extensions | ||
{ | ||
public static class DbConfigurationExtensions | ||
{ | ||
public static void ConfigureDatabase(this IHostApplicationBuilder builder) | ||
{ | ||
string connStringFactory(string name) => new SqlConnectionStringBuilder(builder.Configuration.GetConnectionString(name)) | ||
{ | ||
ApplicationName = "Didact", | ||
PersistSecurityInfo = true, | ||
MultipleActiveResultSets = true, | ||
WorkstationID = Environment.MachineName, | ||
TrustServerCertificate = true | ||
}.ConnectionString; | ||
|
||
builder.Services.AddDbContext<DidactDbContext>( | ||
(sp, opt) => | ||
{ | ||
opt.UseMemoryCache(sp.GetRequiredService<IMemoryCache>()); | ||
opt.UseSqlServer(connStringFactory("Didact"), opt => opt.CommandTimeout(110)); | ||
if (builder.Configuration.GetValue<bool?>("EnableSensitiveDataLogging").GetValueOrDefault()) | ||
{ | ||
opt.EnableDetailedErrors(); | ||
opt.EnableSensitiveDataLogging(); | ||
} | ||
}); | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
DidactEngine/Services/Extensions/MiddlewareConfigurationExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
namespace DidactEngine.Services.Extensions | ||
{ | ||
public static class MiddlewareConfigurationExtensions | ||
{ | ||
public static void ConfigureMiddleware(this WebApplication app) | ||
{ | ||
// Configure the HTTP request pipeline. | ||
if (app.Environment.IsDevelopment()) | ||
{ | ||
app.UseCors("DevelopmentCORS"); | ||
} | ||
|
||
app.UseSwagger(); | ||
app.UseSwaggerUI(); | ||
app.UseHttpsRedirection(); | ||
app.UseAuthorization(); | ||
app.MapControllers(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using Microsoft.OpenApi.Models; | ||
using System.Reflection; | ||
|
||
namespace DidactEngine.Services.Extensions | ||
{ | ||
public static class OpenApiExtensions | ||
{ | ||
public static void ConfigureOpenApi(this IHostApplicationBuilder builder) | ||
{ | ||
string swaggerVersion = "v1"; | ||
builder.Services.AddSwaggerGen(options => | ||
{ | ||
options.SwaggerDoc(swaggerVersion, new OpenApiInfo | ||
{ | ||
Version = swaggerVersion, | ||
Title = "Didact REST API", | ||
Description = "The central REST API of the Didact Engine." | ||
}); | ||
|
||
var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; | ||
options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename)); | ||
}); | ||
builder.Services.AddEndpointsApiExplorer(); | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
DidactEngine/Services/Extensions/ServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using DidactEngine.Services.BackgroundServices; | ||
|
||
namespace DidactEngine.Services.Extensions | ||
{ | ||
public static class ServiceCollectionExtensions | ||
{ | ||
public static void ConfigureApplicationServices(this IHostApplicationBuilder builder) | ||
{ | ||
builder.Services.AddControllers(); | ||
builder.Services.AddMemoryCache(); | ||
|
||
// Register BackgroundServices | ||
builder.Services.AddHostedService<WorkerBackgroundService>(); | ||
// Register Flow helper services from DidactCore. | ||
//builder.Services.AddSingleton<IFlowExecutor, FlowExecutor>(); | ||
// Register repositories from DidactCore. | ||
//builder.Services.AddScoped<IFlowRepository>(); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DMiradakis This was giving me build errors so I changed the variable name.