Skip to content

Commit

Permalink
Refactor Service Registration
Browse files Browse the repository at this point in the history
  • Loading branch information
neozhu committed Dec 22, 2024
1 parent ac5de14 commit 43d8427
Show file tree
Hide file tree
Showing 10 changed files with 341 additions and 210 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ By incorporating robust offline capabilities, CleanAspire empowers developers to
version: '3.8'
services:
apiservice:
image: blazordevlab/cleanaspire-api:0.0.52
image: blazordevlab/cleanaspire-api:0.0.53
environment:
- ASPNETCORE_ENVIRONMENT=Development
- AllowedHosts=*
Expand All @@ -110,7 +110,7 @@ services:


blazorweb:
image: blazordevlab/cleanaspire-webapp:0.0.52
image: blazordevlab/cleanaspire-webapp:0.0.53
environment:
- ASPNETCORE_ENVIRONMENT=Production
- AllowedHosts=*
Expand Down
2 changes: 1 addition & 1 deletion src/CleanAspire.Api/Endpoints/ProductEndpointRegistrar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class ProductEndpointRegistrar : IEndpointRegistrar
{
public void RegisterRoutes(IEndpointRouteBuilder routes)
{
var group = routes.MapGroup("/products").WithTags("products").AllowAnonymous();
var group = routes.MapGroup("/products").WithTags("products").RequireAuthorization();

// Get all products
group.MapGet("/", async ([FromServices] IMediator mediator) =>
Expand Down
123 changes: 93 additions & 30 deletions src/CleanAspire.ClientApp/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,25 @@
using CleanAspire.ClientApp.Services.Interfaces;
using CleanAspire.ClientApp.Services.UserPreferences;
using CleanAspire.ClientApp.Services;
using CleanAspire.ClientApp.Configurations;
using CleanAspire.ClientApp.Services.Identity;
using CleanAspire.ClientApp.Services.JsInterop;
using CleanAspire.ClientApp.Services.Proxies;
using CleanAspire.Api.Client;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.Kiota.Abstractions.Authentication;
using Microsoft.Kiota.Abstractions;
using Microsoft.Kiota.Http.HttpClientLibrary;
using Microsoft.Kiota.Serialization.Form;
using Microsoft.Kiota.Serialization.Json;
using Microsoft.Kiota.Serialization.Multipart;
using Microsoft.Kiota.Serialization.Text;

namespace CleanAspire.ClientApp;

public static class DependencyInjection
{
public static void TryAddScopedMudBlazor(this IServiceCollection services, IConfiguration config)
public static void TryAddMudBlazor(this IServiceCollection services, IConfiguration config)
{
#region register MudBlazor.Services
services.AddMudServices(config =>
Expand Down Expand Up @@ -43,39 +56,89 @@ public static void TryAddScopedMudBlazor(this IServiceCollection services, IConf
services.AddScoped<DialogServiceHelper>();
#endregion
}
public static void TryAddMudBlazor(this IServiceCollection services, IConfiguration config)


public static void AddCoreServices(this IServiceCollection services, IConfiguration configuration)
{
#region register MudBlazor.Services
services.AddMudServices(config =>
{
MudGlobal.InputDefaults.ShrinkLabel = true;
config.SnackbarConfiguration.PositionClass = Defaults.Classes.Position.BottomCenter;
config.SnackbarConfiguration.NewestOnTop = false;
config.SnackbarConfiguration.ShowCloseIcon = true;
config.SnackbarConfiguration.VisibleStateDuration = 3000;
config.SnackbarConfiguration.HideTransitionDuration = 500;
config.SnackbarConfiguration.ShowTransitionDuration = 500;
config.SnackbarConfiguration.SnackbarVariant = Variant.Filled;
// Cookie and Authentication Handlers
services.AddTransient<CookieHandler>();
services.AddTransient<WebpushrAuthHandler>();

// we're currently planning on deprecating `PreventDuplicates`, at least to the end dev. however,
// we may end up wanting to instead set it as internal because the docs project relies on it
// to ensure that the Snackbar always allows duplicates. disabling the warning for now because
// the project is set to treat warnings as errors.
#pragma warning disable 0618
config.SnackbarConfiguration.PreventDuplicates = false;
#pragma warning restore 0618
// Singleton Services
services.AddScoped<UserProfileStore>();
services.AddScoped<OnlineStatusInterop>();
services.AddScoped<OfflineModeState>();
services.AddScoped<IndexedDbCache>();
services.AddScoped<ProductServiceProxy>();
services.AddScoped<OfflineSyncService>();
services.AddScoped<WebpushrService>();

// Configuration
var clientAppSettings = configuration.GetSection(ClientAppSettings.KEY).Get<ClientAppSettings>();
services.AddSingleton(clientAppSettings!);

// MudBlazor Integration
services.TryAddMudBlazor(configuration);
}

public static void AddHttpClients(this IServiceCollection services, IConfiguration configuration)
{
// HttpClient Registration
services.AddHttpClient("apiservice", (sp, options) =>
{
var settings = sp.GetRequiredService<ClientAppSettings>();
options.BaseAddress = new Uri(settings.ServiceBaseUrl);
}).AddHttpMessageHandler<CookieHandler>();

services.AddHttpClient("Webpushr", client =>
{
client.BaseAddress = new Uri("https://api.webpushr.com");
}).AddHttpMessageHandler<WebpushrAuthHandler>();

// ApiClient
services.AddScoped<ApiClient>(sp =>
{
ApiClientBuilder.RegisterDefaultSerializer<JsonSerializationWriterFactory>();
ApiClientBuilder.RegisterDefaultSerializer<TextSerializationWriterFactory>();
ApiClientBuilder.RegisterDefaultSerializer<FormSerializationWriterFactory>();
ApiClientBuilder.RegisterDefaultSerializer<MultipartSerializationWriterFactory>();
ApiClientBuilder.RegisterDefaultDeserializer<JsonParseNodeFactory>();
ApiClientBuilder.RegisterDefaultDeserializer<TextParseNodeFactory>();
ApiClientBuilder.RegisterDefaultDeserializer<FormParseNodeFactory>();

var settings = sp.GetRequiredService<ClientAppSettings>();
var httpClientFactory = sp.GetRequiredService<IHttpClientFactory>();
var httpClient = httpClientFactory.CreateClient("apiservice");
var authProvider = new AnonymousAuthenticationProvider();
var requestAdapter = new HttpClientRequestAdapter(authProvider, httpClient: httpClient);
var apiClient = new ApiClient(requestAdapter);

if (!string.IsNullOrEmpty(settings.ServiceBaseUrl))
{
requestAdapter.BaseUrl = settings.ServiceBaseUrl;
}

return apiClient;
});
services.AddMudPopoverService();
services.AddMudBlazorSnackbar();
services.AddMudBlazorDialog();
services.AddMudLocalization();
services.AddBlazoredLocalStorageAsSingleton();
services.AddSingleton<IStorageService, LocalStorageService>();
services.AddSingleton<IUserPreferencesService, UserPreferencesService>();
services.AddSingleton<LayoutService>();
services.AddScoped<DialogServiceHelper>();
#endregion

// ApiClient Service
services.AddScoped<ApiClientService>();
}

public static void AddAuthenticationAndLocalization(this IServiceCollection services, IConfiguration configuration)
{
// Authentication and Authorization
services.AddAuthorizationCore();
services.AddCascadingAuthenticationState();
services.AddOidcAuthentication(options =>
{
configuration.Bind("Local", options.ProviderOptions);
});
services.AddScoped<AuthenticationStateProvider, CookieAuthenticationStateProvider>();
services.AddScoped(sp => (ISignInManagement)sp.GetRequiredService<AuthenticationStateProvider>());

// Localization
services.AddLocalization(options => options.ResourcesPath = "Resources");
}
}

70 changes: 3 additions & 67 deletions src/CleanAspire.ClientApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,73 +22,9 @@
//builder.RootComponents.Add<HeadOutlet>("head::after");

// register the cookie handler
builder.Services.AddTransient<CookieHandler>();
builder.Services.AddTransient<WebpushrAuthHandler>();
builder.Services.AddSingleton<UserProfileStore>();
builder.Services.AddSingleton<OnlineStatusInterop>();
builder.Services.AddSingleton<OfflineModeState>();
builder.Services.AddSingleton<IndexedDbCache>();
builder.Services.AddSingleton<ProductServiceProxy>();
builder.Services.AddSingleton<OfflineSyncService>();

var clientAppSettings = builder.Configuration.GetSection(ClientAppSettings.KEY).Get<ClientAppSettings>();
builder.Services.AddSingleton(clientAppSettings!);

builder.Services.TryAddMudBlazor(builder.Configuration);

var httpClientBuilder = builder.Services.AddHttpClient("apiservice", (sp, options) =>
{
var settings = sp.GetRequiredService<ClientAppSettings>();
options.BaseAddress = new Uri(settings.ServiceBaseUrl);

}).AddHttpMessageHandler<CookieHandler>();

builder.Services.AddSingleton<ApiClient>(sp =>
{
ApiClientBuilder.RegisterDefaultSerializer<JsonSerializationWriterFactory>();
ApiClientBuilder.RegisterDefaultSerializer<TextSerializationWriterFactory>();
ApiClientBuilder.RegisterDefaultSerializer<FormSerializationWriterFactory>();
ApiClientBuilder.RegisterDefaultSerializer<MultipartSerializationWriterFactory>();
ApiClientBuilder.RegisterDefaultDeserializer<JsonParseNodeFactory>();
ApiClientBuilder.RegisterDefaultDeserializer<TextParseNodeFactory>();
ApiClientBuilder.RegisterDefaultDeserializer<FormParseNodeFactory>();
var settings = sp.GetRequiredService<ClientAppSettings>();
var httpClientFactory = sp.GetRequiredService<IHttpClientFactory>();
var httpClient = httpClientFactory.CreateClient("apiservice");
var authProvider = new AnonymousAuthenticationProvider();
var requestAdapter = new HttpClientRequestAdapter(authProvider, httpClient: httpClient);
var apiClient = new ApiClient(requestAdapter);
if (!string.IsNullOrEmpty(settings.ServiceBaseUrl))
{
requestAdapter.BaseUrl = settings.ServiceBaseUrl;
}
return apiClient;

});
builder.Services.AddHttpClient("Webpushr", client =>
{
client.BaseAddress = new Uri("https://api.webpushr.com");
}).AddHttpMessageHandler<WebpushrAuthHandler>();
builder.Services.AddSingleton<WebpushrService>();

builder.Services.AddSingleton<ApiClientService>();
builder.Services.AddAuthorizationCore();
builder.Services.AddCascadingAuthenticationState();
builder.Services.AddOidcAuthentication(options =>
{
// Configure your authentication provider options here.
// For more information, see https://aka.ms/blazor-standalone-auth
builder.Configuration.Bind("Local", options.ProviderOptions);
});
// register the custom state provider
builder.Services.AddSingleton<AuthenticationStateProvider, CookieAuthenticationStateProvider>();

// register the account management interface
builder.Services.AddSingleton(
sp => (ISignInManagement)sp.GetRequiredService<AuthenticationStateProvider>());


builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
builder.Services.AddCoreServices(builder.Configuration);
builder.Services.AddHttpClients(builder.Configuration);
builder.Services.AddAuthenticationAndLocalization(builder.Configuration);

var app = builder.Build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"ClientAppSettings": {
"AppName": "Blazor Aspire",
"Version": "v0.0.52",
"Version": "v0.0.53",
"ServiceBaseUrl": "https://localhost:7341"
}
}
2 changes: 1 addition & 1 deletion src/CleanAspire.ClientApp/wwwroot/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"ClientAppSettings": {
"AppName": "Blazor Aspire",
"Version": "v0.0.52",
"Version": "v0.0.53",
"ServiceBaseUrl": "https://apiservice.blazorserver.com"
}
}
88 changes: 3 additions & 85 deletions src/CleanAspire.WebApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,103 +2,21 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using CleanAspire.Api.Client;
using CleanAspire.ClientApp.Configurations;
using CleanAspire.ClientApp.Services;
using CleanAspire.ClientApp.Services.Identity;
using CleanAspire.ClientApp.Services.JsInterop;
using CleanAspire.ClientApp.Services.Proxies;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.Kiota.Abstractions;
using Microsoft.Kiota.Abstractions.Authentication;
using Microsoft.Kiota.Http.HttpClientLibrary;
using Microsoft.Kiota.Serialization.Form;
using Microsoft.Kiota.Serialization.Json;
using Microsoft.Kiota.Serialization.Multipart;
using Microsoft.Kiota.Serialization.Text;
using CleanAspire.ClientApp;


var builder = WebApplication.CreateBuilder(args);
builder.AddServiceDefaults();



// Add services to the container.
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents()
.AddInteractiveWebAssemblyComponents();



// register the cookie handler
builder.Services.AddTransient<CookieHandler>();
builder.Services.AddTransient<WebpushrAuthHandler>();
builder.Services.AddScoped<UserProfileStore>();
builder.Services.AddScoped<OnlineStatusInterop>();
builder.Services.AddScoped<OfflineModeState>();
builder.Services.AddScoped<IndexedDbCache>();
builder.Services.AddScoped<ProductServiceProxy>();
builder.Services.AddScoped<OfflineSyncService>();

var clientAppSettings = builder.Configuration.GetSection(ClientAppSettings.KEY).Get<ClientAppSettings>();
builder.Services.AddSingleton(clientAppSettings!);

builder.Services.TryAddScopedMudBlazor(builder.Configuration);

var httpClientBuilder = builder.Services.AddHttpClient("apiservice", (sp, options) =>
{
var settings = sp.GetRequiredService<ClientAppSettings>();
options.BaseAddress = new Uri(settings.ServiceBaseUrl);

}).AddHttpMessageHandler<CookieHandler>();

builder.Services.AddScoped<ApiClient>(sp =>
{
ApiClientBuilder.RegisterDefaultSerializer<JsonSerializationWriterFactory>();
ApiClientBuilder.RegisterDefaultSerializer<TextSerializationWriterFactory>();
ApiClientBuilder.RegisterDefaultSerializer<FormSerializationWriterFactory>();
ApiClientBuilder.RegisterDefaultSerializer<MultipartSerializationWriterFactory>();
ApiClientBuilder.RegisterDefaultDeserializer<JsonParseNodeFactory>();
ApiClientBuilder.RegisterDefaultDeserializer<TextParseNodeFactory>();
ApiClientBuilder.RegisterDefaultDeserializer<FormParseNodeFactory>();
var settings = sp.GetRequiredService<ClientAppSettings>();
var httpClientFactory = sp.GetRequiredService<IHttpClientFactory>();
var httpClient = httpClientFactory.CreateClient("apiservice");
var authProvider = new AnonymousAuthenticationProvider();
var requestAdapter = new HttpClientRequestAdapter(authProvider, httpClient: httpClient);
var apiClient = new ApiClient(requestAdapter);
if (!string.IsNullOrEmpty(settings.ServiceBaseUrl))
{
requestAdapter.BaseUrl = settings.ServiceBaseUrl;
}
return apiClient;

});
builder.Services.AddHttpClient("Webpushr", client =>
{
client.BaseAddress = new Uri("https://api.webpushr.com");
}).AddHttpMessageHandler<WebpushrAuthHandler>();
builder.Services.AddScoped<WebpushrService>();

builder.Services.AddScoped<ApiClientService>();
builder.Services.AddAuthorizationCore();
builder.Services.AddCascadingAuthenticationState();
builder.Services.AddOidcAuthentication(options =>
{
// Configure your authentication provider options here.
// For more information, see https://aka.ms/blazor-standalone-auth
builder.Configuration.Bind("Local", options.ProviderOptions);
});
// register the custom state provider
builder.Services.AddScoped<AuthenticationStateProvider, CookieAuthenticationStateProvider>();

// register the account management interface
builder.Services.AddScoped(
sp => (ISignInManagement)sp.GetRequiredService<AuthenticationStateProvider>());


builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
builder.Services.AddCoreServices(builder.Configuration);
builder.Services.AddHttpClients(builder.Configuration);
builder.Services.AddAuthenticationAndLocalization(builder.Configuration);

var app = builder.Build();

Expand Down
2 changes: 1 addition & 1 deletion src/CleanAspire.WebApp/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"ClientAppSettings": {
"AppName": "Blazor Aspire",
"Version": "v0.0.52",
"Version": "v0.0.53",
"ServiceBaseUrl": "https://localhost:7341"
}
}
2 changes: 1 addition & 1 deletion src/CleanAspire.WebApp/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"AllowedHosts": "*",
"ClientAppSettings": {
"AppName": "Blazor Aspire",
"Version": "v0.0.52",
"Version": "v0.0.53",
"ServiceBaseUrl": "https://apiservice.blazorserver.com"
}
}
Loading

0 comments on commit 43d8427

Please sign in to comment.