Update to NetCore 3.1, added Kestrel for REST api and few basic endpoints.

This commit is contained in:
Karolis2011
2020-01-04 14:42:57 +02:00
parent 402d1943c1
commit 0e06afd5e6
8 changed files with 171 additions and 53 deletions

View File

@@ -2,21 +2,25 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Emet.FileSystems" Version="0.0.1-alpha2" />
<PackageReference Include="Grpc" Version="2.23.0" />
<PackageReference Include="LibGit2Sharp" Version="0.26.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.2.4" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.2.0" />
<PackageReference Include="Emet.FileSystems" Version="0.0.1" />
<PackageReference Include="Grpc" Version="2.26.0" />
<PackageReference Include="LibGit2Sharp" Version="0.26.2" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.0" />
</ItemGroup>
<ItemGroup>

View File

@@ -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<ILogger<GrpcService>>()));
var grpc = services.GetRequiredService<GrpcService>();
grpc.Initialize();
await Task.Delay(-1);
}
}
private ServiceProvider ConfigureServices(string[] args)
{
// Stage 1 services
var services = new ServiceCollection()
.AddSingleton<IConfiguration>(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<InstanceService>()
.AddSingleton<ByondService>()
.AddSingleton<HttpClient>()
)
.ConfigureWebHostDefaults(webBuilder =>
{
c.ClearProviders();
c.AddConsole();
c.AddConfiguration(serviceProvider.GetRequiredService<IConfiguration>().GetSection("Logging"));
webBuilder.UseStartup<Startup>();
})
.AddSingleton(sp => new GrpcService(sp))
.AddSingleton<InstanceService>()
.AddSingleton<ByondService>()
.AddSingleton<HttpClient>();
;
return services.BuildServiceProvider();
}
;
}
}

View File

@@ -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/";

View File

@@ -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<IConfiguration>())
{
@@ -29,6 +30,7 @@ namespace ASS.Server.Services
public void Initialize()
{
IsInitilized = true;
Services.Add(API.Instance.BindService(_sp.GetRequiredService<InstanceService>()));
Start();
logger.LogInformation($"gRPC server listening on {config["GRPC:Host"]}:{config["GRPC:Port"]}");

View File

@@ -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<int> InstallVersionAsync(int major, int minor)
{
await byondService.SwitchToVersion(new API.ByondVersion() { Major = major, Minor = minor });
return 0;
}
}
}

View File

@@ -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<int> GetInt()
{
var rng = new Random();
var grpc = serviceProvider.GetRequiredService<GrpcService>();
if (!grpc.IsInitilized)
grpc.Initialize();
return new int[] { rng.Next() };
}
}
}

48
ASS.Server/Web/Startup.cs Normal file
View File

@@ -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();
});
}
}
}

View File

@@ -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"
},