Health check
This commit is contained in:
@@ -22,11 +22,13 @@ namespace weddingapi.Controllers
|
|||||||
this.repository = repository;
|
this.repository = repository;
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET /families
|
// GET /family
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public async Task<IEnumerable<FamilyDto>> GetFamiliesAsync()
|
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}
|
// GET /family/{id}
|
||||||
@@ -52,6 +54,7 @@ namespace weddingapi.Controllers
|
|||||||
Id = Guid.NewGuid(),
|
Id = Guid.NewGuid(),
|
||||||
CreatedDate = DateTimeOffset.UtcNow,
|
CreatedDate = DateTimeOffset.UtcNow,
|
||||||
Name = familyDto.Name,
|
Name = familyDto.Name,
|
||||||
|
Overnight = familyDto.Overnight,
|
||||||
Members = familyDto.Members.Select(person => new Person() {
|
Members = familyDto.Members.Select(person => new Person() {
|
||||||
IsChild = person.IsChild,
|
IsChild = person.IsChild,
|
||||||
Name = person.Name,
|
Name = person.Name,
|
||||||
@@ -80,6 +83,7 @@ namespace weddingapi.Controllers
|
|||||||
Name = person.Name,
|
Name = person.Name,
|
||||||
IsChild = person.IsChild,
|
IsChild = person.IsChild,
|
||||||
}).ToList(),
|
}).ToList(),
|
||||||
|
Overnight = familyDto.Overnight,
|
||||||
Name = familyDto.Name,
|
Name = familyDto.Name,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ namespace weddingapi.Dtos
|
|||||||
[Required]
|
[Required]
|
||||||
public string Name { get; init; }
|
public string Name { get; init; }
|
||||||
[Required]
|
[Required]
|
||||||
|
public bool Overnight { get; set; }
|
||||||
|
[Required]
|
||||||
public IList<PersonDto> Members { get; init;}
|
public IList<PersonDto> Members { get; init;}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -9,6 +9,8 @@ namespace weddingapi.Dtos
|
|||||||
|
|
||||||
public string Name { get; init; }
|
public string Name { get; init; }
|
||||||
|
|
||||||
|
public bool Overnight { get; init; }
|
||||||
|
|
||||||
public DateTimeOffset CreatedDate { get; set; }
|
public DateTimeOffset CreatedDate { get; set; }
|
||||||
|
|
||||||
public IList<PersonDto> Members { get; init;}
|
public IList<PersonDto> Members { get; init;}
|
||||||
|
|||||||
@@ -9,5 +9,7 @@ namespace weddingapi.Dtos
|
|||||||
public string Name { get; init; }
|
public string Name { get; init; }
|
||||||
[Required]
|
[Required]
|
||||||
public IList<PersonDto> Members { get; init;}
|
public IList<PersonDto> Members { get; init;}
|
||||||
|
[Required]
|
||||||
|
public bool Overnight { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -13,6 +13,8 @@ namespace weddingapi.Models
|
|||||||
|
|
||||||
public IList<Person> Members { get; init; }
|
public IList<Person> Members { get; init; }
|
||||||
|
|
||||||
|
public bool Overnight { get; init; }
|
||||||
|
|
||||||
public Family()
|
public Family()
|
||||||
{
|
{
|
||||||
this.Members = new List<Person>();
|
this.Members = new List<Person>();
|
||||||
|
|||||||
28
README.md
28
README.md
@@ -1 +1,29 @@
|
|||||||
|
|
||||||
https://www.youtube.com/watch?v=bgk8N_rx1F4
|
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
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
20
Startup.cs
20
Startup.cs
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.AspNetCore.HttpsPolicy;
|
using Microsoft.AspNetCore.HttpsPolicy;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
@@ -34,11 +35,11 @@ namespace weddingapi
|
|||||||
{
|
{
|
||||||
BsonSerializer.RegisterSerializer(new GuidSerializer(BsonType.String));
|
BsonSerializer.RegisterSerializer(new GuidSerializer(BsonType.String));
|
||||||
BsonSerializer.RegisterSerializer(new DateTimeOffsetSerializer(BsonType.String));
|
BsonSerializer.RegisterSerializer(new DateTimeOffsetSerializer(BsonType.String));
|
||||||
|
var mongoDbConfig = Configuration.GetSection(nameof(MongoDbConfig)).Get<MongoDbConfig>();
|
||||||
|
|
||||||
services.AddSingleton<IMongoClient>(serviceProvider =>
|
services.AddSingleton<IMongoClient>(serviceProvider =>
|
||||||
{
|
{
|
||||||
var config = Configuration.GetSection(nameof(MongoDbConfig)).Get<MongoDbConfig>();
|
return new MongoClient(mongoDbConfig.ConnectionString);
|
||||||
return new MongoClient(config.ConnectionString);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
services.AddSingleton<IFamilyRepository, MongoDBFamilyRepository>();
|
services.AddSingleton<IFamilyRepository, MongoDBFamilyRepository>();
|
||||||
@@ -49,6 +50,13 @@ namespace weddingapi
|
|||||||
{
|
{
|
||||||
c.SwaggerDoc("v1", new OpenApiInfo { Title = "weddingapi", Version = "v1" });
|
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.
|
// 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 =>
|
app.UseEndpoints(endpoints =>
|
||||||
{
|
{
|
||||||
endpoints.MapControllers();
|
endpoints.MapControllers();
|
||||||
|
|
||||||
|
endpoints.MapHealthChecks("/health/ready", new HealthCheckOptions {
|
||||||
|
Predicate = (check) => check.Tags.Contains("ready")
|
||||||
|
});
|
||||||
|
|
||||||
|
endpoints.MapHealthChecks("/health/live", new HealthCheckOptions {
|
||||||
|
Predicate = (_) => false
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
},
|
},
|
||||||
"AllowedHosts": "*",
|
"AllowedHosts": "*",
|
||||||
"MongoDbConfig": {
|
"MongoDbConfig": {
|
||||||
"Host": "localhost",
|
"Host": "truenas",
|
||||||
"Port": "27017"
|
"Port": "27017"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
31
docker-compose.yml
Normal file
31
docker-compose.yml
Normal 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"
|
||||||
@@ -5,6 +5,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="AspNetCore.HealthChecks.MongoDb" Version="6.0.1" />
|
||||||
<PackageReference Include="MongoDB.Driver" Version="2.14.1" />
|
<PackageReference Include="MongoDB.Driver" Version="2.14.1" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
Reference in New Issue
Block a user