Skip to content

Commit

Permalink
more handling of swagger and appinstights logging
Browse files Browse the repository at this point in the history
  • Loading branch information
peter committed Feb 21, 2024
1 parent 6b022e7 commit 5268c06
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 42 deletions.
27 changes: 14 additions & 13 deletions DoSomethingService/Controllers/v1/SomethingV1Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace DoSomethingService.Controllers.v1;

/// <summary>
///
/// This is an api to return some data in a made up senario
/// </summary>
[ApiController]
[Route("v1/something")]
Expand All @@ -12,33 +12,34 @@ public class SomethingV1Controller : ControllerBase
private readonly ILogger<SomethingV1Controller> _logger;

/// <summary>
///
/// Setting up the code so it can do things
/// </summary>
/// <param name="logger"></param>
/// <param name="logger">The logger so we know if things have been done</param>
public SomethingV1Controller(ILogger<SomethingV1Controller> logger)
{
_logger = logger;
}

/// <summary>
///
/// If you need to do something then, just tell it
/// what you want to do
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
/// <param name="whatToDo">The thing you want it to do</param>
/// <returns>What happened</returns>
[HttpGet("DoSomething")]
public string Get(string input)
public string Get(string whatToDo)
{
return $"This was passed to me {input}, there I did something";
return $"You wanted me to {whatToDo}, there I did something";
}

/// <summary>
///
/// If you want to post something for it to do
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
/// <param name="whatToDo">The thing you want it to do</param>
/// <returns>What happened</returns>
[HttpPost("PostSomething")]
public string Post(string input)
public string Post(string whatToDo)
{
return $"This was passed to me {input}, there I did something";
return $"This was passed to me {whatToDo}, there I did something";
}
}
1 change: 1 addition & 0 deletions DoSomethingService/DoSomethingService.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.1" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.21.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

Expand Down
56 changes: 28 additions & 28 deletions DoSomethingService/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

var myAllowSpecificOrigins = "_myAllowSpecificOrigins";
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddApplicationInsightsTelemetry();

var apiVersions = new Dictionary<string, string>();

apiVersions.Add("v1", "SomethingV1");
apiVersions.Add("v2", "SomethingV2");
builder.Services.AddCors(options =>
Expand All @@ -18,42 +22,38 @@
builder.Services.AddControllers();

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
/*options =>
{
/*options.SwaggerDoc("v1", new OpenApiInfo
builder.Services.AddSwaggerGen(
options =>
{
Version = "v1",
Title = "The DoSomething API"
options.SwaggerDoc("v1", new OpenApiInfo
{
Version = "v1",
Title = "The DoSomething API"
});
/*options.SwaggerDoc("v2", new OpenApiInfo
{
Version = "v2",
Title = "The DoSomething API"
});*/
options.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
});
options.SwaggerDoc("v2", new OpenApiInfo
{
Version = "v2",
Title = "The DoSomething API"
});#1#
//options.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
});*/

var app = builder.Build();

app.UseSwagger();
app.UseSwaggerUI();
app.UseSwaggerUI(options =>
{
//options.SwaggerEndpoint($"/v1/Something/swagger/swagger.json", "SomethingV1");
//options.SwaggerEndpoint($"/swagger/v2/Something/swagger.json", "SomethingV2");

/*options =>*/
/*{
options.SwaggerEndpoint($"/v1/Something/swagger/swagger.json", "SomethingV1");
options.SwaggerEndpoint($"/swagger/v2/Something/swagger.json", "SomethingV2");
}*/
/*options =>
// build a swagger endpoint for each discovered API version
foreach (var description in apiVersions)
{
// build a swagger endpoint for each discovered API version
foreach (var description in apiVersions)
{
var name = description.Value.ToUpperInvariant();
var url = $"/swagger/{description.Key}/Something/swagger.json";
options.SwaggerEndpoint(url, name);
}
}*/
var name = description.Value.ToUpperInvariant();
var url = $"/swagger/{description.Key}/Something/swagger.json";
options.SwaggerEndpoint(url, name);
}
});

app.UseHttpsRedirection();
app.UseCors(myAllowSpecificOrigins);
Expand Down
5 changes: 4 additions & 1 deletion DoSomethingService/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"ApplicationInsights":{
"ConnectionString": "InstrumentationKey=6bd44586-aeec-47ae-9056-0019ea08f8f9;IngestionEndpoint=https://centralus-0.in.applicationinsights.azure.com/;LiveEndpoint=https://centralus.livediagnostics.monitor.azure.com/"
}
}

0 comments on commit 5268c06

Please sign in to comment.