Skip to content

Commit

Permalink
Use Unlaunch feature flag
Browse files Browse the repository at this point in the history
  • Loading branch information
tnguyenquy committed Aug 26, 2021
1 parent ce20b08 commit 116ee29
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/Infrastructure/Infrastructure.csproj
Expand Up @@ -12,6 +12,7 @@
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.8.0" />
<PackageReference Include="unlaunch" Version="1.0.0" />

This comment has been minimized.

Copy link
@tnguyenquy

tnguyenquy Aug 26, 2021

Author Owner

Import unlaunch package for feature flag

</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ApplicationCore\ApplicationCore.csproj" />
Expand Down
19 changes: 14 additions & 5 deletions src/Web/Pages/Index.cshtml
@@ -1,4 +1,6 @@
@page
@using Microsoft.eShopWeb.Web.Services;
@inject UnlaunchService UnlaunchService;

This comment has been minimized.

Copy link
@tnguyenquy

tnguyenquy Aug 26, 2021

Author Owner

inject Unlaunch service in .cshtml file

@{
ViewData["Title"] = "Catalog";
@model IndexModel
Expand Down Expand Up @@ -27,11 +29,18 @@
<partial name="_pagination" for="CatalogModel.PaginationInfo" />

<div class="esh-catalog-items row">
@foreach (var catalogItem in Model.CatalogModel.CatalogItems)
{
<div class="esh-catalog-item col-md-4">
<partial name="_product" for="@catalogItem" />
</div>
@{
if (UnlaunchService.IsEnabled(HttpContext.Connection.RemoteIpAddress.ToString()))
{
Model.CatalogModel.CatalogItems.Reverse();

This comment has been minimized.

Copy link
@tnguyenquy

tnguyenquy Aug 26, 2021

Author Owner

if feature flag is on, we want catalog items to show in reverse order

}

foreach (var catalogItem in Model.CatalogModel.CatalogItems)
{
<div class="esh-catalog-item col-md-4">
<partial name="_product" for="@catalogItem" />
</div>
}
}
</div>
<partial name="_pagination" for="CatalogModel.PaginationInfo" />
Expand Down
22 changes: 22 additions & 0 deletions src/Web/Services/UnlaunchService.cs
@@ -0,0 +1,22 @@
using io.unlaunch;

namespace Microsoft.eShopWeb.Web.Services
{
public class UnlaunchService
{
private const string FlagKey = "catalog_reverse";

private readonly IUnlaunchClient _client;

public UnlaunchService(IUnlaunchClient client)
{
_client = client;
}

public bool IsEnabled(string userIdentity)
{
var variation = _client.GetVariation(FlagKey, userIdentity);
return variation == "on";

This comment has been minimized.

Copy link
@tnguyenquy

tnguyenquy Aug 26, 2021

Author Owner

Check if feature flag is enabled with provided flag_key and userId (userId could be anything, it is an identity)

}
}
}
5 changes: 5 additions & 0 deletions src/Web/Startup.cs
Expand Up @@ -3,6 +3,7 @@
using BlazorAdmin.Services;
using Blazored.LocalStorage;
using BlazorShared;
using io.unlaunch;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.DataProtection;
Expand All @@ -16,6 +17,7 @@
using Microsoft.eShopWeb.Infrastructure.Data;
using Microsoft.eShopWeb.Infrastructure.Identity;
using Microsoft.eShopWeb.Web.Configuration;
using Microsoft.eShopWeb.Web.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
Expand Down Expand Up @@ -161,6 +163,9 @@ public void ConfigureServices(IServiceCollection services)
services.AddScoped<HttpService>();
services.AddBlazorServices();

services.AddSingleton<IUnlaunchClient>(UnlaunchClient.Create("prod-server-0bdc1324-cbec-4740-83e9-86045d1cd93f"));

This comment has been minimized.

Copy link
@tnguyenquy

tnguyenquy Aug 26, 2021

Author Owner

make unlaunchClient a singleton, so we only have single connection to Unlaunch server

services.AddTransient<UnlaunchService>();

This comment has been minimized.

Copy link
@tnguyenquy

tnguyenquy Aug 26, 2021

Author Owner

We can create an Unlaunch service, to handle feature flag logic in here, and inject this service in other places


services.AddDatabaseDeveloperPageExceptionFilter();

_services = services; // used to debug registered services
Expand Down

0 comments on commit 116ee29

Please sign in to comment.