From 49444107d018a3a55f832e39d946839d49721803 Mon Sep 17 00:00:00 2001 From: sis0k0 Date: Tue, 25 Aug 2026 11:36:07 +0300 Subject: [PATCH 1/2] Add Codespace lifecycle telemetry tracking Replicates the tracking setup from mongodb-developer/mean-stack-example: .devcontainer/track.sh plus postCreateCommand/postStartCommand hooks that best-effort report created/started events to the shared codespace-telemetry function. Appended with || true so telemetry can never break the dev environment. --- .devcontainer/devcontainer.json | 4 ++-- .devcontainer/track.sh | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 .devcontainer/track.sh diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 1653a13..16ccb69 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -27,6 +27,6 @@ "GamesDatabaseSettings__GamesCollectionName": "Games", "ASPNETCORE_ENVIRONMENT": "Development" }, - "postCreateCommand": "dotnet restore", - "postStartCommand": "echo 'Startup seed runs when the API starts (StartupBehaviorSettings:SeedOnStartup=true by default).'" + "postCreateCommand": "dotnet restore && bash /workspaces/mongodb-dotnet-example/.devcontainer/track.sh created || true", + "postStartCommand": "echo 'Startup seed runs when the API starts (StartupBehaviorSettings:SeedOnStartup=true by default).'; bash /workspaces/mongodb-dotnet-example/.devcontainer/track.sh started || true" } diff --git a/.devcontainer/track.sh b/.devcontainer/track.sh new file mode 100644 index 0000000..57ca4d2 --- /dev/null +++ b/.devcontainer/track.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash +# Best-effort telemetry: log a codespace lifecycle event. Never fails the caller. +EVENT="${1:-unknown}" +GIT_EMAIL="$(git config --get user.email 2>/dev/null || true)" +URL="https://us-central1-project-learning-fuel.cloudfunctions.net/trackCodespace" +curl -fsS -m 5 -X POST "$URL" \ + -H "Content-Type: application/json" \ + -d "{\"event\":\"$EVENT\",\"codespace\":\"${CODESPACE_NAME:-}\",\"user\":\"${GITHUB_USER:-}\",\"gitEmail\":\"${GIT_EMAIL:-}\",\"repository\":\"${GITHUB_REPOSITORY:-mongodb-developer/mongodb-dotnet-example}\"}" \ + >/dev/null 2>&1 || true From 20f4788584c4ec9b8b271792b10992fbe0da94a3 Mon Sep 17 00:00:00 2001 From: sis0k0 Date: Wed, 26 Aug 2026 13:20:17 +0300 Subject: [PATCH 2/2] Migrate to minimal hosting model, remove Startup.cs Consolidate ConfigureServices/Configure into Program.cs using the .NET minimal hosting APIs, matching current ASP.NET Core convention. --- Program.cs | 80 +++++++++++++++++++++++++++++++++++----------------- Startup.cs | 82 ------------------------------------------------------ 2 files changed, 54 insertions(+), 108 deletions(-) delete mode 100644 Startup.cs diff --git a/Program.cs b/Program.cs index 7940450..093fd70 100644 --- a/Program.cs +++ b/Program.cs @@ -1,26 +1,54 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; - -namespace mongodb_dotnet_example -{ - public class Program - { - public static void Main(string[] args) - { - CreateHostBuilder(args).Build().Run(); - } - - public static IHostBuilder CreateHostBuilder(string[] args) => - Host.CreateDefaultBuilder(args) - .ConfigureWebHostDefaults(webBuilder => - { - webBuilder.UseStartup(); - }); - } -} +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Options; +using Microsoft.OpenApi.Models; +using mongodb_dotnet_example.Models; +using mongodb_dotnet_example.Services; + +var builder = WebApplication.CreateBuilder(args); + +builder.Services.Configure(builder.Configuration.GetSection(nameof(GamesDatabaseSettings))); +builder.Services.Configure(builder.Configuration.GetSection(nameof(StartupBehaviorSettings))); + +builder.Services.AddSingleton(sp => sp.GetRequiredService>().Value); + +builder.Services.AddSingleton(); + +builder.Services.AddControllers(); +builder.Services.AddHealthChecks(); +builder.Services.AddSwaggerGen(c => +{ + c.SwaggerDoc("v1", new OpenApiInfo { Title = "mongodb_dotnet_example", Version = "v1" }); +}); + +var app = builder.Build(); + +if (app.Environment.IsDevelopment()) +{ + app.UseDeveloperExceptionPage(); +} + +app.UseSwagger(); +app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "mongodb_dotnet_example v1")); + +if (!app.Environment.IsDevelopment()) +{ + app.UseHttpsRedirection(); +} + +app.UseRouting(); + +app.UseAuthorization(); + +var startupBehaviorOptions = app.Services.GetRequiredService>(); +if (startupBehaviorOptions.Value.SeedOnStartup) +{ + var gamesService = app.Services.GetRequiredService(); + gamesService.SeedIfEmpty(GameSeedData.DefaultGames); +} + +app.MapControllers(); +app.MapHealthChecks("/health"); + +app.Run(); diff --git a/Startup.cs b/Startup.cs deleted file mode 100644 index b08bee8..0000000 --- a/Startup.cs +++ /dev/null @@ -1,82 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.HttpsPolicy; -using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; -using Microsoft.OpenApi.Models; -using mongodb_dotnet_example.Models; -using mongodb_dotnet_example.Services; - -namespace mongodb_dotnet_example -{ - public class Startup - { - public Startup(IConfiguration configuration) - { - Configuration = configuration; - } - - public IConfiguration Configuration { get; } - - // This method gets called by the runtime. Use this method to add services to the container. - public void ConfigureServices(IServiceCollection services) - { - services.Configure(Configuration.GetSection(nameof(GamesDatabaseSettings))); - services.Configure(Configuration.GetSection(nameof(StartupBehaviorSettings))); - - services.AddSingleton(sp => sp.GetRequiredService>().Value); - - services.AddSingleton(); - - services.AddControllers(); - services.AddHealthChecks(); - services.AddSwaggerGen(c => - { - c.SwaggerDoc("v1", new OpenApiInfo { Title = "mongodb_dotnet_example", Version = "v1" }); - }); - } - - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IOptions startupBehaviorOptions) - { - if (env.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); - - - } - - app.UseSwagger(); - app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "mongodb_dotnet_example v1")); - - if (!env.IsDevelopment()) - { - app.UseHttpsRedirection(); - } - - app.UseRouting(); - - app.UseAuthorization(); - - if (startupBehaviorOptions.Value.SeedOnStartup) - { - var gamesService = app.ApplicationServices.GetRequiredService(); - gamesService.SeedIfEmpty(GameSeedData.DefaultGames); - } - - app.UseEndpoints(endpoints => - { - endpoints.MapControllers(); - endpoints.MapHealthChecks("/health"); - }); - } - } -}