diff --git a/DockerStudyApi/.dockerignore b/DockerStudyApi/.dockerignore new file mode 100644 index 0000000..3729ff0 --- /dev/null +++ b/DockerStudyApi/.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/DockerStudyApi/DockerStudyApi.sln b/DockerStudyApi/DockerStudyApi.sln new file mode 100644 index 0000000..f936c5b --- /dev/null +++ b/DockerStudyApi/DockerStudyApi.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31911.196 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DockerStudyApi", "DockerStudyApi\DockerStudyApi.csproj", "{969D5123-0168-4A1D-998B-E3AD43E77B69}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {969D5123-0168-4A1D-998B-E3AD43E77B69}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {969D5123-0168-4A1D-998B-E3AD43E77B69}.Debug|Any CPU.Build.0 = Debug|Any CPU + {969D5123-0168-4A1D-998B-E3AD43E77B69}.Release|Any CPU.ActiveCfg = Release|Any CPU + {969D5123-0168-4A1D-998B-E3AD43E77B69}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {776B8329-57DF-4139-9E90-F6388D3F1160} + EndGlobalSection +EndGlobal diff --git a/DockerStudyApi/DockerStudyApi/Config/TestOption.cs b/DockerStudyApi/DockerStudyApi/Config/TestOption.cs new file mode 100644 index 0000000..8aa548e --- /dev/null +++ b/DockerStudyApi/DockerStudyApi/Config/TestOption.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace DockerStudyApi +{ + public class TestOption + { + public string AppId { get; set; } + public string AppName { get; set; } + + public string AppDesc { get; set; } + + public Version AppVersion { get; set; } + } +} diff --git a/DockerStudyApi/DockerStudyApi/Controllers/DemoController.cs b/DockerStudyApi/DockerStudyApi/Controllers/DemoController.cs new file mode 100644 index 0000000..90c7a6b --- /dev/null +++ b/DockerStudyApi/DockerStudyApi/Controllers/DemoController.cs @@ -0,0 +1,50 @@ +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace DockerStudyApi.Controllers +{ + [Route("api/[controller]/[action]")] + [ApiController] + public class DemoController : ControllerBase + { + private readonly ILogger _logger; + private readonly IOptions _tespOption; + + public DemoController(ILogger logger,IOptions tespOption) + { + _logger = logger; + _tespOption = tespOption; + } + + [HttpGet] + public IActionResult Test() + { + return Ok(); + } + + [HttpGet] + public TestOption TestOption() + { + return _tespOption.Value; + } + + [HttpGet] + public IActionResult Evn(string envName) + { + return Ok(); + } + + [HttpGet] + public IActionResult Info() + { + return Ok(); + } + } +} diff --git a/DockerStudyApi/DockerStudyApi/DockerStudyApi.csproj b/DockerStudyApi/DockerStudyApi/DockerStudyApi.csproj new file mode 100644 index 0000000..5774d7d --- /dev/null +++ b/DockerStudyApi/DockerStudyApi/DockerStudyApi.csproj @@ -0,0 +1,18 @@ + + + + net5.0 + Linux + + + + + + + + + + + + + diff --git a/DockerStudyApi/DockerStudyApi/Dockerfile b/DockerStudyApi/DockerStudyApi/Dockerfile new file mode 100644 index 0000000..a77bcbc --- /dev/null +++ b/DockerStudyApi/DockerStudyApi/Dockerfile @@ -0,0 +1,21 @@ +#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging. + +FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base +WORKDIR /app +EXPOSE 80 + +FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build +WORKDIR /src +COPY ["DockerStudyApi/DockerStudyApi.csproj", "DockerStudyApi/"] +RUN dotnet restore "DockerStudyApi/DockerStudyApi.csproj" +COPY . . +WORKDIR "/src/DockerStudyApi" +RUN dotnet build "DockerStudyApi.csproj" -c Release -o /app/build + +FROM build AS publish +RUN dotnet publish "DockerStudyApi.csproj" -c Release -o /app/publish + +FROM base AS final +WORKDIR /app +COPY --from=publish /app/publish . +ENTRYPOINT ["dotnet", "DockerStudyApi.dll"] \ No newline at end of file diff --git a/DockerStudyApi/DockerStudyApi/Program.cs b/DockerStudyApi/DockerStudyApi/Program.cs new file mode 100644 index 0000000..3ee6e10 --- /dev/null +++ b/DockerStudyApi/DockerStudyApi/Program.cs @@ -0,0 +1,27 @@ +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 DockerStudyApi +{ + 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/DockerStudyApi/DockerStudyApi/Properties/launchSettings.json b/DockerStudyApi/DockerStudyApi/Properties/launchSettings.json new file mode 100644 index 0000000..4af9c36 --- /dev/null +++ b/DockerStudyApi/DockerStudyApi/Properties/launchSettings.json @@ -0,0 +1,37 @@ +{ + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:37705", + "sslPort": 0 + } + }, + "$schema": "http://json.schemastore.org/launchsettings.json", + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "DockerStudyApi": { + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "dotnetRunMessages": "true", + "applicationUrl": "http://localhost:5000" + }, + "Docker": { + "commandName": "Docker", + "launchBrowser": true, + "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger", + "publishAllPorts": true + } + } +} \ No newline at end of file diff --git a/DockerStudyApi/DockerStudyApi/Startup.cs b/DockerStudyApi/DockerStudyApi/Startup.cs new file mode 100644 index 0000000..df237c6 --- /dev/null +++ b/DockerStudyApi/DockerStudyApi/Startup.cs @@ -0,0 +1,75 @@ +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 Microsoft.OpenApi.Models; + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using Newtonsoft.Json.Schema; +using Newtonsoft.Json.Converters; +using Newtonsoft.Json.Serialization; + +namespace DockerStudyApi +{ + 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.AddOptions(); + services.Configure(Configuration.GetSection("TestOption")); + + services.AddControllersWithViews() + .AddNewtonsoftJson(options => + { + options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; + options.SerializerSettings.Formatting = Formatting.Indented; + }) + //.AddJsonOptions(config=> + //{ + // config.JsonSerializerOptions.Encoder = System.Text.Encodings.Web.JavaScriptEncoder.Create(System.Text.Unicode.UnicodeRanges.All); + //}) + ; + services.AddSwaggerGen(c => + { + c.SwaggerDoc("v1", new OpenApiInfo { Title = "DockerStudyApi", Version = "v1" }); + }); + } + + // 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", "DockerStudyApi v1")); + } + + app.UseRouting(); + + app.UseAuthorization(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllers(); + }); + } + } +} diff --git a/DockerStudyApi/DockerStudyApi/appsettings.Development.json b/DockerStudyApi/DockerStudyApi/appsettings.Development.json new file mode 100644 index 0000000..8983e0f --- /dev/null +++ b/DockerStudyApi/DockerStudyApi/appsettings.Development.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git a/DockerStudyApi/DockerStudyApi/appsettings.json b/DockerStudyApi/DockerStudyApi/appsettings.json new file mode 100644 index 0000000..a543e33 --- /dev/null +++ b/DockerStudyApi/DockerStudyApi/appsettings.json @@ -0,0 +1,21 @@ +{ + "TestOption": { + "AppId": "WebApiDemo1", + "AppName": "示例WebApi", + "AppDesc": "Docker学习测试WebApi服务", + "AppVersion": { + "Major": 1, + "Minor": 5, + "Revision": 6, + "Build": 15236 + } + }, + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*" +}