From 47b8f189572fba60bc4357af1316e7c5fdd44298 Mon Sep 17 00:00:00 2001 From: Lukas Millard Date: Tue, 14 May 2024 17:07:00 +0100 Subject: [PATCH] add example odata API to recreate #3198 --- src/.dockerignore | 30 +++++++++++++++ .../AspNetODataWithMarten.csproj | 20 ++++++++++ .../Controllers/WeatherForecastController.cs | 17 +++++++++ src/AspNetODataWithMarten/Dockerfile | 24 ++++++++++++ src/AspNetODataWithMarten/Program.cs | 38 +++++++++++++++++++ .../Properties/launchSettings.json | 14 +++++++ src/docker-compose.dcproj | 16 ++++++++ src/docker-compose.yml | 23 +++++++++++ src/launchSettings.json | 11 ++++++ 9 files changed, 193 insertions(+) create mode 100644 src/.dockerignore create mode 100644 src/AspNetODataWithMarten/AspNetODataWithMarten.csproj create mode 100644 src/AspNetODataWithMarten/Controllers/WeatherForecastController.cs create mode 100644 src/AspNetODataWithMarten/Dockerfile create mode 100644 src/AspNetODataWithMarten/Program.cs create mode 100644 src/AspNetODataWithMarten/Properties/launchSettings.json create mode 100644 src/docker-compose.dcproj create mode 100644 src/docker-compose.yml create mode 100644 src/launchSettings.json diff --git a/src/.dockerignore b/src/.dockerignore new file mode 100644 index 0000000000..fe1152bdb8 --- /dev/null +++ b/src/.dockerignore @@ -0,0 +1,30 @@ +**/.classpath +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/azds.yaml +**/bin +**/charts +**/docker-compose* +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +LICENSE +README.md +!**/.gitignore +!.git/HEAD +!.git/config +!.git/packed-refs +!.git/refs/heads/** \ No newline at end of file diff --git a/src/AspNetODataWithMarten/AspNetODataWithMarten.csproj b/src/AspNetODataWithMarten/AspNetODataWithMarten.csproj new file mode 100644 index 0000000000..58e60e3242 --- /dev/null +++ b/src/AspNetODataWithMarten/AspNetODataWithMarten.csproj @@ -0,0 +1,20 @@ + + + + net8.0 + enable + enable + Linux + ..\docker-compose.dcproj + + + + + + + + + + + + diff --git a/src/AspNetODataWithMarten/Controllers/WeatherForecastController.cs b/src/AspNetODataWithMarten/Controllers/WeatherForecastController.cs new file mode 100644 index 0000000000..165ce419f0 --- /dev/null +++ b/src/AspNetODataWithMarten/Controllers/WeatherForecastController.cs @@ -0,0 +1,17 @@ +using Marten; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.OData.Query; +using Microsoft.AspNetCore.OData.Routing.Controllers; + +namespace AspNetODataWithMarten.Controllers; + +[Route("/weatherforecast")] +public class WeatherForecastController : ODataController +{ + [HttpGet] + [EnableQuery] + public IActionResult Get([FromServices] IDocumentSession session ) + { + return Ok(session.Query()); + } +} diff --git a/src/AspNetODataWithMarten/Dockerfile b/src/AspNetODataWithMarten/Dockerfile new file mode 100644 index 0000000000..2cb0f32aec --- /dev/null +++ b/src/AspNetODataWithMarten/Dockerfile @@ -0,0 +1,24 @@ +#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging. + +FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base +USER app +WORKDIR /app +EXPOSE 5045 + +FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build +ARG BUILD_CONFIGURATION=Release +WORKDIR /src +COPY ["AspNetODataWithMarten/AspNetODataWithMarten.csproj", "AspNetODataWithMarten/"] +RUN dotnet restore "./AspNetODataWithMarten/AspNetODataWithMarten.csproj" +COPY . . +WORKDIR "/src/AspNetODataWithMarten" +RUN dotnet build "./AspNetODataWithMarten.csproj" -c $BUILD_CONFIGURATION -o /app/build + +FROM build AS publish +ARG BUILD_CONFIGURATION=Release +RUN dotnet publish "./AspNetODataWithMarten.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false + +FROM base AS final +WORKDIR /app +COPY --from=publish /app/publish . +ENTRYPOINT ["dotnet", "AspNetODataWithMarten.dll"] diff --git a/src/AspNetODataWithMarten/Program.cs b/src/AspNetODataWithMarten/Program.cs new file mode 100644 index 0000000000..38dab869a2 --- /dev/null +++ b/src/AspNetODataWithMarten/Program.cs @@ -0,0 +1,38 @@ +using Marten; +using Microsoft.AspNetCore.OData; +using Microsoft.OData.ModelBuilder; +using System.ComponentModel.DataAnnotations; + +var builder = WebApplication.CreateBuilder(args); + +builder.Services.AddMarten((options) => +{ + options.Connection(Environment.GetEnvironmentVariable("POSTGRES_CONNECTION_STRING")); + options.RegisterDocumentType(); + options.DatabaseSchemaName = "public"; +}); + +builder.Services.AddControllers().AddOData(options => +{ + var entityBuilder = new ODataModelBuilder(); + + entityBuilder.EntityType().HasKey(t => t.Id); + entityBuilder.EntitySet("WeatherForecast"); + + options.EnableQueryFeatures().AddRouteComponents("odata", entityBuilder.GetEdmModel()); +}); + +var app = builder.Build(); + +app.MapControllers(); + +app.Run(); + +public record WeatherForecast() +{ + public Guid Id { get; init; } + public DateOnly Date { get; init; } + public int Temperature { get; init; } + public string? Summary { get; init; } + public decimal? Humidity { get; init; } +} diff --git a/src/AspNetODataWithMarten/Properties/launchSettings.json b/src/AspNetODataWithMarten/Properties/launchSettings.json new file mode 100644 index 0000000000..430eb641af --- /dev/null +++ b/src/AspNetODataWithMarten/Properties/launchSettings.json @@ -0,0 +1,14 @@ +{ + "profiles": { + "Container (Dockerfile)": { + "commandName": "Docker", + "launchBrowser": fa, + "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/weatherforecast", + "environmentVariables": { + "ASPNETCORE_HTTP_PORTS": "8080" + }, + "publishAllPorts": true + } + }, + "$schema": "http://json.schemastore.org/launchsettings.json" +} diff --git a/src/docker-compose.dcproj b/src/docker-compose.dcproj new file mode 100644 index 0000000000..0f38737a0c --- /dev/null +++ b/src/docker-compose.dcproj @@ -0,0 +1,16 @@ + + + + 2.1 + Linux + False + 0f9f373e-a47a-4028-aaeb-04e2fe4e7f24 + LaunchBrowser + {Scheme}://localhost:{ServicePort}/weatherforecast + aspnetodatawithmarten + + + + + + \ No newline at end of file diff --git a/src/docker-compose.yml b/src/docker-compose.yml new file mode 100644 index 0000000000..e269ebf443 --- /dev/null +++ b/src/docker-compose.yml @@ -0,0 +1,23 @@ +version: '3.4' + +services: + aspnetodatawithmarten: + build: + context: . + dockerfile: AspNetODataWithMarten/Dockerfile + ports: + - "8080:8080" + environment: + ASPNETCORE_ENVIRONMENT: "Development" + ASPNETCORE_HTTP_PORTS: 8080 + POSTGRES_CONNECTION_STRING: "Host=postgres;Port=5432;Database=weatherforecast;Username=postgres;password=Password123!" + depends_on: + - postgres + + postgres: + image: postgres + environment: + POSTGRES_DB: "weatherforecast" + POSTGRES_PASSWORD: "Password123!" + ports: + - "5432:5432" diff --git a/src/launchSettings.json b/src/launchSettings.json new file mode 100644 index 0000000000..4393cca76a --- /dev/null +++ b/src/launchSettings.json @@ -0,0 +1,11 @@ +{ + "profiles": { + "Docker Compose": { + "commandName": "DockerCompose", + "commandVersion": "1.0", + "serviceActions": { + "aspnetodatawithmarten": "StartDebugging" + } + } + } +} \ No newline at end of file