Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/nuget/log4net-2.0.10
Browse files Browse the repository at this point in the history
  • Loading branch information
0GiS0 authored Nov 25, 2023
2 parents bf75ab3 + 6b947c4 commit 45a0c60
Show file tree
Hide file tree
Showing 16 changed files with 239 additions and 207 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM mcr.microsoft.com/devcontainers/dotnet:0-7.0
FROM mcr.microsoft.com/devcontainers/dotnet:8.0

# Install SQL Tools: SQLPackage and sqlcmd
COPY mssql/installSQLtools.sh installSQLtools.sh
Expand Down
7 changes: 5 additions & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"password": "P@ssword",
"emptyPasswordInput": false,
"savePassword": false,
"profileName": "mssql-container"
"profileName": "mssql-container",
"trustServerCertificate": true
}
]
},
Expand All @@ -36,7 +37,9 @@
"humao.rest-client",
"hashicorp.terraform",
"ms-azuretools.vscode-docker",
"redhat.vscode-yaml"
"redhat.vscode-yaml",
"GitHub.copilot",
"GitHub.copilot-chat"
]
}
},
Expand Down
5 changes: 3 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/bin/Debug/net7.0/tour-of-heroes-api.dll",
"program": "${workspaceFolder}/bin/Debug/net8.0/tour-of-heroes-api.dll",
"args": [],
"cwd": "${workspaceFolder}",
"stopAtEntry": false,
"serverReadyAction": {
"action": "openExternally",
"pattern": "\\bNow listening on:\\s+(https?://\\S+)"
"pattern": "\\bNow listening on:\\s+(https?://\\S+)",
"uriFormat": "%s/swagger"
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development",
Expand Down
82 changes: 29 additions & 53 deletions Controllers/HeroController.cs
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);
}
}
}
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
WORKDIR /app
EXPOSE 5000

Expand All @@ -9,7 +9,7 @@ ENV ASPNETCORE_URLS=http://+:5000
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser

FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build

ARG TARGETARCH
ARG TARGETOS
Expand Down
2 changes: 1 addition & 1 deletion Models/Hero.cs
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
{
Expand Down
1 change: 0 additions & 1 deletion Models/HeroContext.cs
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
{
Expand Down
96 changes: 75 additions & 21 deletions Program.cs
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();

70 changes: 0 additions & 70 deletions Startup.cs

This file was deleted.

9 changes: 1 addition & 8 deletions appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,5 @@
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"Kestrel": {
"Endpoints": {
"Http": {
"Url": "http://+:5000"
}
}
}
"AllowedHosts": "*"
}
Loading

0 comments on commit 45a0c60

Please sign in to comment.