Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
9 changes: 9 additions & 0 deletions .devcontainer/track.sh
Original file line number Diff line number Diff line change
@@ -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
80 changes: 54 additions & 26 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -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<Startup>();
});
}
}
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<GamesDatabaseSettings>(builder.Configuration.GetSection(nameof(GamesDatabaseSettings)));
builder.Services.Configure<StartupBehaviorSettings>(builder.Configuration.GetSection(nameof(StartupBehaviorSettings)));

builder.Services.AddSingleton<IGamesDatabaseSettings>(sp => sp.GetRequiredService<IOptions<GamesDatabaseSettings>>().Value);

builder.Services.AddSingleton<IGamesService, GamesService>();

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<IOptions<StartupBehaviorSettings>>();
if (startupBehaviorOptions.Value.SeedOnStartup)
{
var gamesService = app.Services.GetRequiredService<IGamesService>();
gamesService.SeedIfEmpty(GameSeedData.DefaultGames);
}

app.MapControllers();
app.MapHealthChecks("/health");

app.Run();
82 changes: 0 additions & 82 deletions Startup.cs

This file was deleted.

Loading