From 01e277c3cd255e2ed69c5fc1fd9d13c3048723e8 Mon Sep 17 00:00:00 2001 From: thepra Date: Sun, 22 Nov 2020 10:21:01 +0100 Subject: [PATCH] Add project files. --- .dockerignore | 25 ++++++++++++++ Controllers/PokemonController.cs | 45 +++++++++++++++++++++++++ Dockerfile | 26 +++++++++++++++ Pokespearean.csproj | 19 +++++++++++ Pokespearean.sln | 31 +++++++++++++++++ Program.cs | 26 +++++++++++++++ Properties/launchSettings.json | 36 ++++++++++++++++++++ Startup.cs | 57 ++++++++++++++++++++++++++++++++ WeatherForecast.cs | 15 +++++++++ appsettings.Development.json | 9 +++++ appsettings.json | 10 ++++++ 11 files changed, 299 insertions(+) create mode 100644 .dockerignore create mode 100644 Controllers/PokemonController.cs create mode 100644 Dockerfile create mode 100644 Pokespearean.csproj create mode 100644 Pokespearean.sln create mode 100644 Program.cs create mode 100644 Properties/launchSettings.json create mode 100644 Startup.cs create mode 100644 WeatherForecast.cs create mode 100644 appsettings.Development.json create mode 100644 appsettings.json diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..3729ff0 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,25 @@ +**/.classpath +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/azds.yaml +**/bin +**/charts +**/docker-compose* +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +LICENSE +README.md \ No newline at end of file diff --git a/Controllers/PokemonController.cs b/Controllers/PokemonController.cs new file mode 100644 index 0000000..6cacdbd --- /dev/null +++ b/Controllers/PokemonController.cs @@ -0,0 +1,45 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; +using PokeApiNet; +using Pokespearean.Models.Generic; +using Pokespearean.Models.Pokemon; +using System; +using System.Linq; +using System.Threading.Tasks; + +namespace Pokespearean.Controllers +{ + [ApiController] + [Route("[controller]")] + public class PokemonController : ControllerBase + { + private readonly ILogger _logger; + + public PokemonController(ILogger logger) + { + _logger = logger; + } + + [Route("{pokemon}")] + public async Task Get([FromRoute] string pokemon = "ho-oh") + { + var result = new WebResult(); + try + { + var client = new PokeApiClient(); + + var pokedex = await client.GetResourceAsync(pokemon); + + return Ok(new PokeResult + { + Name = pokedex.Name, + Description = pokedex.Descriptions.FirstOrDefault(d => d.Language.Name == "en").Description + }); + } + catch (Exception ex) + { + return BadRequest(result.Invalidate(ex.Message, ex)); + } + } + } +} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..674a24d --- /dev/null +++ b/Dockerfile @@ -0,0 +1,26 @@ +#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging. + +#Depending on the operating system of the host machines(s) that will build or run the containers, the image specified in the FROM statement may need to be changed. +#For more information, please see https://aka.ms/containercompat + +FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base +USER 1001 +WORKDIR /app +EXPOSE 80 + +FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build +WORKDIR /src +COPY ["Pokespearean.csproj", ""] +COPY ["../Pokespearean.Models/Pokespearean.Models.csproj", "../Pokespearean.Models/"] +RUN dotnet restore "./Pokespearean.csproj" +COPY . . +WORKDIR "/src/." +RUN dotnet build "Pokespearean.csproj" -c Release -o /app/build + +FROM build AS publish +RUN dotnet publish "Pokespearean.csproj" -c Release -o /app/publish + +FROM base AS final +WORKDIR /app +COPY --from=publish /app/publish . +ENTRYPOINT ["dotnet", "Pokespearean.dll"] \ No newline at end of file diff --git a/Pokespearean.csproj b/Pokespearean.csproj new file mode 100644 index 0000000..b647ef1 --- /dev/null +++ b/Pokespearean.csproj @@ -0,0 +1,19 @@ + + + + net5.0 + Windows + . + + + + + + + + + + + + + diff --git a/Pokespearean.sln b/Pokespearean.sln new file mode 100644 index 0000000..bc09bab --- /dev/null +++ b/Pokespearean.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30711.63 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pokespearean", "Pokespearean.csproj", "{B9920ADA-7244-4E6A-8935-4EE1D66273D7}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pokespearean.Models", "..\Pokespearean.Models\Pokespearean.Models.csproj", "{B62A4320-ED4F-4B29-9CE7-56EC1EBA4A26}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {B9920ADA-7244-4E6A-8935-4EE1D66273D7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B9920ADA-7244-4E6A-8935-4EE1D66273D7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B9920ADA-7244-4E6A-8935-4EE1D66273D7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B9920ADA-7244-4E6A-8935-4EE1D66273D7}.Release|Any CPU.Build.0 = Release|Any CPU + {B62A4320-ED4F-4B29-9CE7-56EC1EBA4A26}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B62A4320-ED4F-4B29-9CE7-56EC1EBA4A26}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B62A4320-ED4F-4B29-9CE7-56EC1EBA4A26}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B62A4320-ED4F-4B29-9CE7-56EC1EBA4A26}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {41FC3C36-F6DC-430D-9C13-9CBA0D981604} + EndGlobalSection +EndGlobal diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..7b27465 --- /dev/null +++ b/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 Pokespearean +{ + 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/Properties/launchSettings.json b/Properties/launchSettings.json new file mode 100644 index 0000000..a62b6b7 --- /dev/null +++ b/Properties/launchSettings.json @@ -0,0 +1,36 @@ +{ + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:5000", + "sslPort": 0 + } + }, + "$schema": "http://json.schemastore.org/launchsettings.json", + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "Pokespearean": { + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "dotnetRunMessages": "true", + "applicationUrl": "http://localhost:5000" + }, + "Docker": { + "commandName": "Docker", + "launchBrowser": true, + "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}", + "publishAllPorts": true + } + } +} \ No newline at end of file diff --git a/Startup.cs b/Startup.cs new file mode 100644 index 0000000..b693671 --- /dev/null +++ b/Startup.cs @@ -0,0 +1,57 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text.Json; +using System.Threading.Tasks; + +namespace Pokespearean +{ + 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().AddJsonOptions(options => + { + //options.JsonSerializerOptions.MaxDepth = 0; + //options.JsonSerializerOptions.PropertyNameCaseInsensitive = true; + //options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; + options.JsonSerializerOptions.IgnoreReadOnlyFields = true; + options.JsonSerializerOptions.IgnoreReadOnlyProperties = true; + }); + } + + // 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.UseRouting(); + + app.UseAuthorization(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllers(); + }); + } + } +} diff --git a/WeatherForecast.cs b/WeatherForecast.cs new file mode 100644 index 0000000..f673bb6 --- /dev/null +++ b/WeatherForecast.cs @@ -0,0 +1,15 @@ +using System; + +namespace Pokespearean +{ + public class WeatherForecast + { + public DateTime Date { get; set; } + + public int TemperatureC { get; set; } + + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); + + public string Summary { get; set; } + } +} diff --git a/appsettings.Development.json b/appsettings.Development.json new file mode 100644 index 0000000..8983e0f --- /dev/null +++ b/appsettings.Development.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git a/appsettings.json b/appsettings.json new file mode 100644 index 0000000..d9d9a9b --- /dev/null +++ b/appsettings.json @@ -0,0 +1,10 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*" +}