-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into dependabot/nuget/log4net-2.0.10
- Loading branch information
Showing
16 changed files
with
239 additions
and
207 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
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 |
---|---|---|
@@ -1,110 +1,86 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Security.Cryptography; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.EntityFrameworkCore; | ||
using tour_of_heroes_api.Models; | ||
using tour_of_heroes_api.Modesl; | ||
using System.Linq; | ||
|
||
namespace tour_of_heroes_api.Controllers | ||
{ | ||
[Route("api/[controller]")] | ||
[ApiController] | ||
public class HeroController : ControllerBase | ||
{ | ||
private readonly HeroContext _context; | ||
|
||
public HeroController(HeroContext context) | ||
private IHeroRepository _heroRepository; | ||
public HeroController(IHeroRepository heroRepository) | ||
{ | ||
_context = context; | ||
_heroRepository = heroRepository; | ||
} | ||
|
||
// GET: api/Hero | ||
[HttpGet] | ||
public async Task<ActionResult<IEnumerable<Hero>>> GetHeroes() | ||
public ActionResult<IEnumerable<Hero>> GetHeroes() | ||
{ | ||
// Just for demo purposes 🤓 | ||
var hash = MD5.Create(); | ||
|
||
return await _context.Heroes.ToListAsync(); | ||
var heroes = _heroRepository.GetAll(); | ||
return Ok(heroes); | ||
} | ||
|
||
// GET: api/Hero/5 | ||
[HttpGet("{id}")] | ||
public async Task<ActionResult<Hero>> GetHero(int id) | ||
[HttpGet("{id}")] | ||
public ActionResult<Hero> GetHero(int id) | ||
{ | ||
var hero = await _context.Heroes.FindAsync(id); | ||
var hero = _heroRepository.GetById(id); | ||
|
||
if (hero == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
return hero; | ||
return Ok(hero); | ||
} | ||
|
||
// PUT: api/Hero/5 | ||
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 | ||
[HttpPut("{id}")] | ||
public async Task<IActionResult> PutHero(int id, Hero hero) | ||
public ActionResult PutHero(int id, Hero hero) | ||
{ | ||
if (id != hero.Id) | ||
{ | ||
return BadRequest(); | ||
} | ||
|
||
_context.Entry(hero).State = EntityState.Modified; | ||
var heroToUpdate = _heroRepository.GetById(id); | ||
|
||
try | ||
if (heroToUpdate == null) | ||
{ | ||
await _context.SaveChangesAsync(); | ||
} | ||
catch (DbUpdateConcurrencyException) | ||
{ | ||
if (!HeroExists(id)) | ||
{ | ||
return NotFound(); | ||
} | ||
else | ||
{ | ||
throw; | ||
} | ||
return NotFound(); | ||
} | ||
|
||
heroToUpdate.Name = hero.Name; | ||
heroToUpdate.AlterEgo = hero.AlterEgo; | ||
heroToUpdate.Description = hero.Description; | ||
|
||
_heroRepository.Update(heroToUpdate); | ||
|
||
return NoContent(); | ||
|
||
} | ||
|
||
// POST: api/Hero | ||
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 | ||
[HttpPost] | ||
public async Task<ActionResult<Hero>> PostHero(Hero hero) | ||
public ActionResult<Hero> PostHero(Hero hero) | ||
{ | ||
_context.Heroes.Add(hero); | ||
await _context.SaveChangesAsync(); | ||
|
||
return CreatedAtAction(nameof(GetHero), new { id = hero.Id }, hero); | ||
_heroRepository.Add(hero); | ||
|
||
return Ok(hero); | ||
|
||
} | ||
|
||
// DELETE: api/Hero/5 | ||
[HttpDelete("{id}")] | ||
public async Task<IActionResult> DeleteHero(int id) | ||
public IActionResult DeleteHero(int id) | ||
{ | ||
var hero = await _context.Heroes.FindAsync(id); | ||
if (hero == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
_context.Heroes.Remove(hero); | ||
await _context.SaveChangesAsync(); | ||
_heroRepository.Delete(id); | ||
|
||
return NoContent(); | ||
} | ||
|
||
private bool HeroExists(int id) | ||
{ | ||
return _context.Heroes.Any(e => e.Id == id); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
namespace tour_of_heroes_api.Modesl | ||
namespace tour_of_heroes_api.Models | ||
{ | ||
public class Hero | ||
{ | ||
|
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 |
---|---|---|
@@ -1,5 +1,4 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using tour_of_heroes_api.Modesl; | ||
|
||
namespace tour_of_heroes_api.Models | ||
{ | ||
|
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 |
---|---|---|
@@ -1,26 +1,80 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using OpenTelemetry.Metrics; | ||
using OpenTelemetry.Resources; | ||
using OpenTelemetry.Trace; | ||
using tour_of_heroes_api.Models; | ||
|
||
namespace tour_of_heroes_api | ||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
// Add services to the container. | ||
builder.Services.AddScoped<IHeroRepository, HeroRepository>(); | ||
builder.Services.AddControllers();builder.Services.AddDbContext<HeroContext>(opt => opt.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"))); | ||
builder.Services.AddSwaggerGen(c => | ||
{ | ||
public class Program | ||
c.SwaggerDoc("v1", new() { Title = "tour_of_heroes_api", Version = "v1" }); | ||
}); | ||
|
||
// Open Telemetry configuration | ||
var tracingOtlpEndpoint = builder.Configuration["OTLP_ENDPOINT_URL"]; | ||
var otel = builder.Services.AddOpenTelemetry(); | ||
|
||
// Configure OpenTelemetry Resources with the application name | ||
otel.ConfigureResource(resource => resource | ||
.AddService(serviceName: builder.Environment.ApplicationName)); | ||
|
||
// Add Metrics for ASP.NET Core and our custom metrics and export to Prometheus | ||
otel.WithMetrics(metrics => metrics | ||
// Metrics provider from OpenTelemetry | ||
.AddAspNetCoreInstrumentation() | ||
//.AddMeter(greeterMeter.Name) | ||
// Metrics provides by ASP.NET Core in .NET 8 | ||
.AddMeter("Microsoft.AspNetCore.Hosting") | ||
.AddMeter("Microsoft.AspNetCore.Server.Kestrel") | ||
.AddPrometheusExporter()); | ||
|
||
// Add Tracing for ASP.NET Core and our custom ActivitySource and export to Jaeger | ||
otel.WithTracing(tracing => | ||
{ | ||
tracing.AddAspNetCoreInstrumentation(); | ||
tracing.AddHttpClientInstrumentation(); | ||
tracing.AddSqlClientInstrumentation(); | ||
//tracing.AddSource(greeterActivitySource.Name); | ||
if (tracingOtlpEndpoint != null) | ||
{ | ||
tracing.AddOtlpExporter(otlpOptions => | ||
{ | ||
otlpOptions.Endpoint = new Uri(tracingOtlpEndpoint); | ||
}); | ||
} | ||
else | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
CreateHostBuilder(args).Build().Run(); | ||
} | ||
|
||
public static IHostBuilder CreateHostBuilder(string[] args) => | ||
Host.CreateDefaultBuilder(args) | ||
.ConfigureWebHostDefaults(webBuilder => | ||
{ | ||
webBuilder.UseStartup<Startup>(); | ||
}); | ||
tracing.AddConsoleExporter(); | ||
} | ||
} | ||
}); | ||
|
||
// Add CORS policy | ||
builder.Services.AddCors(options => | ||
{ | ||
options.AddPolicy("CorsPolicy", | ||
builder => builder.AllowAnyOrigin() | ||
.AllowAnyMethod() | ||
.AllowAnyHeader()); | ||
}); | ||
|
||
var app = builder.Build(); | ||
|
||
app.UseCors("CorsPolicy"); | ||
|
||
app.UseSwagger(); | ||
app.UseSwaggerUI(); | ||
|
||
app.MapControllers(); | ||
|
||
// Configure the Prometheus scraping endpoint | ||
app.MapPrometheusScrapingEndpoint(); | ||
|
||
app.Run(); | ||
|
This file was deleted.
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
Oops, something went wrong.