-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
79b78aa
commit 4a6b53b
Showing
10 changed files
with
225 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
|
||
<TargetFrameworks>net8.0;net9.0</TargetFrameworks> | ||
|
||
<LangVersion>13.0</LangVersion> | ||
<Nullable>enable</Nullable> | ||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
|
||
<AssemblyVersion>9.4.0.0</AssemblyVersion> | ||
<FileVersion>9.4.0.0</FileVersion> | ||
<Version>9.4.0</Version> | ||
|
||
<Authors>Andreas Nägeli</Authors> | ||
<Company/> | ||
|
||
<PackageLicenseFile>LICENSE</PackageLicenseFile> | ||
<PackageProjectUrl>https://genhttp.org/</PackageProjectUrl> | ||
|
||
<Description>Serves Swagger UI for debugging purposes.</Description> | ||
<PackageTags>HTTP Webserver C# Module Swagger UI</PackageTags> | ||
|
||
<PublishRepositoryUrl>true</PublishRepositoryUrl> | ||
<IncludeSymbols>true</IncludeSymbols> | ||
<SymbolPackageFormat>snupkg</SymbolPackageFormat> | ||
|
||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
<NoWarn>CS1591,CS1587,CS1572,CS1573</NoWarn> | ||
|
||
<PackageIcon>icon.png</PackageIcon> | ||
|
||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
|
||
<None Include="..\..\LICENSE" Pack="true" PackagePath="\"/> | ||
<None Include="..\..\Resources\icon.png" Pack="true" PackagePath="\"/> | ||
|
||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
|
||
<ProjectReference Include="..\..\API\GenHTTP.Api.csproj"/> | ||
|
||
<ProjectReference Include="..\IO\GenHTTP.Modules.IO.csproj" /> | ||
|
||
<ProjectReference Include="..\Layouting\GenHTTP.Modules.Layouting.csproj"/> | ||
|
||
<ProjectReference Include="..\Pages\GenHTTP.Modules.Pages.csproj" /> | ||
|
||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All"/> | ||
|
||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Remove="Resources\Static\swagger-ui.css" /> | ||
<EmbeddedResource Include="Resources\Static\swagger-ui.css" /> | ||
<None Remove="Resources\Static\swagger-ui-bundle.js" /> | ||
<EmbeddedResource Include="Resources\Static\swagger-ui-bundle.js" /> | ||
<None Remove="Resources\Static\swagger-ui-standalone-preset.js" /> | ||
<None Remove="Resources\Templates\Index.html" /> | ||
<EmbeddedResource Include="Resources\Templates\Index.html" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using Cottle; | ||
using GenHTTP.Api.Content; | ||
using GenHTTP.Api.Protocol; | ||
using GenHTTP.Modules.Basics; | ||
using GenHTTP.Modules.IO; | ||
using GenHTTP.Modules.Pages; | ||
using GenHTTP.Modules.Pages.Rendering; | ||
|
||
namespace GenHTTP.Modules.Swagger.Handler; | ||
|
||
public sealed class SwaggerUIHandler : IHandler | ||
{ | ||
|
||
#region Get-/Setters | ||
|
||
public IHandler StaticResources { get; } | ||
|
||
public TemplateRenderer Template { get; } | ||
|
||
public string Url { get; } | ||
|
||
#endregion | ||
|
||
#region Initialization | ||
|
||
public SwaggerUIHandler(string? url) | ||
{ | ||
StaticResources = Resources.From(ResourceTree.FromAssembly("Resources.Static")) | ||
.Build(); | ||
|
||
Template = Renderer.From(Resource.FromAssembly("Index.html").Build()); | ||
|
||
Url = url ?? "../openapi.json"; | ||
} | ||
|
||
#endregion | ||
|
||
#region Functionality | ||
|
||
public ValueTask PrepareAsync() => ValueTask.CompletedTask; | ||
|
||
public async ValueTask<IResponse?> HandleAsync(IRequest request) | ||
{ | ||
if (!request.HasType(RequestMethod.Get, RequestMethod.Head)) | ||
{ | ||
throw new ProviderException(ResponseStatus.MethodNotAllowed, "Only GET requests are allowed by this handler", (b) => b.Header("Allow", "GET")); | ||
} | ||
|
||
if (request.Target.Ended) | ||
{ | ||
var config = new Dictionary<Value, Value> | ||
{ | ||
["url"] = Url | ||
}; | ||
|
||
var content = await Template.RenderAsync(config); | ||
|
||
return request.GetPage(content) | ||
.Build(); | ||
} | ||
|
||
if (request.Target.Current?.Value == "static") | ||
{ | ||
request.Target.Advance(); | ||
return await StaticResources.HandleAsync(request); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
#endregion | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using GenHTTP.Api.Content; | ||
|
||
namespace GenHTTP.Modules.Swagger.Handler; | ||
|
||
public sealed class SwaggerUIHandlerBuilder : IHandlerBuilder<SwaggerUIHandlerBuilder> | ||
{ | ||
private readonly List<IConcernBuilder> _Concerns = []; | ||
|
||
private string? _Url; | ||
|
||
public SwaggerUIHandlerBuilder Url(string url) | ||
{ | ||
_Url = url; | ||
return this; | ||
} | ||
|
||
public SwaggerUIHandlerBuilder Add(IConcernBuilder concern) | ||
{ | ||
_Concerns.Add(concern); | ||
return this; | ||
} | ||
|
||
public IHandler Build() | ||
{ | ||
return Concerns.Chain(_Concerns, new SwaggerUIHandler(_Url)); | ||
} | ||
|
||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
<meta name="description" content="SwaggerUI" /> | ||
<title>SwaggerUI</title> | ||
<link rel="stylesheet" href="./static/swagger-ui.css" /> | ||
</head> | ||
<body> | ||
<div id="swagger-ui"></div> | ||
<script src="./static/swagger-ui-bundle.js"></script> | ||
<script> | ||
window.onload = () => \{ | ||
window.ui = SwaggerUIBundle(\{ | ||
url: '{url}', | ||
dom_id: '#swagger-ui' | ||
\}); | ||
\}; | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using GenHTTP.Modules.Layouting.Provider; | ||
using GenHTTP.Modules.Swagger.Handler; | ||
|
||
namespace GenHTTP.Modules.Swagger; | ||
|
||
public static class SwaggerUI | ||
{ | ||
|
||
public static SwaggerUIHandlerBuilder Create() => new(); | ||
|
||
public static LayoutBuilder AddSwaggerUI(this LayoutBuilder layout, string segment = "swagger", string? url = null) | ||
=> layout.Add(segment, (url != null) ? Create().Url(url) : Create()); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters