Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/OneScript/Application/ApplicationInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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("ИспользоватьКонсольЗаданий")]
Expand Down
64 changes: 64 additions & 0 deletions src/OneScript/Infrastructure/SwaggerServicePlugin.cs
Original file line number Diff line number Diff line change
@@ -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");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А что такое "Контакт" применительно к документации на API?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Это собственно контакт (выводится в шапке страницы), некая сущность с которой можно связаться, если у потребителя api возникнут вопросы.
Обычно там или контакты конкретного человека указывают или ИТ отдела.
Визуально выглядет так
image

Вообще вот тут можешь пощупать как это работает.
Там сразу по идее должен у тебя должно подтянутся демо описание проекта.
http://editor.swagger.io/


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<string>(name) ?? defaultValue;
}
}
}
1 change: 1 addition & 0 deletions src/OneScript/OneScriptWeb.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="2.1.2" />
<PackageReference Include="OneScript" Version="1.0.21" />
<PackageReference Include="OneScript.StandardLibrary" Version="1.0.21" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="4.0.1" />
</ItemGroup>
<ItemGroup>
<Folder Include="wwwroot\" />
Expand Down
1 change: 1 addition & 0 deletions src/OneScript/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down