From 0e06afd5e6f862b6db4ba25a81444e2820b3fa0f Mon Sep 17 00:00:00 2001 From: Karolis2011 Date: Sat, 4 Jan 2020 14:42:57 +0200 Subject: [PATCH] Update to NetCore 3.1, added Kestrel for REST api and few basic endpoints. --- ASS.Server/ASS.Server.csproj | 28 ++++++---- ASS.Server/Program.cs | 55 +++++++------------ ASS.Server/Services/ByondService.cs | 2 +- ASS.Server/Services/GRPCService.cs | 2 + ASS.Server/Web/Controllers/ByondController.cs | 31 +++++++++++ .../Web/Controllers/StatusController.cs | 34 ++++++++++++ ASS.Server/Web/Startup.cs | 48 ++++++++++++++++ ASS.Server/defaultConfig.json | 24 ++++++-- 8 files changed, 171 insertions(+), 53 deletions(-) create mode 100644 ASS.Server/Web/Controllers/ByondController.cs create mode 100644 ASS.Server/Web/Controllers/StatusController.cs create mode 100644 ASS.Server/Web/Startup.cs diff --git a/ASS.Server/ASS.Server.csproj b/ASS.Server/ASS.Server.csproj index ceba0cb..da1cbcc 100644 --- a/ASS.Server/ASS.Server.csproj +++ b/ASS.Server/ASS.Server.csproj @@ -2,21 +2,25 @@ Exe - netcoreapp2.2 + netcoreapp3.1 + + + + - - - - - - - - - - - + + + + + + + + + + + diff --git a/ASS.Server/Program.cs b/ASS.Server/Program.cs index 4b5f237..e8a5c1e 100644 --- a/ASS.Server/Program.cs +++ b/ASS.Server/Program.cs @@ -6,54 +6,37 @@ using System.Threading.Tasks; using ASS.Server.Services; using ASS.Server.Helpers; using System.Net.Http; +using Microsoft.Extensions.Hosting; +using Microsoft.AspNetCore.Hosting; +using ASS.Server.Web; namespace ASS.Server { class Program { static void Main(string[] args) - => new Program().MainAsync(args).GetAwaiter().GetResult(); + => CreateHostBuilder(args).Build().Run(); - public async Task MainAsync(string[] args) - { - - using (var services = ConfigureServices(args)) - { - Grpc.Core.GrpcEnvironment.SetLogger(new GrpcLoggingWraper(services.GetRequiredService>())); - var grpc = services.GetRequiredService(); - grpc.Initialize(); - await Task.Delay(-1); - } - } - - private ServiceProvider ConfigureServices(string[] args) - { - // Stage 1 services - var services = new ServiceCollection() - .AddSingleton(sp => - new ConfigurationBuilder() - .AddJsonFile("defaultConfig.json", false, false) + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder() + .ConfigureAppConfiguration((hbc, cb) => + cb.AddJsonFile("defaultConfig.json", false, false) .AddJsonFile("config.json", true, false) .AddEnvironmentVariables() .AddCommandLine(args) - .Build() - ); - var serviceProvider = services.BuildServiceProvider(); - // Stage 2 services - services - .AddLogging(c => + ) + .ConfigureServices((hbc, sc) => + sc.AddSingleton(sp => new GrpcService(sp)) // This service is not started. + .AddSingleton() + .AddSingleton() + .AddSingleton() + ) + .ConfigureWebHostDefaults(webBuilder => { - c.ClearProviders(); - c.AddConsole(); - c.AddConfiguration(serviceProvider.GetRequiredService().GetSection("Logging")); + webBuilder.UseStartup(); }) - .AddSingleton(sp => new GrpcService(sp)) - .AddSingleton() - .AddSingleton() - .AddSingleton(); - ; - return services.BuildServiceProvider(); - } + + ; } } diff --git a/ASS.Server/Services/ByondService.cs b/ASS.Server/Services/ByondService.cs index 6a4acd2..393b30f 100644 --- a/ASS.Server/Services/ByondService.cs +++ b/ASS.Server/Services/ByondService.cs @@ -16,7 +16,7 @@ using Microsoft.Extensions.Logging; namespace ASS.Server.Services { - class ByondService : Byond.ByondBase + public class ByondService : Byond.ByondBase { const string BYOND_LATEST_URL = "https://secure.byond.com/download/build/LATEST/"; const string BYOND_DOWNLOAD_URL = "https://secure.byond.com/download/build/"; diff --git a/ASS.Server/Services/GRPCService.cs b/ASS.Server/Services/GRPCService.cs index bb5cf78..1419fbb 100644 --- a/ASS.Server/Services/GRPCService.cs +++ b/ASS.Server/Services/GRPCService.cs @@ -14,6 +14,7 @@ namespace ASS.Server.Services IServiceProvider _sp; ILogger logger; IConfiguration config; + public bool IsInitilized { get; private set; } = false; public GrpcService(IServiceProvider sp) : this(sp.GetRequiredService()) { @@ -29,6 +30,7 @@ namespace ASS.Server.Services public void Initialize() { + IsInitilized = true; Services.Add(API.Instance.BindService(_sp.GetRequiredService())); Start(); logger.LogInformation($"gRPC server listening on {config["GRPC:Host"]}:{config["GRPC:Port"]}"); diff --git a/ASS.Server/Web/Controllers/ByondController.cs b/ASS.Server/Web/Controllers/ByondController.cs new file mode 100644 index 0000000..25a1b4e --- /dev/null +++ b/ASS.Server/Web/Controllers/ByondController.cs @@ -0,0 +1,31 @@ +using ASS.Server.Services; +using Microsoft.AspNetCore.Mvc; +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; + +namespace ASS.Server.Web.Controllers +{ + + [ApiController] + [Route("[controller]")] + public class ByondController : ControllerBase + { + + ByondService byondService; + + public ByondController(ByondService bs) + { + byondService = bs; + } + + [HttpPost("install/{major}.{minor}")] + public async Task InstallVersionAsync(int major, int minor) + { + await byondService.SwitchToVersion(new API.ByondVersion() { Major = major, Minor = minor }); + return 0; + } + + } +} diff --git a/ASS.Server/Web/Controllers/StatusController.cs b/ASS.Server/Web/Controllers/StatusController.cs new file mode 100644 index 0000000..4c8bac8 --- /dev/null +++ b/ASS.Server/Web/Controllers/StatusController.cs @@ -0,0 +1,34 @@ +using ASS.Server.Services; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Collections.Generic; +using System.Text; + +namespace ASS.Server.Web.Controllers +{ + [ApiController] + [Route("[controller]")] + public class StatusController : ControllerBase + { + + IServiceProvider serviceProvider; + + public StatusController(IServiceProvider sp) + { + serviceProvider = sp; + } + + [Route("")] + [Route("int")] + [HttpGet] + public IEnumerable GetInt() + { + var rng = new Random(); + var grpc = serviceProvider.GetRequiredService(); + if (!grpc.IsInitilized) + grpc.Initialize(); + return new int[] { rng.Next() }; + } + } +} diff --git a/ASS.Server/Web/Startup.cs b/ASS.Server/Web/Startup.cs new file mode 100644 index 0000000..6d82d49 --- /dev/null +++ b/ASS.Server/Web/Startup.cs @@ -0,0 +1,48 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using System; +using System.Collections.Generic; +using System.Text; + +namespace ASS.Server.Web +{ + 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(); + } + + // 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.UseDeveloperExceptionPage(); + } + + app.UseHttpsRedirection(); + + app.UseRouting(); + + //app.UseAuthorization(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllers(); + }); + } + } +} diff --git a/ASS.Server/defaultConfig.json b/ASS.Server/defaultConfig.json index 274bca8..fff3283 100644 --- a/ASS.Server/defaultConfig.json +++ b/ASS.Server/defaultConfig.json @@ -1,20 +1,36 @@ { "Logging": { "LogLevel": { - "Default": "Debug" + "Default": "Debug", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" } }, "GRPC": { - "Port": 5001, + "Port": 5002, "Host": "0.0.0.0" }, + "Kestrel": { + "Limits": { + "MaxConcurrentConnections": 100, + "MaxConcurrentUpgradedConnections": 100 + }, + "EndPoints": { + "Http": { + "Url": "http://0.0.0.0:5000" + }, + "Https": { + "Url": "https://0.0.0.0:5001" + } + } + }, "Repository": { "URL": "https://github.com/Aurorastation/Aurora.3.git", "Branch": "", "Author": { "Name": "Aurora Server System", "Email": "ass@aurorastation.org" - } + }, "Credentials": { "github.com/Aurorastation/Aurora.3.git": {} } @@ -22,7 +38,7 @@ "BYOND": { "Version": { "Major": 512, - "Minor": 1467 + "Minor": 1488 }, "Dir": "BYOND" },