Skip to content

Commit

Permalink
Minor progress adding another (users) service
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornnorgaard committed Jun 20, 2024
1 parent 46f2d77 commit 73fb63b
Show file tree
Hide file tree
Showing 20 changed files with 399 additions and 8 deletions.
24 changes: 19 additions & 5 deletions .env
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
# Postgres
POSTGRES_PASSWORD=mysecretpassword
POSTGRES_USER=todos
POSTGRES_DB=todos
POSTGRES_USER=postgres
POSTGRES_DB=postgres
POSTGRES_HOST=localhost
POSTGRES_PORT=5432

# Open Telemetry Collector
OTEL_COLLECTOR_HOST="http://otel-collector:4317"

# Services
DATABASE_HOST="postgres"
ENVIRONMENT_NAME="Docker"

# Todos API
ASPNETCORE_ENVIRONMENT="Docker"
ASPNETCORE_ENVIRONMENT=${ENVIRONMENT_NAME}
ServiceOptions__ServiceName="Todos API"
ServiceOptions__ConnectionString="Host=postgres; Database=todos; Username=todos; Password=mysecretpassword"
ServiceOptions__ConnectionString="Host=${DATABASE_HOST}; Database=todos; Username=${POSTGRES_USER}; Password=${POSTGRES_PASSWORD}"
ServiceOptions__HangfireEnabled="true"
ServiceOptions__TelemetryCollectorHost=${OTEL_COLLECTOR_HOST}

# Users API
ASPNETCORE_ENVIRONMENT=${ENVIRONMENT_NAME}
ServiceOptions__ServiceName="Users API"
ServiceOptions__ConnectionString="Host=postgres; Database=users; Username=${POSTGRES_USER}; Password=${POSTGRES_PASSWORD}"
ServiceOptions__HangfireEnabled="true"
ServiceOptions__TelemetryCollectorHost="http://otel-collector:4317"
ServiceOptions__TelemetryCollectorHost=${OTEL_COLLECTOR_HOST}
44 changes: 44 additions & 0 deletions .github/workflows/users.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Build Users Api

on:
push:
branches:
- "main"
paths:
- ".github/workflows/users.yml"
- "dockerfiles/users.Dockerfile"
- "src/**"
pull_request:
branches:
- "main"
paths:
- ".github/workflows/users.yml"
- "dockerfiles/users.Dockerfile"
- "src/**"

jobs:
dotnet-test:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./src/
steps:
- uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: Test
run: dotnet test --no-build --verbosity normal

docker-build:
runs-on: ubuntu-latest
needs: dotnet-test
steps:
- uses: actions/checkout@v2
- name: Build the Docker image
run: docker build -f dockerfiles/users.Dockerfile src
25 changes: 25 additions & 0 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,31 @@ services:
- "6001:8080"
env_file:
- .env
environment:
ASPNETCORE_ENVIRONMENT: ${ENVIRONMENT_NAME}
ServiceOptions__ServiceName: "Todos API"
ServiceOptions__ConnectionString: "Host=${DATABASE_HOST}; Database=todos; Username=${POSTGRES_USER}; Password=${POSTGRES_PASSWORD}"
ServiceOptions__HangfireEnabled: "true"
ServiceOptions__TelemetryCollectorHost: ${OTEL_COLLECTOR_HOST}
restart: on-failure
depends_on:
- postgres

users-api:
image: ursu/ant-users-api:latest
build:
context: src
dockerfile: ../dockerfiles/users.Dockerfile
ports:
- "6002:8080"
env_file:
- .env
environment:
ASPNETCORE_ENVIRONMENT: ${ENVIRONMENT_NAME}
ServiceOptions__ServiceName: "Users API"
ServiceOptions__ConnectionString: "Host=postgres; Database=users; Username=${POSTGRES_USER}; Password=${POSTGRES_PASSWORD}"
ServiceOptions__HangfireEnabled: "true"
ServiceOptions__TelemetryCollectorHost: ${OTEL_COLLECTOR_HOST}
restart: on-failure
depends_on:
- postgres
Expand Down
18 changes: 18 additions & 0 deletions dockerfiles/users.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /app

