Initial Commit

master
Alexander Sigler 3 years ago
commit 6de240c87a

3
.gitignore vendored

@ -0,0 +1,3 @@
AOC2021/bin
AOC2021/obj
.vs

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31515.178
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AOC2021", "AOC2021\AOC2021.csproj", "{1C97C7BD-E112-4CC4-B870-BCEE6146C707}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{1C97C7BD-E112-4CC4-B870-BCEE6146C707}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1C97C7BD-E112-4CC4-B870-BCEE6146C707}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1C97C7BD-E112-4CC4-B870-BCEE6146C707}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1C97C7BD-E112-4CC4-B870-BCEE6146C707}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {EEC2AC4B-D8B0-4F88-A296-52DB5EE4980D}
EndGlobalSection
EndGlobal

@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="6.2.3" />
</ItemGroup>
<ItemGroup>
<Folder Include="Models\Day1\" />
</ItemGroup>
</Project>

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup>
<ActiveDebugProfile>AOC2021</ActiveDebugProfile>
</PropertyGroup>
</Project>

@ -0,0 +1,51 @@
using AOC2021.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Linq;
namespace AOC2021.Controllers
{
[ApiController]
[Route("[controller]")]
public class AdventOfCodeController : ControllerBase
{
private readonly ILogger<AdventOfCodeController> _logger;
public AdventOfCodeController(ILogger<AdventOfCodeController> logger)
{
_logger = logger;
}
[HttpPost]
[Consumes("text/plain")]
[Route("day1")]
public AOCResponse Day(AOCVersion version, [FromBody] string input)
{
AOCRequest request = new AOCRequest() { Input = input, Version = version };
var splitRoute = this.ControllerContext.HttpContext.Request.Path.ToString().Split("/");
string requestedRoute = splitRoute[splitRoute.Length - 1]; //Get the last endpoint value.
return GetAOCDay(requestedRoute).ExecuteDay(request);
}
private AOCDay GetAOCDay(string route)
{
IAOCService day = null;
var type = typeof(AOCDay);
var types = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(p => type.IsAssignableFrom(p) && !p.IsInterface && !p.IsAbstract);
foreach (var x in types)
{
Console.WriteLine(x.Name.ToLower());
if (x.Name.ToLower() == route.ToLower())
{
day = (IAOCService)Activator.CreateInstance(x);
}
}
return (AOCDay) day;
}
}
}

@ -0,0 +1,23 @@
using AOC2021.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace AOC2021.Days
{
public class Day1 : AOCDay
{
protected override AOCResponse ExecutePartA()
{
Console.WriteLine(this._request.Input);
return null;
}
protected override AOCResponse ExecutePartB()
{
throw new NotImplementedException();
}
}
}

@ -0,0 +1,35 @@
using Microsoft.AspNetCore.Mvc.Formatters;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace AOC2021.Helper
{
public class TextPlainInputFormatter : InputFormatter
{
private const string ContentType = "text/plain";
public TextPlainInputFormatter()
{
SupportedMediaTypes.Add(ContentType);
}
public override async Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context)
{
var request = context.HttpContext.Request;
using (var reader = new StreamReader(request.Body))
{
var content = await reader.ReadToEndAsync();
return await InputFormatterResult.SuccessAsync(content);
}
}
public override bool CanRead(InputFormatterContext context)
{
var contentType = context.HttpContext.Request.ContentType;
return contentType.StartsWith(ContentType);
}
}
}

@ -0,0 +1,26 @@

using Microsoft.Extensions.Logging;
namespace AOC2021.Models
{
public abstract class AOCDay : IAOCService
{
protected AOCRequest _request;
protected ILogger<AOCDay> _logger;
public AOCResponse ExecuteDay(AOCRequest request)
{
_request = request;
switch (request.Version)
{
case AOCVersion.A:
return ExecutePartA();
case AOCVersion.B:
return ExecutePartB();
}
return new AOCResponse() { Answer = "AOCDay failed to get a response", Status = false };
}
protected abstract AOCResponse ExecutePartA();
protected abstract AOCResponse ExecutePartB();
}
}

@ -0,0 +1,15 @@
using System;
using System.Runtime.Serialization;
namespace AOC2021.Models
{
[DataContract]
[Serializable]
public class AOCRequest
{
[DataMember]
public AOCVersion Version { get; set; }
[DataMember]
public string Input { get; set; }
}
}

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
namespace AOC2021.Models
{
[DataContract]
[Serializable]
public class AOCResponse
{
[DataMember]
public bool Status { get; set; }
[DataMember]
public IEnumerable<string> Debug { get; set; }
[DataMember]
public string Answer { get; set; }
}
}

@ -0,0 +1,15 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Runtime.Serialization;
namespace AOC2021.Models
{
[JsonConverter(typeof(StringEnumConverter))]
public enum AOCVersion
{
[EnumMember(Value = "A")]
A,
[EnumMember(Value = "B")]
B
}
}

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace AOC2021.Models
{
public interface IAOCService
{
public AOCResponse ExecuteDay(AOCRequest request);
}
}

@ -0,0 +1,26 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace AOC2021
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}

@ -0,0 +1,30 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:60073",
"sslPort": 44390
}
},
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"AOC2021": {
"commandName": "Project",
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": "true",
"applicationUrl": "https://localhost:5001;http://localhost:5000"
}
}
}

@ -0,0 +1,65 @@
using AOC2021.Helper;
using AOC2021.Models;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace AOC2021
{
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();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "AOC2021", Version = "v1" });
});
services.AddControllers(o => o.InputFormatters.Insert(o.InputFormatters.Count, new TextPlainInputFormatter()));
services.AddSwaggerGenNewtonsoftSupport();
}
// 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.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "AOC2021 v1"));
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}

@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}

@ -0,0 +1,10 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
Loading…
Cancel
Save