forked from asyncapi/saunter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'asyncapi#196-Publish-a-global-dotnet-tool' into asyncap…
…i#197-more-logging # Conflicts: # Saunter.sln # examples/StreetlightsAPI/StreetlightsAPI.csproj # src/AsyncAPI.Saunter.Generator.Cli/ToFile/ServiceProviderBuilder.cs # src/AsyncAPI.Saunter.Generator.Cli/readme.md # test/AsyncAPI.Saunter.Generator.Cli.Tests/AsyncAPI.Saunter.Generator.Cli.Tests.csproj # test/AsyncAPI.Saunter.Generator.Cli.Tests/IntegrationTests.cs
- Loading branch information
Showing
19 changed files
with
336 additions
and
51 deletions.
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,4 @@ | ||
specs/ | ||
streetlights.json | ||
streetlights.yml | ||
streetlights.yaml |
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,79 @@ | ||
using System.Linq; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
using NLog; | ||
using NLog.Web; | ||
using Saunter; | ||
using Saunter.AsyncApiSchema.v2; | ||
using StreetlightsAPI; | ||
|
||
LogManager.Setup().LoadConfigurationFromAppSettings(); | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
builder.Logging.AddSimpleConsole(console => console.SingleLine = true); | ||
builder.Host.UseNLog(); | ||
|
||
// Add Saunter to the application services. | ||
builder.Services.AddAsyncApiSchemaGeneration(options => | ||
{ | ||
options.AssemblyMarkerTypes = [typeof(StreetlightMessageBus)]; | ||
|
||
options.Middleware.UiTitle = "Streetlights API"; | ||
|
||
options.AsyncApi = new AsyncApiDocument | ||
{ | ||
Info = new Info("Streetlights API", "1.0.0") | ||
{ | ||
Description = "The Smartylighting Streetlights API allows you to remotely manage the city lights.", | ||
License = new License("Apache 2.0") | ||
{ | ||
Url = "https://www.apache.org/licenses/LICENSE-2.0" | ||
} | ||
}, | ||
Servers = | ||
{ | ||
["mosquitto"] = new Server("test.mosquitto.org", "mqtt"), | ||
["webapi"] = new Server("localhost:5000", "http"), | ||
}, | ||
}; | ||
}); | ||
|
||
builder.Services.AddScoped<IStreetlightMessageBus, StreetlightMessageBus>(); | ||
builder.Services.AddControllers(); | ||
|
||
var app = builder.Build(); | ||
|
||
app.UseDeveloperExceptionPage(); | ||
|
||
app.UseRouting(); | ||
app.UseCors(configure => configure.AllowAnyOrigin().AllowAnyMethod()); | ||
|
||
// to be fixed with issue #173 | ||
#pragma warning disable ASP0014 // Suggest using top level route registrations instead of UseEndpoints | ||
app.UseEndpoints(endpoints => | ||
{ | ||
endpoints.MapAsyncApiDocuments(); | ||
endpoints.MapAsyncApiUi(); | ||
|
||
endpoints.MapControllers(); | ||
}); | ||
#pragma warning restore ASP0014 // Suggest using top level route registrations instead of UseEndpoints | ||
|
||
await app.StartAsync(); | ||
|
||
// Print the AsyncAPI doc location | ||
var logger = app.Services.GetService<ILoggerFactory>().CreateLogger<Program>(); | ||
var options = app.Services.GetService<IOptions<AsyncApiOptions>>(); | ||
var addresses = app.Urls; | ||
logger.LogInformation("AsyncAPI doc available at: {URL}", $"{addresses.FirstOrDefault()}{options.Value.Middleware.Route}"); | ||
logger.LogInformation("AsyncAPI UI available at: {URL}", $"{addresses.FirstOrDefault()}{options.Value.Middleware.UiBaseRoute}"); | ||
|
||
// Redirect base url to AsyncAPI UI | ||
app.Map("/", () => Results.Redirect("index.html")); | ||
app.Map("/index.html", () => Results.Redirect(options.Value.Middleware.UiBaseRoute)); | ||
|
||
await app.WaitForShutdownAsync(); |
63 changes: 63 additions & 0 deletions
63
examples/StreetlightsAPI.TopLevelStatement/StreetlightsAPI.TopLevelStatement.csproj
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,63 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<!-- This project is targeting .NET8 intentionally (The 'old school' Startup-class project is .NET6), to prove that | ||
the AsyncAPI.Saunter.Generator.Cli tool can generate specs for projects targetting .NET6 and .NET8. --> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<IsPackable>false</IsPackable> | ||
|
||
<!-- Example settings for "AsyncAPI.Saunter.Generator.Build", they are all option though --> | ||
<AsyncAPIGenerateDocumentsOnBuild>true</AsyncAPIGenerateDocumentsOnBuild> | ||
<AsyncAPIDocumentFormats>json,yml</AsyncAPIDocumentFormats> | ||
<AsyncAPIDocumentFilename>streetlights.{extension}</AsyncAPIDocumentFilename> | ||
<AsyncAPIDocumentOutputPath>specs</AsyncAPIDocumentOutputPath> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' "> | ||
<DocumentationFile>bin\Debug\StreetlightsAPI.TopLevelStatement.xml</DocumentationFile> | ||
<NoWarn>1701;1702;1591</NoWarn> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' "> | ||
<DocumentationFile>bin\Release\StreetlightsAPI.TopLevelStatement.xml</DocumentationFile> | ||
<NoWarn>1701;1702;1591</NoWarn> | ||
</PropertyGroup> | ||
|
||
<!-- Get the latest local build nuget package from source code, from the local nuget packages source as configured in nuget.config | ||
999.* are the 'special' version number for local builds --> | ||
<ItemGroup Condition=" '$(Configuration)' == 'Debug' "> | ||
<PackageReference Include="AsyncAPI.Saunter.Generator.Build" Version="999.*"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<!-- For release: use a real published nuget package --> | ||
<ItemGroup Condition=" '$(Configuration)' == 'Release' "> | ||
<PackageReference Include="AsyncAPI.Saunter.Generator.Build" Version="*-*"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Saunter\Saunter.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Folder Include="specs\" /> | ||
|
||
<Compile Include="../StreetlightsAPI/API.cs" /> | ||
<Compile Include="../StreetlightsAPI/Messaging.cs" /> | ||
|
||
<None Include="../StreetlightsAPI/nlog.config"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="NLog" Version="5.3.2" /> | ||
<PackageReference Include="NLog.Web.AspNetCore" Version="5.3.11" /> | ||
</ItemGroup> | ||
|
||
</Project> |
19 changes: 19 additions & 0 deletions
19
examples/StreetlightsAPI.TopLevelStatement/appsettings.json
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,19 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft": "Warning", | ||
"Microsoft.Hosting.Lifetime": "Information" | ||
} | ||
}, | ||
|
||
"AllowedHosts": "*", | ||
|
||
"Kestrel": { | ||
"EndPoints": { | ||
"Http": { | ||
"Url": "http://localhost:5001" | ||
} | ||
} | ||
} | ||
} |
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
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,23 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
autoReload="true"> | ||
|
||
<variable name="BaseDir" value="./" /> | ||
|
||
<!-- the targets to write to --> | ||
<targets> | ||
<!-- File Target for all log messages with basic details --> | ||
<target xsi:type="File" name="allfile" fileName="${BaseDir}/streetlights.nlog" | ||
layout="${longdate}|${level:uppercase=true}|${activity:property=TraceId}|${logger}|${message} ${exception:format=tostring}" | ||
maxArchiveFiles="5" archiveAboveSize="104857600" archiveNumbering="DateAndSequence" archiveDateFormat="yyyy-MM-dd" archiveFileName="${BaseDir}/streetlights-{#}.nlog" /> | ||
|
||
<!--Console Target for hosting lifetime messages to improve Docker / Visual Studio startup detection --> | ||
<target xsi:type="Console" name="lifetimeConsole" layout="${MicrosoftConsoleLayout}" /> | ||
</targets> | ||
|
||
<!-- rules to map from logger name to target --> | ||
<rules> | ||
<logger name="*" minlevel="Debug" writeTo="allfile" /> | ||
</rules> | ||
</nlog> |
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 |
---|---|---|
|
@@ -15,3 +15,5 @@ | |
var app = ConsoleApp.Create(); | ||
app.Add<ToFileCommand>(); | ||
app.Run(args); | ||
|
||
Environment.ExitCode = 0; |
22 changes: 22 additions & 0 deletions
22
src/AsyncAPI.Saunter.Generator.Cli/ToFile/DependencyResolver.cs
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 @@ | ||
using System.Reflection; | ||
|
||
namespace AsyncAPI.Saunter.Generator.Cli.ToFile; | ||
|
||
internal static class DependencyResolver | ||
{ | ||
public static void Init(string startupAssemblyBasePath) | ||
{ | ||
AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => | ||
{ | ||
var requestedAssembly = new AssemblyName(args.Name); | ||
var fullPath = Path.Combine(startupAssemblyBasePath, $"{requestedAssembly.Name}.dll"); | ||
if (File.Exists(fullPath)) | ||
{ | ||
var assembly = Assembly.LoadFile(fullPath); | ||
return assembly; | ||
} | ||
Console.WriteLine($"Could not resolve assembly: {args.Name}, requested by {args.RequestingAssembly?.FullName}"); | ||
return default; | ||
}; | ||
} | ||
} |
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
Oops, something went wrong.