Health check

This commit is contained in:
Julian
2022-02-12 14:48:49 +01:00
parent 0e1b585db6
commit 9fa602664f
10 changed files with 94 additions and 6 deletions

View File

@@ -22,11 +22,13 @@ namespace weddingapi.Controllers
this.repository = repository;
}
// GET /families
// GET /family
[HttpGet]
public async Task<IEnumerable<FamilyDto>> GetFamiliesAsync()
{
return (await repository.GetFamiliesAsync()).Select(family => family.AsDto());
var fams = (await repository.GetFamiliesAsync()).Select(family => family.AsDto());
Response.Headers.Add("X-Total-Count", fams.Count().ToString());
return fams;
}
// GET /family/{id}
@@ -52,6 +54,7 @@ namespace weddingapi.Controllers
Id = Guid.NewGuid(),
CreatedDate = DateTimeOffset.UtcNow,
Name = familyDto.Name,
Overnight = familyDto.Overnight,
Members = familyDto.Members.Select(person => new Person() {
IsChild = person.IsChild,
Name = person.Name,
@@ -80,6 +83,7 @@ namespace weddingapi.Controllers
Name = person.Name,
IsChild = person.IsChild,
}).ToList(),
Overnight = familyDto.Overnight,
Name = familyDto.Name,
};

View File

@@ -8,6 +8,8 @@ namespace weddingapi.Dtos
[Required]
public string Name { get; init; }
[Required]
public bool Overnight { get; set; }
[Required]
public IList<PersonDto> Members { get; init;}
}
}

View File

@@ -9,6 +9,8 @@ namespace weddingapi.Dtos
public string Name { get; init; }
public bool Overnight { get; init; }
public DateTimeOffset CreatedDate { get; set; }
public IList<PersonDto> Members { get; init;}

View File

@@ -9,5 +9,7 @@ namespace weddingapi.Dtos
public string Name { get; init; }
[Required]
public IList<PersonDto> Members { get; init;}
[Required]
public bool Overnight { get; set; }
}
}

View File

@@ -13,6 +13,8 @@ namespace weddingapi.Models
public IList<Person> Members { get; init; }
public bool Overnight { get; init; }
public Family()
{
this.Members = new List<Person>();

View File

@@ -1 +1,29 @@
https://www.youtube.com/watch?v=bgk8N_rx1F4
# Build docker image:
docker build -t weddingapi:v2 .
# Run weddingapi
docker run -d --rm --name weddingapi -p 8080:80 -e MongoDbConfig:Host=mongo -e MongoDbConfig:Port=27017 weddingapi:v2
# Run mongodb
docker run -d --rm --name mongo -p 27017:27017 -v mongodbdata:/data/db mongo
# Push new docker version to registry:
## Rename
docker image tag weddingapi:v2 registry.git.rismer.de/rismer/weddingapi:v2
docker image tag weddingapi:v2 registry.git.rismer.de/rismer/weddingapi:latest
## Push
docker push registry.git.rismer.de/rismer/weddingapi:v2

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
@@ -34,11 +35,11 @@ namespace weddingapi
{
BsonSerializer.RegisterSerializer(new GuidSerializer(BsonType.String));
BsonSerializer.RegisterSerializer(new DateTimeOffsetSerializer(BsonType.String));
var mongoDbConfig = Configuration.GetSection(nameof(MongoDbConfig)).Get<MongoDbConfig>();
services.AddSingleton<IMongoClient>(serviceProvider =>
{
var config = Configuration.GetSection(nameof(MongoDbConfig)).Get<MongoDbConfig>();
return new MongoClient(config.ConnectionString);
return new MongoClient(mongoDbConfig.ConnectionString);
});
services.AddSingleton<IFamilyRepository, MongoDBFamilyRepository>();
@@ -49,6 +50,13 @@ namespace weddingapi
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "weddingapi", Version = "v1" });
});
services.AddHealthChecks()
.AddMongoDb(mongoDbConfig.ConnectionString,
name: "mongodb",
timeout: TimeSpan.FromSeconds(3),
tags: new[] { "ready" });
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@@ -73,6 +81,14 @@ namespace weddingapi
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapHealthChecks("/health/ready", new HealthCheckOptions {
Predicate = (check) => check.Tags.Contains("ready")
});
endpoints.MapHealthChecks("/health/live", new HealthCheckOptions {
Predicate = (_) => false
});
});
}
}

View File

@@ -8,7 +8,7 @@
},
"AllowedHosts": "*",
"MongoDbConfig": {
"Host": "localhost",
"Host": "truenas",
"Port": "27017"
}
}

31
docker-compose.yml Normal file
View File

@@ -0,0 +1,31 @@
version: "3.9"
volumes:
mongodbdata:
name: "mongodbdata"
networks:
weddingNetwork:
services:
weddingapi:
container_name: "weddingapi"
image: "weddingapi:v3"
ports:
- "8080:80"
networks:
- "weddingNetwork"
environment:
- MongoDbConfig:Host=mongo
- MongoDbConfig:Port=27017
mongo:
container_name: mongo
image: "mongo"
ports:
- "27017:27017"
volumes:
- "mongodbdata:/data/db"
networks:
- "weddingNetwork"

View File

@@ -5,6 +5,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AspNetCore.HealthChecks.MongoDb" Version="6.0.1" />
<PackageReference Include="MongoDB.Driver" Version="2.14.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
</ItemGroup>