From 2859f5204d91ffb1bc693c047c550895acaa6ec8 Mon Sep 17 00:00:00 2001 From: Dmitriy Korolev Date: Mon, 12 Nov 2018 10:47:23 +0700 Subject: [PATCH 1/2] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D1=81=D0=B2=D0=B0=D0=B3=D0=B3=D0=B5=D1=80?= =?UTF-8?q?=D0=B0=20=D0=B2=20=D0=BF=D1=80=D0=BE=D0=B5=D0=BA=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Application/ApplicationInstance.cs | 9 +++ .../Infrastructure/SwaggerServicePlugin.cs | 64 +++++++++++++++++++ src/OneScript/OneScriptWeb.csproj | 1 + src/OneScript/Startup.cs | 1 + 4 files changed, 75 insertions(+) create mode 100644 src/OneScript/Infrastructure/SwaggerServicePlugin.cs diff --git a/src/OneScript/Application/ApplicationInstance.cs b/src/OneScript/Application/ApplicationInstance.cs index f65fda0..44a3190 100644 --- a/src/OneScript/Application/ApplicationInstance.cs +++ b/src/OneScript/Application/ApplicationInstance.cs @@ -184,6 +184,15 @@ public void UseResponseCompression() _startupBuilder.UseResponseCompression(); } + [ContextMethod("ИспользоватьДокументацию")] + public void UseSwagger() + { + _startupBuilder.UseSwagger(options => + { + options.PreSerializeFilters.Add((swaggerDoc, httpReq) => swaggerDoc.Host = httpReq.Host.Value); + }); + } + // TODO: // Включить управление консолью, когда будет готова архитектура ролей в целом. //[ContextMethod("ИспользоватьКонсольЗаданий")] diff --git a/src/OneScript/Infrastructure/SwaggerServicePlugin.cs b/src/OneScript/Infrastructure/SwaggerServicePlugin.cs new file mode 100644 index 0000000..364a3c4 --- /dev/null +++ b/src/OneScript/Infrastructure/SwaggerServicePlugin.cs @@ -0,0 +1,64 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Swashbuckle.AspNetCore.Swagger; + +namespace OneScript.WebHost.Infrastructure +{ + public static class SwaggerServicePlugin + { + public static void AddOneScriptSwagger(this IServiceCollection services, IConfiguration conf) + { + services.AddSwaggerGen(options => + { + var info = LoadFromConfig(conf); + options.SwaggerDoc(info.Version, info); + }); + } + + private static Info LoadFromConfig(IConfiguration config) + { + + var infoSection = config?.GetSection("Info"); + + Info info = new Info + { + Version = GetSectionValue(infoSection, "version", "v1"), + Title = GetSectionValue(infoSection, "name", "OneScipt API"), + Description = GetSectionValue(infoSection, "description"), + TermsOfService = GetSectionValue(infoSection, "terms") + }; + + var licSection = infoSection?.GetSection("License"); + + if (licSection != null) + { + License lic = new License + { + Name = GetSectionValue(licSection, "name"), + Url = GetSectionValue(licSection, "url") + }; + info.License = lic; + } + + var contactSection = infoSection?.GetSection("Contact"); + + if (contactSection != null) + { + Contact contact = new Contact + { + Name = GetSectionValue(contactSection, "name"), + Email = GetSectionValue(contactSection, "email"), + Url = GetSectionValue(contactSection, "url") + }; + info.Contact = contact; + } + + return info; + } + + private static string GetSectionValue(IConfigurationSection section, string name, string defaultValue = null) + { + return section?.GetValue(name) ?? defaultValue; + } + } +} diff --git a/src/OneScript/OneScriptWeb.csproj b/src/OneScript/OneScriptWeb.csproj index 3ae1480..c68d479 100644 --- a/src/OneScript/OneScriptWeb.csproj +++ b/src/OneScript/OneScriptWeb.csproj @@ -46,6 +46,7 @@ + diff --git a/src/OneScript/Startup.cs b/src/OneScript/Startup.cs index 71a7100..b518e6b 100644 --- a/src/OneScript/Startup.cs +++ b/src/OneScript/Startup.cs @@ -73,6 +73,7 @@ public void ConfigureServices(IServiceCollection services) .ConfigureApplicationPartManager(pm=>pm.FeatureProviders.Add(new ScriptedViewComponentFeatureProvider())); services.AddOneScript(); + services.AddOneScriptSwagger(Configuration); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. From e706537104aaeb69e049e7e39fe4e3249d3ae8e3 Mon Sep 17 00:00:00 2001 From: Dmitriy Korolev Date: Mon, 12 Nov 2018 11:00:34 +0700 Subject: [PATCH 2/2] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20UI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/OneScript/Application/ApplicationInstance.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/OneScript/Application/ApplicationInstance.cs b/src/OneScript/Application/ApplicationInstance.cs index 44a3190..93854f9 100644 --- a/src/OneScript/Application/ApplicationInstance.cs +++ b/src/OneScript/Application/ApplicationInstance.cs @@ -191,6 +191,13 @@ public void UseSwagger() { options.PreSerializeFilters.Add((swaggerDoc, httpReq) => swaggerDoc.Host = httpReq.Host.Value); }); + + //TODO: Get url and name from config + + _startupBuilder.UseSwaggerUI(options => + { + options.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"); + }); } // TODO: