diff --git a/src/OneScript/Application/ApplicationInstance.cs b/src/OneScript/Application/ApplicationInstance.cs index f65fda0..93854f9 100644 --- a/src/OneScript/Application/ApplicationInstance.cs +++ b/src/OneScript/Application/ApplicationInstance.cs @@ -184,6 +184,22 @@ public void UseResponseCompression() _startupBuilder.UseResponseCompression(); } + [ContextMethod("ИспользоватьДокументацию")] + public void UseSwagger() + { + _startupBuilder.UseSwagger(options => + { + 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: // Включить управление консолью, когда будет готова архитектура ролей в целом. //[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.