From 6de240c87a260d27893dd221a515e7d8e29557e2 Mon Sep 17 00:00:00 2001 From: Alexander Sigler Date: Fri, 3 Dec 2021 14:49:26 -0800 Subject: [PATCH] Initial Commit --- .gitignore | 3 + AOC2021.sln | 25 +++++++ AOC2021/AOC2021.csproj | 17 +++++ AOC2021/AOC2021.csproj.user | 9 +++ AOC2021/Controllers/AdventOfCodeController.cs | 51 +++++++++++++++ AOC2021/Days/Day1.cs | 23 +++++++ AOC2021/Helper/TextPlainInputFormatter.cs | 35 ++++++++++ AOC2021/Models/AOCDay.cs | 26 ++++++++ AOC2021/Models/AOCRequest.cs | 15 +++++ AOC2021/Models/AOCResponse.cs | 19 ++++++ AOC2021/Models/AOCVersion.cs | 15 +++++ AOC2021/Models/IAOCService.cs | 12 ++++ AOC2021/Program.cs | 26 ++++++++ AOC2021/Properties/launchSettings.json | 30 +++++++++ AOC2021/Startup.cs | 65 +++++++++++++++++++ AOC2021/appsettings.Development.json | 9 +++ AOC2021/appsettings.json | 10 +++ 17 files changed, 390 insertions(+) create mode 100644 .gitignore create mode 100644 AOC2021.sln create mode 100644 AOC2021/AOC2021.csproj create mode 100644 AOC2021/AOC2021.csproj.user create mode 100644 AOC2021/Controllers/AdventOfCodeController.cs create mode 100644 AOC2021/Days/Day1.cs create mode 100644 AOC2021/Helper/TextPlainInputFormatter.cs create mode 100644 AOC2021/Models/AOCDay.cs create mode 100644 AOC2021/Models/AOCRequest.cs create mode 100644 AOC2021/Models/AOCResponse.cs create mode 100644 AOC2021/Models/AOCVersion.cs create mode 100644 AOC2021/Models/IAOCService.cs create mode 100644 AOC2021/Program.cs create mode 100644 AOC2021/Properties/launchSettings.json create mode 100644 AOC2021/Startup.cs create mode 100644 AOC2021/appsettings.Development.json create mode 100644 AOC2021/appsettings.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a8c0a80 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +AOC2021/bin +AOC2021/obj +.vs \ No newline at end of file diff --git a/AOC2021.sln b/AOC2021.sln new file mode 100644 index 0000000..de9a426 --- /dev/null +++ b/AOC2021.sln @@ -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 diff --git a/AOC2021/AOC2021.csproj b/AOC2021/AOC2021.csproj new file mode 100644 index 0000000..4a85dff --- /dev/null +++ b/AOC2021/AOC2021.csproj @@ -0,0 +1,17 @@ + + + + net5.0 + + + + + + + + + + + + + diff --git a/AOC2021/AOC2021.csproj.user b/AOC2021/AOC2021.csproj.user new file mode 100644 index 0000000..19372ed --- /dev/null +++ b/AOC2021/AOC2021.csproj.user @@ -0,0 +1,9 @@ + + + + ProjectDebugger + + + AOC2021 + + \ No newline at end of file diff --git a/AOC2021/Controllers/AdventOfCodeController.cs b/AOC2021/Controllers/AdventOfCodeController.cs new file mode 100644 index 0000000..8d23fca --- /dev/null +++ b/AOC2021/Controllers/AdventOfCodeController.cs @@ -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 _logger; + + public AdventOfCodeController(ILogger 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; + } + } +} diff --git a/AOC2021/Days/Day1.cs b/AOC2021/Days/Day1.cs new file mode 100644 index 0000000..9163e73 --- /dev/null +++ b/AOC2021/Days/Day1.cs @@ -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(); + } + } +} diff --git a/AOC2021/Helper/TextPlainInputFormatter.cs b/AOC2021/Helper/TextPlainInputFormatter.cs new file mode 100644 index 0000000..4250c20 --- /dev/null +++ b/AOC2021/Helper/TextPlainInputFormatter.cs @@ -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 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); + } + } +} diff --git a/AOC2021/Models/AOCDay.cs b/AOC2021/Models/AOCDay.cs new file mode 100644 index 0000000..54da9ff --- /dev/null +++ b/AOC2021/Models/AOCDay.cs @@ -0,0 +1,26 @@ + +using Microsoft.Extensions.Logging; + +namespace AOC2021.Models +{ + public abstract class AOCDay : IAOCService + { + protected AOCRequest _request; + protected ILogger _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(); + } +} diff --git a/AOC2021/Models/AOCRequest.cs b/AOC2021/Models/AOCRequest.cs new file mode 100644 index 0000000..551b574 --- /dev/null +++ b/AOC2021/Models/AOCRequest.cs @@ -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; } + } +} diff --git a/AOC2021/Models/AOCResponse.cs b/AOC2021/Models/AOCResponse.cs new file mode 100644 index 0000000..f9e9b7e --- /dev/null +++ b/AOC2021/Models/AOCResponse.cs @@ -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 Debug { get; set; } + [DataMember] + public string Answer { get; set; } + } +} diff --git a/AOC2021/Models/AOCVersion.cs b/AOC2021/Models/AOCVersion.cs new file mode 100644 index 0000000..bf0d0c3 --- /dev/null +++ b/AOC2021/Models/AOCVersion.cs @@ -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 + } +} diff --git a/AOC2021/Models/IAOCService.cs b/AOC2021/Models/IAOCService.cs new file mode 100644 index 0000000..b448243 --- /dev/null +++ b/AOC2021/Models/IAOCService.cs @@ -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); + } +} diff --git a/AOC2021/Program.cs b/AOC2021/Program.cs new file mode 100644 index 0000000..a3c944f --- /dev/null +++ b/AOC2021/Program.cs @@ -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(); + }); + } +} diff --git a/AOC2021/Properties/launchSettings.json b/AOC2021/Properties/launchSettings.json new file mode 100644 index 0000000..f636b9e --- /dev/null +++ b/AOC2021/Properties/launchSettings.json @@ -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" + } + } +} \ No newline at end of file diff --git a/AOC2021/Startup.cs b/AOC2021/Startup.cs new file mode 100644 index 0000000..955ba25 --- /dev/null +++ b/AOC2021/Startup.cs @@ -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(); + }); + } + + + } +} diff --git a/AOC2021/appsettings.Development.json b/AOC2021/appsettings.Development.json new file mode 100644 index 0000000..8983e0f --- /dev/null +++ b/AOC2021/appsettings.Development.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git a/AOC2021/appsettings.json b/AOC2021/appsettings.json new file mode 100644 index 0000000..d9d9a9b --- /dev/null +++ b/AOC2021/appsettings.json @@ -0,0 +1,10 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*" +}