148 lines
4.0 KiB
C#
148 lines
4.0 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using System.Net;
|
|
using System.Text.Json.Serialization;
|
|
using NLog;
|
|
using ChatRoomContract;
|
|
using System.Diagnostics;
|
|
using SimpleRpc.Transports;
|
|
using SimpleRpc.Transports.Http.Client;
|
|
using SimpleRpc.Serialization.Hyperion;
|
|
|
|
namespace RestToSimpleRPCAdapter;
|
|
|
|
public class Server
|
|
{
|
|
/// <summary>
|
|
/// Logger for this class.
|
|
/// </summary>
|
|
Logger log = LogManager.GetCurrentClassLogger();
|
|
|
|
/// <summary>
|
|
/// Program entry point.
|
|
/// </summary>
|
|
/// <param name="args">Command line arguments.</param>
|
|
public static void Main(string[] args)
|
|
{
|
|
var self = new Server();
|
|
self.Run(args);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Program body.
|
|
/// </summary>
|
|
/// <param name="args">Command line arguments.</param>
|
|
private void Run(string[] args)
|
|
{
|
|
//configure logging
|
|
ConfigureLogging();
|
|
|
|
//indicate server is about to start
|
|
log.Info("Server is about to start");
|
|
|
|
//start the server
|
|
StartServer(args);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Configure loggin subsystem.
|
|
/// </summary>
|
|
private void ConfigureLogging()
|
|
{
|
|
var config = new NLog.Config.LoggingConfiguration();
|
|
|
|
var console =
|
|
new NLog.Targets.ConsoleTarget("console")
|
|
{
|
|
Layout = @"${date:format=HH\:mm\:ss}|${level}| ${message} ${exception}"
|
|
};
|
|
config.AddTarget(console);
|
|
config.AddRuleForAllLevels(console);
|
|
|
|
LogManager.Configuration = config;
|
|
}
|
|
|
|
public IChatRoomService getSimpleRPCService()
|
|
{
|
|
//connect to the server, get service client proxy
|
|
var sc = new ServiceCollection();
|
|
sc
|
|
.AddSimpleRpcClient(
|
|
"chatRoomService", //must be same as on line 86
|
|
new HttpClientTransportOptions
|
|
{
|
|
Url = "http://127.0.0.1:5000/simplerpc",
|
|
Serializer = "HyperionMessageSerializer"
|
|
}
|
|
)
|
|
.AddSimpleRpcHyperionSerializer();
|
|
|
|
sc.AddSimpleRpcProxy<IChatRoomService>("chatRoomService"); //must be same as on line 77
|
|
|
|
var sp = sc.BuildServiceProvider();
|
|
|
|
var chatRoom = sp.GetService<IChatRoomService>();
|
|
Debug.Assert(chatRoom != null);
|
|
|
|
return chatRoom;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Starts integrated server.
|
|
/// </summary>
|
|
/// <param name="args">Command line arguments.</param>
|
|
public void StartServer(string[] args)
|
|
{
|
|
//create web app builder
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
//configure integrated server
|
|
builder.WebHost.ConfigureKestrel(opts =>
|
|
{
|
|
opts.Listen(IPAddress.Loopback, 5001);
|
|
});
|
|
|
|
//turn on support for web api controllers
|
|
builder.Services
|
|
.AddControllers()
|
|
.AddJsonOptions(opts =>
|
|
{
|
|
//this makes enumeration values to be strings instead of integers in opeanapi doc
|
|
opts.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
|
|
});
|
|
|
|
//add CORS policies
|
|
builder.Services.AddCors(cr =>
|
|
{
|
|
//allow everything from everywhere
|
|
cr.AddPolicy("allowAll", cp =>
|
|
{
|
|
cp.AllowAnyOrigin();
|
|
cp.AllowAnyMethod();
|
|
cp.AllowAnyHeader();
|
|
});
|
|
});
|
|
|
|
//publish the background logic as (an internal) service through dependency injection,
|
|
//otherwise it will not start until the first client calls into controller
|
|
builder.Services.AddSingleton(getSimpleRPCService());
|
|
|
|
//build the server
|
|
var app = builder.Build();
|
|
|
|
//turn CORS policy on
|
|
app.UseCors("allowAll");
|
|
|
|
//turn on request routing
|
|
app.UseRouting();
|
|
|
|
//configure routes
|
|
app.MapControllerRoute(
|
|
name: "default",
|
|
pattern: "{controller}/{action=Index}/{id?}"
|
|
);
|
|
|
|
//run the server
|
|
app.Run();
|
|
}
|
|
}
|