-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
298 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>() | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |