Skip to content

Commit

Permalink
chore: run inMemory
Browse files Browse the repository at this point in the history
  • Loading branch information
atrakic committed Jun 8, 2024
1 parent f58f702 commit 4a75845
Show file tree
Hide file tree
Showing 12 changed files with 298 additions and 16 deletions.
18 changes: 2 additions & 16 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@

all: generate
dotnet sln add src/Message.DAL/Message.DAL.csproj
all:
dotnet restore
dotnet build --configuration Release --no-restore
dotnet test --no-restore --verbosity normal

generate:
./scripts/generate.sh

HELP_ARGS ?= config-help -g aspnetcore

help: ### make help HELP_ARGS=help
docker run --rm -v "${PWD}:/local" openapitools/openapi-generator-cli $(HELP_ARGS)

bootstrap:
dotnet new tool-manifest --force
dotnet tool install Microsoft.dotnet-openapi
dotnet tool list
dotnet run --project src/Echo.OpenAPI/Echo.OpenAPI.csproj
16 changes: 16 additions & 0 deletions src/Echo.OpenAPI/DAL/IMessageRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Echo.Model;

namespace Echo.DAL
{
public interface IMessageRepository
{
MessageItem? GetItem(int id);

IEnumerable<MessageItem> GetItems();

void AddItem(MessageItem item);
void UpdateItem(MessageItem item);
void RemoveItem(MessageItem item);
void SaveChanges();
}
}
16 changes: 16 additions & 0 deletions src/Echo.OpenAPI/DAL/MessageDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Echo.Model;

using Microsoft.EntityFrameworkCore;
using System;

namespace Echo.DAL
{
public class MessageDbContext : DbContext
{
public MessageDbContext(DbContextOptions<MessageDbContext> options) : base(options)
{
}

public DbSet<MessageItem> Items { get; set; }
}
}
62 changes: 62 additions & 0 deletions src/Echo.OpenAPI/DAL/MessageRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using Echo.Model;

using Microsoft.EntityFrameworkCore;
using System;
using System.Xml.Linq;

namespace Echo.DAL
{
public class MessageRepository : IMessageRepository
{
private readonly MessageDbContext context;
public MessageRepository(MessageDbContext context)
{
this.context = context;

if (context.Items.Any())
{
return;
}

var messageItems = new MessageItem[]
{
new MessageItem { Name = "Item1" },
new MessageItem { Name = "Item2" },
new MessageItem { Name = "Item3" },
};

context.Items.AddRange(messageItems);
context.SaveChanges();
}

public void AddItem(MessageItem item)
{
context.Items.Add(item);
}

public void UpdateItem(MessageItem item)
{
context.Items.Update(item);
}

public MessageItem? GetItem(int id)
{
return context.Items.FirstOrDefault(x => x.Id.Equals(id));
}

public IEnumerable<MessageItem> GetItems()
{
return context.Items.ToList();
}

public void RemoveItem(MessageItem item)
{
context.Items.Remove(item);
}

public void SaveChanges()
{
context.SaveChanges();
}
}
}
22 changes: 22 additions & 0 deletions src/Echo.OpenAPI/Echo.OpenAPI.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

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

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.6" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="8.0.6" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.6">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="NSwag.AspNetCore" Version="14.0.7" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions src/Echo.OpenAPI/Echo.OpenAPI.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@Echo.OpenAPI_HostAddress = http://localhost:5062

GET {{Echo.OpenAPI_HostAddress}}/weatherforecast/
Accept: application/json

###
8 changes: 8 additions & 0 deletions src/Echo.OpenAPI/Model/MessageItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Echo.Model
{
public class MessageItem
{
public int Id { get; private set; }
public string? Name { get; set; }
}
}
72 changes: 72 additions & 0 deletions src/Echo.OpenAPI/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using Echo.DAL;
using Echo.Model;

using NSwag.AspNetCore;

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Swashbuckle.AspNetCore.SwaggerGen;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddScoped<IMessageRepository, MessageRepository>();
builder.Services.AddDbContext<MessageDbContext>(o => o.UseInMemoryDatabase("MyMessageDb"));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddHealthChecks();
builder.Services.AddAuthorization();
builder.Services.AddAuthentication("Bearer").AddJwtBearer();

// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddOpenApiDocument(config =>
{
config.DocumentName = "EchoAPI";
config.Title = "EchoAPI v1";
config.Version = "v1";
config.Description = "A simple API to echo messages";

/**
config.AddSecurity("Bearer", new
{
Type = OpenApiSecuritySchemeType.Http,
Scheme = "bearer",
Bearer = new OpenApiSecurityScheme
{
Name = "Authorization",
Type = OpenApiSecuritySchemeType.ApiKey,
In = OpenApiSecurityApiKeyLocation.Header,
Description = "JWT Authorization header using the Bearer scheme."
}
});
*/
});

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI();
app.UseOpenApi();
}

app.MapHealthChecks("/healthz");
//app.UseHttpsRedirection();

app.MapGet("/api/messages", (IMessageRepository msgRepository) =>
{
return TypedResults.Ok(msgRepository.GetItems());
})
.WithName("GetMessageItems")
.WithOpenApi();


app.Run();
41 changes: 41 additions & 0 deletions src/Echo.OpenAPI/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:49237",
"sslPort": 44308
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5062",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7196;http://localhost:5062",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
25 changes: 25 additions & 0 deletions src/Echo.OpenAPI/Utilities/JWTSecurity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace Utilities;

using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.OpenApi.Models;

internal static class JWTSecurity
{
static OpenApiSecurityRequirement GetDefaultOpenApiSecurityRequirement()
=> new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = JwtBearerDefaults.AuthenticationScheme,
},
Scheme = SecuritySchemeType.Http.ToString(),
Name = JwtBearerDefaults.AuthenticationScheme,
},
new List<string>()
}
};
}
19 changes: 19 additions & 0 deletions src/Echo.OpenAPI/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"Authentication": {
"Schemes": {
"Bearer": {
"ValidAudiences": [
"http://localhost:5062",
"https://localhost:7196"
],
"ValidIssuer": "dotnet-user-jwts"
}
}
}
}
9 changes: 9 additions & 0 deletions src/Echo.OpenAPI/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}

0 comments on commit 4a75845

Please sign in to comment.