COPY *.sln ./
COPY */*.csproj ./

RUN for file in $(ls *.csproj); do mkdir -p ${file%.*}/ && mv $file ${file%.*}/; done
RUN dotnet restore

COPY . .
RUN dotnet build
RUN dotnet publish Ast.Users/Ast.Users.csproj -c Release -o out

FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime
WORKDIR /app

COPY --from=build /app/out .
ENTRYPOINT [ "dotnet", "Ast.Users.dll" ]
28 changes: 27 additions & 1 deletion src/Ast.Platform/ServiceCollectionExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ public static void AddPlatformServices(
services.AddPlatformHangfire(configuration);
}

public static WebApplicationBuilder AddPlatformServices(this WebApplicationBuilder builder)
{
var configuration = builder.Configuration;
configuration.ValidatePlatformConfiguration();
builder.Services.AddPlatformTelemetry(configuration);
builder.Services.AddPlatformHangfire(configuration);
builder.Services.AddCorsPolicy(configuration);
builder.Services.AddHealthChecks();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

return builder;
}

public static void UsePlatformServices(this IApplicationBuilder app, IConfiguration configuration)
{
app.UsePlatformTelemetry(configuration);
Expand All @@ -41,4 +55,16 @@ public static void UsePlatformServices(this IApplicationBuilder app, IConfigurat
endpoints.EnabledHangfireDashboard(configuration);
});
}
}

public static WebApplication MapPlatformServices(this WebApplication app)
{
var configuration = app.Configuration;
app.UsePlatformTelemetry(configuration);
app.UseCorsPolicy();
app.MapHealthChecks("/hc");
app.EnabledHangfireDashboard(configuration);
app.UseSwagger();
app.UseSwaggerUI();
return app;
}
}
2 changes: 1 addition & 1 deletion src/Ast.Todos/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, TodoCont
todoContext.Database.Migrate();
app.UsePlatformServices(Configuration);
}
}
}
2 changes: 1 addition & 1 deletion src/Ast.Todos/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"AllowedOrigins": "*",
"ServiceOptions": {
"ServiceName": "Todos API",
"ConnectionString": "Host=localhost; Database=todos; Username=todos; Password=mysecretpassword",
"ConnectionString": "Host=localhost; Database=todos; Username=postgres; Password=mysecretpassword",
"HangfireEnabled": true,
"TelemetryCollectorHost": "http://localhost:4317"
}
Expand Down
23 changes: 23 additions & 0 deletions src/Ast.Users/Ast.Users.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<InvariantGlobalization>true</InvariantGlobalization>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MediatR" Version="12.2.0"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.4"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Ast.Platform\Ast.Platform.csproj" />
</ItemGroup>

</Project>
14 changes: 14 additions & 0 deletions src/Ast.Users/Database/Configurations/UserConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Ast.Users.Database.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Ast.Users.Database.Configurations;

public class UserConfiguration : IEntityTypeConfiguration<User>
{
public void Configure(EntityTypeBuilder<User> todo)
{
todo.HasKey(t => t.Id);
todo.Property(t => t.Name).HasMaxLength(UserConstants.Name.MaxLength).IsRequired();
}
}
10 changes: 10 additions & 0 deletions src/Ast.Users/Database/Configurations/UserConstants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Ast.Users.Database.Configurations;

public static class UserConstants
{
public static class Name
{
public const int MaxLength = 250;
public const int MinLength = 1;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions src/Ast.Users/Database/Migrations/20240620214244_Initial.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Ast.Users.Database.Migrations
{
/// <inheritdoc />
public partial class Initial : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Users",
columns: table => new
{
Id = table.Column<Guid>(type: "uuid", nullable: false),
Name = table.Column<string>(type: "character varying(250)", maxLength: 250, nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Users", x => x.Id);
});
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Users");
}
}
}
43 changes: 43 additions & 0 deletions src/Ast.Users/Database/Migrations/UserContextModelSnapshot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// <auto-generated />
using System;
using Ast.Users.Database;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;

#nullable disable

namespace Ast.Users.Database.Migrations
{
[DbContext(typeof(UserContext))]
partial class UserContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "8.0.4")
.HasAnnotation("Relational:MaxIdentifierLength", 63);

NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);

modelBuilder.Entity("Ast.Users.Database.Models.User", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(250)
.HasColumnType("character varying(250)");
b.HasKey("Id");
b.ToTable("Users");
});
#pragma warning restore 612, 618
}
}
}
7 changes: 7 additions & 0 deletions src/Ast.Users/Database/Models/User.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Ast.Users.Database.Models;

public class User
{
public Guid Id { get; set; }
public string Name { get; set; }

Check warning on line 6 in src/Ast.Users/Database/Models/User.cs

View workflow job for this annotation

GitHub Actions / dotnet-test

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 6 in src/Ast.Users/Database/Models/User.cs

View workflow job for this annotation

GitHub Actions / dotnet-test

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 6 in src/Ast.Users/Database/Models/User.cs

View workflow job for this annotation

GitHub Actions / dotnet-test

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 6 in src/Ast.Users/Database/Models/User.cs

View workflow job for this annotation

GitHub Actions / dotnet-test

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
}
19 changes: 19 additions & 0 deletions src/Ast.Users/Database/UserContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Ast.Users.Database.Models;
using Microsoft.EntityFrameworkCore;

namespace Ast.Users.Database;

public class UserContext : DbContext
{
public DbSet<User> Users { get; init; }

public UserContext(DbContextOptions options) : base(options)
{
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfigurationsFromAssembly(typeof(UserContext).Assembly);
}
}
Loading

0 comments on commit 73fb63b

Please sign in to comment.