-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #653 from sandervanteinde/main
Add Orleans support
- Loading branch information
Showing
28 changed files
with
5,132 additions
and
18 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
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,15 @@ | ||
using Vogen; | ||
|
||
namespace OrleansExample; | ||
|
||
[ValueObject<string>(Conversions.Default | Conversions.Orleans)] | ||
public partial class CustomUrl | ||
{ | ||
private static Validation Validate(string value) | ||
{ | ||
// Just for example’s sake, a real Url validator should be more complex then this. | ||
return value.StartsWith("http") | ||
? Validation.Ok | ||
: Validation.Invalid("A url should start with http"); | ||
} | ||
} |
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,7 @@ | ||
namespace OrleansExample; | ||
|
||
public interface IUrlShortenerGrain : IGrainWithStringKey | ||
{ | ||
Task SetUrl(CustomUrl fullUrl); | ||
Task<CustomUrl> GetUrl(); | ||
} |
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,22 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<UseLocallyBuiltPackage>true</UseLocallyBuiltPackage> | ||
<NoWarn>$(NoWarn),NU1603,NU1903</NoWarn> | ||
</PropertyGroup> | ||
|
||
<ItemGroup Condition=" '$(UseLocallyBuiltPackage)' != ''"> | ||
<PackageReference Include="Vogen" Version="999.9.*" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup Condition=" '$(UseLocallyBuiltPackage)' == ''"> | ||
<PackageReference Include="Vogen" Version="999.9.10219943" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Orleans.Server" Version="8.2.0" /> | ||
</ItemGroup> | ||
</Project> |
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,9 @@ | ||
@OrleansExample_HostAddress = http://localhost:5242 | ||
|
||
### Create shortened URL | ||
GET {{OrleansExample_HostAddress}}/shorten?url=https%3A%2F%2Fgithub.com%2FSteveDunn%2FVogen | ||
Accept: application/json | ||
|
||
### Create shortened URL that does not meet validation, this generated a 400 Bad Request | ||
GET {{OrleansExample_HostAddress}}/shorten?url=invalidUrl | ||
Accept: application/json |
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,51 @@ | ||
using Orleans.Runtime; | ||
using OrleansExample; | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
builder.Host.UseOrleans(static siloBuilder => | ||
{ | ||
siloBuilder.UseLocalhostClustering(); | ||
siloBuilder.AddMemoryGrainStorage("urls"); | ||
}); | ||
|
||
var app = builder.Build(); | ||
|
||
app.MapGet("/", static () => "Welcome to the URL shortener, powered by Orleans!"); | ||
|
||
app.MapGet("/shorten", | ||
static async (IGrainFactory grains, HttpRequest request, CustomUrl url) => | ||
{ | ||
var host = $"{request.Scheme}://{request.Host.Value}"; | ||
|
||
// Create a unique, short ID | ||
var shortenedRouteSegment = Guid.NewGuid().GetHashCode().ToString("X"); | ||
|
||
// Create and persist a grain with the shortened ID and full URL | ||
var shortenerGrain = | ||
grains.GetGrain<IUrlShortenerGrain>(shortenedRouteSegment); | ||
|
||
await shortenerGrain.SetUrl(url); | ||
|
||
// Return the shortened URL for later use | ||
var resultBuilder = new UriBuilder(host) | ||
{ | ||
Path = $"/go/{shortenedRouteSegment}" | ||
}; | ||
|
||
return Results.Ok(resultBuilder.Uri); | ||
}); | ||
|
||
app.MapGet("/go/{shortenedRouteSegment:required}", | ||
static async (IGrainFactory grains, string shortenedRouteSegment) => | ||
{ | ||
// Retrieve the grain using the shortened ID and url to the original URL | ||
var shortenerGrain = | ||
grains.GetGrain<IUrlShortenerGrain>(shortenedRouteSegment); | ||
|
||
var url = await shortenerGrain.GetUrl(); | ||
|
||
return Results.Redirect(url.Value); | ||
}); | ||
|
||
app.Run(); |
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,31 @@ | ||
{ | ||
"$schema": "https://json.schemastore.org/launchsettings.json", | ||
"iisSettings": { | ||
"windowsAuthentication": false, | ||
"anonymousAuthentication": true, | ||
"iisExpress": { | ||
"applicationUrl": "http://localhost:59890", | ||
"sslPort": 0 | ||
} | ||
}, | ||
"profiles": { | ||
"http": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"launchUrl": "weatherforecast", | ||
"applicationUrl": "http://localhost:5242", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"IIS Express": { | ||
"commandName": "IISExpress", | ||
"launchBrowser": true, | ||
"launchUrl": "weatherforecast", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
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,7 @@ | ||
# Orleans example | ||
|
||
This project showcases how Vogen can be utilized within the [Microsoft Orleans](https://learn.microsoft.com/en-us/dotnet/orleans/) framework. | ||
|
||
It showcases the [quickstart example](https://learn.microsoft.com/en-us/dotnet/orleans/quickstarts/build-your-first-orleans-app?tabs=visual-studio) but with Vogen value objects. | ||
|
||
The [http file](./OrleansExample.http) can be utilized to test the API running Orleans. |
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,11 @@ | ||
namespace OrleansExample; | ||
|
||
[GenerateSerializer, Alias(nameof(UrlDetails))] | ||
public sealed record UrlDetails | ||
{ | ||
[Id(0)] | ||
public required CustomUrl FullUrl { get; init; } | ||
|
||
[Id(1)] | ||
public required string ShortenedRouteSegment { get; init; } | ||
} |
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,23 @@ | ||
namespace OrleansExample; | ||
|
||
public class UrlShortenerGrain( | ||
[PersistentState(stateName: "url", storageName: "urls")] | ||
IPersistentState<UrlDetails> state | ||
) : Grain, IUrlShortenerGrain | ||
{ | ||
public async Task SetUrl(CustomUrl fullUrl) | ||
{ | ||
state.State = new() | ||
{ | ||
ShortenedRouteSegment = this.GetPrimaryKeyString(), | ||
FullUrl = fullUrl | ||
}; | ||
|
||
await state.WriteStateAsync(); | ||
} | ||
|
||
public Task<CustomUrl> GetUrl() | ||
{ | ||
return Task.FromResult(state.State.FullUrl); | ||
} | ||
} |
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,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
} | ||
} |
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,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |
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
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
Oops, something went wrong.