Skip to content

Commit

Permalink
Upgrade to .net8 + ServiceStack v8 with endpoint routing
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Jan 5, 2024
1 parent dd484f6 commit 3fc8ae1
Show file tree
Hide file tree
Showing 18 changed files with 737 additions and 730 deletions.
25 changes: 15 additions & 10 deletions Chat/Chat.csproj
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<AssemblyName>Chat</AssemblyName>
<PackageId>Chat</PackageId>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
<TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
<PublishProfile>DefaultContainer</PublishProfile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ServiceStack.Text" Version="6.*" />
<PackageReference Include="ServiceStack.Interfaces" Version="6.*" />
<PackageReference Include="ServiceStack.Client" Version="6.*" />
<PackageReference Include="ServiceStack.Common" Version="6.*" />
<PackageReference Include="ServiceStack" Version="6.*" />
<PackageReference Include="ServiceStack.Mvc" Version="6.*" />
<PackageReference Include="ServiceStack.Redis" Version="6.*" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="8.*" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="8.*" />
<PackageReference Include="ServiceStack.Text" Version="8.*" />
<PackageReference Include="ServiceStack.Interfaces" Version="8.*" />
<PackageReference Include="ServiceStack.Client" Version="8.*" />
<PackageReference Include="ServiceStack.Common" Version="8.*" />
<PackageReference Include="ServiceStack" Version="8.*" />
<PackageReference Include="ServiceStack.Mvc" Version="8.*" />
<PackageReference Include="ServiceStack.Redis" Version="8.*" />
</ItemGroup>

</Project>
41 changes: 41 additions & 0 deletions Chat/Configure.AppHost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Net;
using ServiceStack;
using ServiceStack.Auth;

[assembly: HostingStartup(typeof(Chat.AppHost))]

namespace Chat;

public class AppHost() : AppHostBase("Chat"), IHostingStartup
{
public void Configure(IWebHostBuilder builder) => builder
.ConfigureServices((context, services) => {
// Configure ASP.NET Core IOC Dependencies
services.AddSingleton<IChatHistory, MemoryChatHistory>();
services.AddPlugin(new ServerEventsFeature());
});

public override void Configure()
{
SetConfig(new HostConfig
{
DefaultContentType = MimeTypes.Json,
AllowSessionIdsInHttpParams = true,
});

this.CustomErrorHttpHandlers.Remove(HttpStatusCode.Forbidden);

//Register all Authentication methods you want to enable for this web app.
Plugins.Add(new AuthFeature(() => new AuthUserSession(), [
new TwitterAuthProvider(AppSettings), //Sign-in with Twitter
new FacebookAuthProvider(AppSettings), //Sign-in with Facebook
new GithubAuthProvider(AppSettings) //Sign-in with GitHub
]));

// for lte IE 9 support + allow connections from local web dev apps
Plugins.Add(new CorsFeature(
allowOriginWhitelist: ["http://localhost", "http://127.0.0.1:8080", "http://localhost:8080", "http://localhost:8081", "http://null.jsbin.com"],
allowCredentials: true,
allowedHeaders: "Content-Type, Allow, Authorization"));
}
}
4 changes: 0 additions & 4 deletions Chat/Dockerfile

This file was deleted.

53 changes: 53 additions & 0 deletions Chat/IChatHistory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using ServiceStack;
using Chat.ServiceModel;

namespace Chat;

public interface IChatHistory
{
long GetNextMessageId(string channel);

void Log(string channel, ChatMessage msg);

List<ChatMessage> GetRecentChatHistory(string channel, long? afterId, int? take);

void Flush();
}

public class MemoryChatHistory(IServerEvents serverEvents) : IChatHistory
{
public int DefaultLimit { get; set; } = 100;

Dictionary<string, List<ChatMessage>> MessagesMap = new();

public long GetNextMessageId(string channel)
{
return serverEvents.GetNextSequence("chatMsg");
}

public void Log(string channel, ChatMessage msg)
{
if (!MessagesMap.TryGetValue(channel, out var msgs))
MessagesMap[channel] = msgs = new List<ChatMessage>();

msgs.Add(msg);
}

public List<ChatMessage> GetRecentChatHistory(string channel, long? afterId, int? take)
{
if (!MessagesMap.TryGetValue(channel, out var msgs))
return [];

var ret = msgs.Where(x => x.Id > afterId.GetValueOrDefault())
.Reverse() //get latest logs
.Take(take.GetValueOrDefault(DefaultLimit))
.Reverse(); //reverse back

return ret.ToList();
}

public void Flush()
{
MessagesMap = new Dictionary<string, List<ChatMessage>>();
}
}
Loading

0 comments on commit 3fc8ae1

Please sign in to comment.