mirror of
https://github.com/streetwriters/notesnook-sync-server.git
synced 2026-02-12 11:12:44 +00:00
global: newline changes
This commit is contained in:
@@ -1,50 +1,50 @@
|
||||
FROM mcr.microsoft.com/dotnet/runtime-deps:8.0-alpine AS base
|
||||
WORKDIR /app
|
||||
|
||||
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:8.0-alpine AS build
|
||||
ARG TARGETARCH
|
||||
ARG BUILDPLATFORM
|
||||
ENV DOTNET_TC_QuickJitForLoops="1" DOTNET_ReadyToRun="0" DOTNET_TieredPGO="1" DOTNET_SYSTEM_GLOBALIZATION_INVARIANT="true"
|
||||
|
||||
WORKDIR /src
|
||||
|
||||
COPY Streetwriters.Data/*.csproj ./Streetwriters.Data/
|
||||
COPY Streetwriters.Common/*.csproj ./Streetwriters.Common/
|
||||
COPY Streetwriters.Messenger/*.csproj ./Streetwriters.Messenger/
|
||||
|
||||
# restore dependencies
|
||||
RUN dotnet restore -v d /src/Streetwriters.Messenger/Streetwriters.Messenger.csproj --use-current-runtime
|
||||
|
||||
COPY Streetwriters.Data/ ./Streetwriters.Data/
|
||||
COPY Streetwriters.Common/ ./Streetwriters.Common/
|
||||
COPY Streetwriters.Messenger/ ./Streetwriters.Messenger/
|
||||
|
||||
WORKDIR /src/Streetwriters.Messenger/
|
||||
|
||||
RUN dotnet build -c Release -o /app/build -a $TARGETARCH
|
||||
|
||||
FROM build AS publish
|
||||
RUN dotnet publish -c Release -o /app/publish \
|
||||
#--runtime alpine-x64 \
|
||||
--self-contained true \
|
||||
/p:TrimMode=partial \
|
||||
/p:PublishTrimmed=true \
|
||||
/p:PublishSingleFile=true \
|
||||
/p:JsonSerializerIsReflectionEnabledByDefault=true \
|
||||
-a $TARGETARCH
|
||||
|
||||
FROM --platform=$BUILDPLATFORM base AS final
|
||||
ARG TARGETARCH
|
||||
ARG BUILDPLATFORM
|
||||
|
||||
# create a new user and change directory ownership
|
||||
RUN adduser --disabled-password \
|
||||
--home /app \
|
||||
--gecos '' dotnetuser && chown -R dotnetuser /app
|
||||
|
||||
# impersonate into the new user
|
||||
USER dotnetuser
|
||||
WORKDIR /app
|
||||
|
||||
COPY --from=publish /app/publish .
|
||||
FROM mcr.microsoft.com/dotnet/runtime-deps:8.0-alpine AS base
|
||||
WORKDIR /app
|
||||
|
||||
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:8.0-alpine AS build
|
||||
ARG TARGETARCH
|
||||
ARG BUILDPLATFORM
|
||||
ENV DOTNET_TC_QuickJitForLoops="1" DOTNET_ReadyToRun="0" DOTNET_TieredPGO="1" DOTNET_SYSTEM_GLOBALIZATION_INVARIANT="true"
|
||||
|
||||
WORKDIR /src
|
||||
|
||||
COPY Streetwriters.Data/*.csproj ./Streetwriters.Data/
|
||||
COPY Streetwriters.Common/*.csproj ./Streetwriters.Common/
|
||||
COPY Streetwriters.Messenger/*.csproj ./Streetwriters.Messenger/
|
||||
|
||||
# restore dependencies
|
||||
RUN dotnet restore -v d /src/Streetwriters.Messenger/Streetwriters.Messenger.csproj --use-current-runtime
|
||||
|
||||
COPY Streetwriters.Data/ ./Streetwriters.Data/
|
||||
COPY Streetwriters.Common/ ./Streetwriters.Common/
|
||||
COPY Streetwriters.Messenger/ ./Streetwriters.Messenger/
|
||||
|
||||
WORKDIR /src/Streetwriters.Messenger/
|
||||
|
||||
RUN dotnet build -c Release -o /app/build -a $TARGETARCH
|
||||
|
||||
FROM build AS publish
|
||||
RUN dotnet publish -c Release -o /app/publish \
|
||||
#--runtime alpine-x64 \
|
||||
--self-contained true \
|
||||
/p:TrimMode=partial \
|
||||
/p:PublishTrimmed=true \
|
||||
/p:PublishSingleFile=true \
|
||||
/p:JsonSerializerIsReflectionEnabledByDefault=true \
|
||||
-a $TARGETARCH
|
||||
|
||||
FROM --platform=$BUILDPLATFORM base AS final
|
||||
ARG TARGETARCH
|
||||
ARG BUILDPLATFORM
|
||||
|
||||
# create a new user and change directory ownership
|
||||
RUN adduser --disabled-password \
|
||||
--home /app \
|
||||
--gecos '' dotnetuser && chown -R dotnetuser /app
|
||||
|
||||
# impersonate into the new user
|
||||
USER dotnetuser
|
||||
WORKDIR /app
|
||||
|
||||
COPY --from=publish /app/publish .
|
||||
ENTRYPOINT ["./Streetwriters.Messenger"]
|
||||
@@ -1,45 +1,45 @@
|
||||
/*
|
||||
This file is part of the Notesnook Sync Server project (https://notesnook.com/)
|
||||
|
||||
Copyright (C) 2023 Streetwriters (Private) Limited
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the Affero GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
Affero GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the Affero GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Lib.AspNetCore.ServerSentEvents;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace Streetwriters.Messenger.Helpers
|
||||
{
|
||||
public class SSEHelper
|
||||
{
|
||||
public static async Task SendEventToUserAsync(string data, IServerSentEventsService sseService, string userId, string originTokenId = null)
|
||||
{
|
||||
var clients = sseService.GetClients().Where(c => c.User.FindFirstValue("sub") == userId);
|
||||
foreach (var client in clients)
|
||||
{
|
||||
if (originTokenId != null && client.User.FindFirstValue("jti") == originTokenId) continue;
|
||||
if (!client.IsConnected) continue;
|
||||
await client.SendEventAsync(data);
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task SendEventToAllUsersAsync(string data, IServerSentEventsService sseService)
|
||||
{
|
||||
await sseService.SendEventAsync(data);
|
||||
}
|
||||
}
|
||||
/*
|
||||
This file is part of the Notesnook Sync Server project (https://notesnook.com/)
|
||||
|
||||
Copyright (C) 2023 Streetwriters (Private) Limited
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the Affero GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
Affero GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the Affero GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Lib.AspNetCore.ServerSentEvents;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace Streetwriters.Messenger.Helpers
|
||||
{
|
||||
public class SSEHelper
|
||||
{
|
||||
public static async Task SendEventToUserAsync(string data, IServerSentEventsService sseService, string userId, string originTokenId = null)
|
||||
{
|
||||
var clients = sseService.GetClients().Where(c => c.User.FindFirstValue("sub") == userId);
|
||||
foreach (var client in clients)
|
||||
{
|
||||
if (originTokenId != null && client.User.FindFirstValue("jti") == originTokenId) continue;
|
||||
if (!client.IsConnected) continue;
|
||||
await client.SendEventAsync(data);
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task SendEventToAllUsersAsync(string data, IServerSentEventsService sseService)
|
||||
{
|
||||
await sseService.SendEventAsync(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,31 +1,31 @@
|
||||
{
|
||||
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:53323",
|
||||
"sslPort": 44382
|
||||
}
|
||||
},
|
||||
"profiles": {
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"Streetwriters.Messenger": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": "true",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "https://localhost:5001;http://localhost:5000",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
{
|
||||
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:53323",
|
||||
"sslPort": 44382
|
||||
}
|
||||
},
|
||||
"profiles": {
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"Streetwriters.Messenger": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": "true",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "https://localhost:5001;http://localhost:5000",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,64 +1,64 @@
|
||||
/*
|
||||
This file is part of the Notesnook Sync Server project (https://notesnook.com/)
|
||||
|
||||
Copyright (C) 2023 Streetwriters (Private) Limited
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the Affero GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
Affero GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the Affero GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Lib.AspNetCore.ServerSentEvents;
|
||||
using Streetwriters.Messenger.Helpers;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Streetwriters.Messenger.Services
|
||||
{
|
||||
internal class HeartbeatService : BackgroundService
|
||||
{
|
||||
#region Fields
|
||||
private const string HEARTBEAT_MESSAGE_FORMAT = "Streetwriters Heartbeat ({0} UTC)";
|
||||
|
||||
private readonly IServerSentEventsService _serverSentEventsService;
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
public HeartbeatService(IServerSentEventsService serverSentEventsService)
|
||||
{
|
||||
_serverSentEventsService = serverSentEventsService;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
var message = JsonSerializer.Serialize(new
|
||||
{
|
||||
type = "heartbeat",
|
||||
data = JsonSerializer.Serialize(new
|
||||
{
|
||||
t = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
|
||||
})
|
||||
});
|
||||
await SSEHelper.SendEventToAllUsersAsync(message, _serverSentEventsService);
|
||||
await Task.Delay(TimeSpan.FromSeconds(5), stoppingToken);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
/*
|
||||
This file is part of the Notesnook Sync Server project (https://notesnook.com/)
|
||||
|
||||
Copyright (C) 2023 Streetwriters (Private) Limited
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the Affero GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
Affero GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the Affero GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Lib.AspNetCore.ServerSentEvents;
|
||||
using Streetwriters.Messenger.Helpers;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Streetwriters.Messenger.Services
|
||||
{
|
||||
internal class HeartbeatService : BackgroundService
|
||||
{
|
||||
#region Fields
|
||||
private const string HEARTBEAT_MESSAGE_FORMAT = "Streetwriters Heartbeat ({0} UTC)";
|
||||
|
||||
private readonly IServerSentEventsService _serverSentEventsService;
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
public HeartbeatService(IServerSentEventsService serverSentEventsService)
|
||||
{
|
||||
_serverSentEventsService = serverSentEventsService;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
var message = JsonSerializer.Serialize(new
|
||||
{
|
||||
type = "heartbeat",
|
||||
data = JsonSerializer.Serialize(new
|
||||
{
|
||||
t = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
|
||||
})
|
||||
});
|
||||
await SSEHelper.SendEventToAllUsersAsync(message, _serverSentEventsService);
|
||||
await Task.Delay(TimeSpan.FromSeconds(5), stoppingToken);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -1,147 +1,147 @@
|
||||
/*
|
||||
This file is part of the Notesnook Sync Server project (https://notesnook.com/)
|
||||
|
||||
Copyright (C) 2023 Streetwriters (Private) Limited
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the Affero GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
Affero GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the Affero GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Linq;
|
||||
using System.Reactive.Subjects;
|
||||
using System.Text.Json;
|
||||
using Lib.AspNetCore.ServerSentEvents;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.HttpOverrides;
|
||||
using Microsoft.AspNetCore.ResponseCompression;
|
||||
using Microsoft.Extensions.Caching.Distributed;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Streetwriters.Common;
|
||||
using Streetwriters.Common.Extensions;
|
||||
using Streetwriters.Common.Messages;
|
||||
using Streetwriters.Messenger.Helpers;
|
||||
using Streetwriters.Messenger.Services;
|
||||
using WampSharp.AspNetCore.WebSockets.Server;
|
||||
using WampSharp.Binding;
|
||||
using WampSharp.V2;
|
||||
using WampSharp.V2.Realm;
|
||||
|
||||
namespace Streetwriters.Messenger
|
||||
{
|
||||
public class Startup
|
||||
{
|
||||
public Startup(IConfiguration configuration)
|
||||
{
|
||||
Configuration = configuration;
|
||||
}
|
||||
|
||||
public IConfiguration Configuration { get; }
|
||||
|
||||
// This method gets called by the runtime. Use this method to add services to the container.
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
|
||||
services.AddControllers();
|
||||
|
||||
JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
|
||||
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
|
||||
|
||||
services.AddDefaultCors();
|
||||
services.AddDistributedMemoryCache(delegate (MemoryDistributedCacheOptions cacheOptions)
|
||||
{
|
||||
cacheOptions.SizeLimit = 262144000L;
|
||||
});
|
||||
|
||||
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
||||
.AddOAuth2Introspection("introspection", options =>
|
||||
{
|
||||
options.Authority = Servers.IdentityServer.ToString();
|
||||
options.ClientSecret = Constants.NOTESNOOK_API_SECRET;
|
||||
options.ClientId = "notesnook";
|
||||
options.DiscoveryPolicy.RequireHttps = false;
|
||||
options.CacheKeyGenerator = (options, token) => (token + ":" + "reference_token").Sha256();
|
||||
options.SaveToken = true;
|
||||
options.EnableCaching = true;
|
||||
options.CacheDuration = TimeSpan.FromMinutes(30);
|
||||
});
|
||||
|
||||
services.AddServerSentEvents();
|
||||
services.AddSingleton<IHostedService, HeartbeatService>();
|
||||
services.AddResponseCompression(options =>
|
||||
{
|
||||
options.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(new[] { "text/event-stream" });
|
||||
});
|
||||
services.AddHealthChecks();
|
||||
}
|
||||
|
||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||
{
|
||||
if (!env.IsDevelopment())
|
||||
{
|
||||
app.UseForwardedHeaders(new ForwardedHeadersOptions
|
||||
{
|
||||
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
|
||||
});
|
||||
}
|
||||
|
||||
app.UseCors("notesnook");
|
||||
app.UseVersion(Servers.MessengerServer);
|
||||
|
||||
app.UseRouting();
|
||||
|
||||
app.UseAuthorization();
|
||||
app.UseAuthentication();
|
||||
|
||||
var options = new ServerSentEventsOptions();
|
||||
options.Authorization = new ServerSentEventsAuthorization()
|
||||
{
|
||||
AuthenticationSchemes = "introspection"
|
||||
};
|
||||
app.MapServerSentEvents("/sse", options);
|
||||
|
||||
app.UseWamp(WampServers.MessengerServer, (realm, server) =>
|
||||
{
|
||||
IServerSentEventsService service = app.ApplicationServices.GetRequiredService<IServerSentEventsService>();
|
||||
realm.Subscribe<SendSSEMessage>(MessengerServerTopics.SendSSETopic, async (ev) =>
|
||||
{
|
||||
var message = JsonSerializer.Serialize(ev.Message);
|
||||
if (ev.SendToAll)
|
||||
{
|
||||
await SSEHelper.SendEventToAllUsersAsync(message, service);
|
||||
}
|
||||
else
|
||||
{
|
||||
await SSEHelper.SendEventToUserAsync(message, service, ev.UserId, ev.OriginTokenId);
|
||||
}
|
||||
});
|
||||
|
||||
IDistributedCache cache = app.GetScopedService<IDistributedCache>();
|
||||
realm.Subscribe<ClearCacheMessage>(IdentityServerTopics.ClearCacheTopic, (ev) => ev.Keys.ForEach((key) => cache.Remove(key)));
|
||||
});
|
||||
|
||||
app.UseEndpoints(endpoints =>
|
||||
{
|
||||
endpoints.MapControllers();
|
||||
endpoints.MapHealthChecks("/health");
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
This file is part of the Notesnook Sync Server project (https://notesnook.com/)
|
||||
|
||||
Copyright (C) 2023 Streetwriters (Private) Limited
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the Affero GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
Affero GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the Affero GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Linq;
|
||||
using System.Reactive.Subjects;
|
||||
using System.Text.Json;
|
||||
using Lib.AspNetCore.ServerSentEvents;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.HttpOverrides;
|
||||
using Microsoft.AspNetCore.ResponseCompression;
|
||||
using Microsoft.Extensions.Caching.Distributed;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Streetwriters.Common;
|
||||
using Streetwriters.Common.Extensions;
|
||||
using Streetwriters.Common.Messages;
|
||||
using Streetwriters.Messenger.Helpers;
|
||||
using Streetwriters.Messenger.Services;
|
||||
using WampSharp.AspNetCore.WebSockets.Server;
|
||||
using WampSharp.Binding;
|
||||
using WampSharp.V2;
|
||||
using WampSharp.V2.Realm;
|
||||
|
||||
namespace Streetwriters.Messenger
|
||||
{
|
||||
public class Startup
|
||||
{
|
||||
public Startup(IConfiguration configuration)
|
||||
{
|
||||
Configuration = configuration;
|
||||
}
|
||||
|
||||
public IConfiguration Configuration { get; }
|
||||
|
||||
// This method gets called by the runtime. Use this method to add services to the container.
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
|
||||
services.AddControllers();
|
||||
|
||||
JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
|
||||
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
|
||||
|
||||
services.AddDefaultCors();
|
||||
services.AddDistributedMemoryCache(delegate (MemoryDistributedCacheOptions cacheOptions)
|
||||
{
|
||||
cacheOptions.SizeLimit = 262144000L;
|
||||
});
|
||||
|
||||
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
||||
.AddOAuth2Introspection("introspection", options =>
|
||||
{
|
||||
options.Authority = Servers.IdentityServer.ToString();
|
||||
options.ClientSecret = Constants.NOTESNOOK_API_SECRET;
|
||||
options.ClientId = "notesnook";
|
||||
options.DiscoveryPolicy.RequireHttps = false;
|
||||
options.CacheKeyGenerator = (options, token) => (token + ":" + "reference_token").Sha256();
|
||||
options.SaveToken = true;
|
||||
options.EnableCaching = true;
|
||||
options.CacheDuration = TimeSpan.FromMinutes(30);
|
||||
});
|
||||
|
||||
services.AddServerSentEvents();
|
||||
services.AddSingleton<IHostedService, HeartbeatService>();
|
||||
services.AddResponseCompression(options =>
|
||||
{
|
||||
options.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(new[] { "text/event-stream" });
|
||||
});
|
||||
services.AddHealthChecks();
|
||||
}
|
||||
|
||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||
{
|
||||
if (!env.IsDevelopment())
|
||||
{
|
||||
app.UseForwardedHeaders(new ForwardedHeadersOptions
|
||||
{
|
||||
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
|
||||
});
|
||||
}
|
||||
|
||||
app.UseCors("notesnook");
|
||||
app.UseVersion(Servers.MessengerServer);
|
||||
|
||||
app.UseRouting();
|
||||
|
||||
app.UseAuthorization();
|
||||
app.UseAuthentication();
|
||||
|
||||
var options = new ServerSentEventsOptions();
|
||||
options.Authorization = new ServerSentEventsAuthorization()
|
||||
{
|
||||
AuthenticationSchemes = "introspection"
|
||||
};
|
||||
app.MapServerSentEvents("/sse", options);
|
||||
|
||||
app.UseWamp(WampServers.MessengerServer, (realm, server) =>
|
||||
{
|
||||
IServerSentEventsService service = app.ApplicationServices.GetRequiredService<IServerSentEventsService>();
|
||||
realm.Subscribe<SendSSEMessage>(MessengerServerTopics.SendSSETopic, async (ev) =>
|
||||
{
|
||||
var message = JsonSerializer.Serialize(ev.Message);
|
||||
if (ev.SendToAll)
|
||||
{
|
||||
await SSEHelper.SendEventToAllUsersAsync(message, service);
|
||||
}
|
||||
else
|
||||
{
|
||||
await SSEHelper.SendEventToUserAsync(message, service, ev.UserId, ev.OriginTokenId);
|
||||
}
|
||||
});
|
||||
|
||||
IDistributedCache cache = app.GetScopedService<IDistributedCache>();
|
||||
realm.Subscribe<ClearCacheMessage>(IdentityServerTopics.ClearCacheTopic, (ev) => ev.Keys.ForEach((key) => cache.Remove(key)));
|
||||
});
|
||||
|
||||
app.UseEndpoints(endpoints =>
|
||||
{
|
||||
endpoints.MapControllers();
|
||||
endpoints.MapHealthChecks("/health");
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft": "Warning",
|
||||
"Microsoft.Hosting.Lifetime": "Information"
|
||||
}
|
||||
}
|
||||
}
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft": "Warning",
|
||||
"Microsoft.Hosting.Lifetime": "Information"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user