-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring the Startup.cs and Program1.cs (#229)
* Refactor program rename Program1 to BlazorServerStarter, as we already got a Program but I needed a better name for that fix minor codestyle issues * Extract loading of configuration into another method * extract methods from Main to enhance readability * remove unnecessary comments and commented code * make class static and generalize IConfigurationRoot to IConfiguration in parameters * Extract methods based on comments * Extract Configutation and Dependency Registration from Startup.cs * Minor beautification's and enhancing documentation * Introduce Changelog.md
- Loading branch information
Showing
8 changed files
with
442 additions
and
331 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Changelog | ||
|
||
All notable changes to this project will be documented in this file. | ||
|
||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), | ||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). | ||
|
||
## [Unreleased] | ||
|
||
### Changed | ||
- Renamed Program1.cs to BlazorServerStarter for better readability and to avoid misunderstandings, as there already is a Program class. | ||
- Cleaned BlazorServerStarter in general to have an easier understanding on the process | ||
- Extracted dependency registration into DependencyRegistry.cs and server configuration into ServerConfiguration.cs from Startup.cs | ||
- Refactored ServerConfiguration.cs into smaller parts and applying Clean code and SOLID principles. | ||
|
||
## [Released] | ||
|
||
### [x.x.x] - yyyy-mm-dd | ||
|
||
Here comes the next release version, |
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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using AasSecurity; | ||
using AasxServer; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Hosting; | ||
using System; | ||
using System.IO; | ||
|
||
namespace AasxServerBlazor | ||
{ | ||
public static class BlazorServerStarter | ||
{ | ||
private const string AppSettingsFileName = "appsettings.json"; | ||
private const string KestrelEndpointsHttpUrl = "Kestrel:Endpoints:Http:Url"; | ||
private const string DefaultUrl = "http://*:5000"; | ||
private const char KestrelUrlSeparator = ':'; | ||
|
||
public static void Main(string[] args) | ||
{ | ||
var config = LoadConfiguration(); | ||
var host = BuildHost(args, config); | ||
|
||
host.RunAsync(); | ||
|
||
InitializeProgram(args, config); | ||
|
||
host.WaitForShutdownAsync(); | ||
} | ||
|
||
private static void InitializeProgram(string[] args, IConfiguration config) | ||
{ | ||
Console.WriteLine($"{nameof(InitializeProgram)}"); | ||
Program.con = config; | ||
Program.Main(args); | ||
SecurityHelper.SecurityInit(); | ||
} | ||
|
||
private static IHost BuildHost(string[] args, IConfiguration config) | ||
{ | ||
Console.WriteLine($"{nameof(BuildHost)} with {config}"); | ||
var hostBuilder = CreateHostBuilder(args, config); | ||
return hostBuilder.Build(); | ||
} | ||
|
||
private static IHostBuilder CreateHostBuilder(string[] args, IConfiguration config) | ||
{ | ||
var url = config[KestrelEndpointsHttpUrl]?.Split(KestrelUrlSeparator); | ||
if (url?[2] != null) | ||
{ | ||
Program.blazorPort = url[2]; | ||
} | ||
|
||
return Host.CreateDefaultBuilder(args) | ||
.ConfigureWebHostDefaults(webBuilder => | ||
{ | ||
webBuilder | ||
.UseStartup<Startup>() | ||
.UseUrls(DefaultUrl); | ||
}); | ||
} | ||
|
||
private static IConfigurationRoot LoadConfiguration() | ||
{ | ||
Console.WriteLine($"Loading Configuration in path: {Directory.GetCurrentDirectory()}"); | ||
|
||
var config = new ConfigurationBuilder() | ||
.SetBasePath(Directory.GetCurrentDirectory()) | ||
.AddJsonFile(AppSettingsFileName) | ||
.AddEnvironmentVariables() | ||
.Build(); | ||
return config; | ||
} | ||
} | ||
} |
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,53 @@ | ||
using AasSecurity; | ||
using AasxServerBlazor.Data; | ||
using AasxServerStandardBib.Interfaces; | ||
using AasxServerStandardBib.Logging; | ||
using AasxServerStandardBib.Services; | ||
using IO.Swagger.Lib.V3.Interfaces; | ||
using IO.Swagger.Lib.V3.SerializationModifiers.Mappers; | ||
using IO.Swagger.Lib.V3.SerializationModifiers.Mappers.ValueMappers; | ||
using IO.Swagger.Lib.V3.Services; | ||
using IO.Swagger.Registry.Lib.V3.Interfaces; | ||
using IO.Swagger.Registry.Lib.V3.Services; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace AasxServerBlazor.Configuration; | ||
|
||
public static class DependencyRegistry | ||
{ | ||
public static void Register(IServiceCollection services) | ||
{ | ||
// NOTE: If you register new classes, keep them sorted alphabetically for better readability! | ||
|
||
services.AddSingleton<AASService>(); | ||
services.AddScoped<BlazorSessionService>(); | ||
services.AddSingleton<CredentialService>(); | ||
|
||
services.AddScoped(typeof(IAppLogger<>), typeof(LoggerAdapter<>)); | ||
services.AddSingleton<IAuthorizationHandler, AasSecurityAuthorizationHandler>(); | ||
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); | ||
services.AddSingleton<IRegistryInitializerService, RegistryInitializerService>(); | ||
services.AddTransient<IAasDescriptorPaginationService, AasDescriptorPaginationService>(); | ||
services.AddTransient<IAasRegistryService, AasRegistryService>(); | ||
services.AddTransient<IAasRepositoryApiHelperService, AasRepositoryApiHelperService>(); | ||
services.AddTransient<IAasxFileServerInterfaceService, AasxFileServerInterfaceService>(); | ||
services.AddTransient<IAdminShellPackageEnvironmentService, AdminShellPackageEnvironmentService>(); | ||
services.AddTransient<IAssetAdministrationShellService, AssetAdministrationShellService>(); | ||
services.AddTransient<IBase64UrlDecoderService, Base64UrlDecoderService>(); | ||
services.AddTransient<IConceptDescriptionService, ConceptDescriptionService>(); | ||
services.AddTransient<IGenerateSerializationService, GenerateSerializationService>(); | ||
services.AddTransient<IIdShortPathParserService, IdShortPathParserService>(); | ||
services.AddTransient<IJsonQueryDeserializer, JsonQueryDeserializer>(); | ||
services.AddTransient<ILevelExtentModifierService, LevelExtentModifierService>(); | ||
services.AddTransient<IMappingService, MappingService>(); | ||
services.AddTransient<IMetamodelVerificationService, MetamodelVerificationService>(); | ||
services.AddTransient<IPaginationService, PaginationService>(); | ||
services.AddTransient<IPathModifierService, PathModifierService>(); | ||
services.AddTransient<IReferenceModifierService, ReferenceModifierService>(); | ||
services.AddTransient<ISecurityService, SecurityService>(); | ||
services.AddTransient<ISubmodelService, SubmodelService>(); | ||
services.AddTransient<IValueOnlyJsonDeserializer, ValueOnlyJsonDeserializer>(); | ||
} | ||
} |
Oops, something went wrong.