Compare commits

..

20 Commits

Author SHA1 Message Date
Developer 02
6dc8bd233b feat(ProxyController): Optionaler Körper als Dictionary<string, object> hinzugefügt 2025-01-30 15:11:38 +01:00
Developer 02
54fce2990b fix(ProxyController): Hinzufügen von Standard-Headern zur Antwort entfernt 2025-01-30 13:44:26 +01:00
Developer 02
3eed85e7de feat(ProxyController): Cookies und Header hinzugefügt 2025-01-30 13:34:04 +01:00
Developer 02
61bb5acdff refactor(ProxyController): Updated to add default headers 2025-01-30 12:10:30 +01:00
Developer 02
d8ee61d107 refactor(OriginServerParams): Umbenennung von Headers in DefaultHeaders. 2025-01-30 11:46:57 +01:00
Developer 02
7189b39d6b refactor(ProxyController): Aktualisiert, um JsonResult zu verwenden, wenn es einen Körper in der Redir-Methode gibt. 2025-01-30 11:39:18 +01:00
Developer 02
1fde6e7e34 feat(MockController): Erstellt, um einen echten Server zu simulieren 2025-01-30 11:10:41 +01:00
Developer 02
54eca6fceb feat(ProxyController): Umleitungsendpunkt für Post-Requests mit dem Muster „{ServicetierName}/ODataV4/{WebserviceName}_CreateInvoice“ implementiert 2025-01-30 10:20:00 +01:00
Developer 02
782680cda2 feat(CompanyRepo): Aktualisiert zur Verwendung von ApiDbContext 2025-01-30 00:49:36 +01:00
Developer 02
aeb1c4a810 chore: ef In-Memory-Datenbank hinzugefügt und Dienst hinzugefügt 2025-01-30 00:09:02 +01:00
Developer 02
67a4e91a1b feat(ApiDbContext): Mit Seed hinzugefügt 2025-01-30 00:06:58 +01:00
Developer 02
6ba7fe230a feat(CompaniesController): OData implementiert 2025-01-29 23:58:04 +01:00
Developer 02
9473ad7619 chore: Konfigurierte OData-Konvention Model Builder 2025-01-29 23:53:55 +01:00
Developer 02
c70257bb28 feat(Company): Entitäten und Repository erstellt 2025-01-29 23:52:21 +01:00
Developer 02
054527b4ba feat(MockAPI): Hinzufügen einer API zum Testen von Proxy-Servern.
- Swagger optional gemacht.
2025-01-29 18:17:45 +01:00
Developer 02
836b19df9e fix: Lösung in den Stammordner verschoben 2025-01-29 18:12:37 +01:00
Developer 02
27fb2af845 feat(controller): MetaController zum Abrufen der Parameter des Ursprungs-Servers hinzufügen 2025-01-29 17:50:19 +01:00
Developer 02
89b98af1be feat: Konfiguration für OriginServer-Parameter hinzufügen
- Die Klasse 'OriginServerParams' wurde hinzugefügt, um die URL und Header zu speichern.
- Konfigurationslogik implementiert, um den Abschnitt 'OriginServer' zu laden und sein Vorhandensein zu überprüfen.
2025-01-29 17:34:45 +01:00
Developer 02
c352ee987c refactor(Proxy): aktualisiert, um Proxy in der Produktion zu verwenden 2025-01-29 16:26:57 +01:00
Developer 02
590546c9f9 feat(Proxy): Initliazed .net api project. 2025-01-29 16:25:30 +01:00
23 changed files with 612 additions and 0 deletions

37
DigitalData.Swagger.sln Normal file
View File

@@ -0,0 +1,37 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34622.214
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DigitalData.Swagger.Proxy", "src\DigitalData.Swagger.Proxy\DigitalData.Swagger.Proxy.csproj", "{2CC25C4F-147E-4A64-9EA9-51C00BC7430E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{C5ED9B8C-5A36-440E-BF25-8FB0EC870924}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DigitalData.Swagger.MockAPI", "src\DigitalData.Swagger.MockAPI\DigitalData.Swagger.MockAPI.csproj", "{1D229E96-67BC-4717-B6E6-60A067BC498E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{2CC25C4F-147E-4A64-9EA9-51C00BC7430E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2CC25C4F-147E-4A64-9EA9-51C00BC7430E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2CC25C4F-147E-4A64-9EA9-51C00BC7430E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2CC25C4F-147E-4A64-9EA9-51C00BC7430E}.Release|Any CPU.Build.0 = Release|Any CPU
{1D229E96-67BC-4717-B6E6-60A067BC498E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1D229E96-67BC-4717-B6E6-60A067BC498E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1D229E96-67BC-4717-B6E6-60A067BC498E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1D229E96-67BC-4717-B6E6-60A067BC498E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{2CC25C4F-147E-4A64-9EA9-51C00BC7430E} = {C5ED9B8C-5A36-440E-BF25-8FB0EC870924}
{1D229E96-67BC-4717-B6E6-60A067BC498E} = {C5ED9B8C-5A36-440E-BF25-8FB0EC870924}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {071BB599-BE8A-4680-A008-35B0357D4899}
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,73 @@
using DigitalData.Swagger.MockAPI.Dtos;
using DigitalData.Swagger.MockAPI.Repos;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OData.Formatter;
using Microsoft.AspNetCore.OData.Query;
using Microsoft.AspNetCore.OData.Results;
namespace DigitalData.Swagger.MockAPI.Controllers;
[Route("api/[controller]")]
[ApiController]
public class CompaniesController(ICompanyRepo repo) : ControllerBase
{
private readonly ICompanyRepo _repo = repo;
[EnableQuery(PageSize = 3)]
[HttpGet]
public IQueryable<Company> Get()
{
return _repo.GetAll();
}
[EnableQuery]
[HttpGet("{id}")]
public SingleResult<Company> Get([FromODataUri] int key)
{
return SingleResult.Create(_repo.GetById(key));
}
[HttpPost]
public IActionResult Post([FromBody] Company company)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
_repo.Create(company);
return Created("companies", company);
}
[HttpPut]
public IActionResult Put([FromODataUri] int key, [FromBody] Company company)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (key != company.ID)
{
return BadRequest();
}
_repo.Update(company);
return NoContent();
}
[HttpDelete]
public IActionResult Delete([FromODataUri] int key)
{
var company = _repo.GetById(key);
if (company is null)
{
return BadRequest();
}
_repo.Delete(company.First());
return NoContent();
}
}

View File

@@ -0,0 +1,20 @@
using Microsoft.AspNetCore.Mvc;
namespace DigitalData.Swagger.MockAPI.Controllers
{
[ApiController]
public class MockController : ControllerBase
{
[HttpPost("{servicetierName}/ODataV4/{webserviceName}_CreateInvoice")]
public IActionResult CreateInvoice([FromRoute] string servicetierName, [FromRoute] string webserviceName, [FromQuery] string company, Dictionary<string, object>? body)
{
return Created($"{servicetierName}/ODataV4/{webserviceName}?id={1}", new { Id = 1, Foo = 1});
}
[HttpGet("{servicetierName}/ODataV4/{webserviceName}")]
public IActionResult GetInvoice([FromRoute] string servicetierName, [FromRoute] string webserviceName, [FromQuery] string id)
{
return Ok(new { Id = id, Foo = 1 });
}
}
}

View File

@@ -0,0 +1,11 @@
using DigitalData.Swagger.MockAPI.Dtos;
using Microsoft.EntityFrameworkCore;
namespace DigitalData.Swagger.MockAPI.Data;
public class ApiContext(DbContextOptions<ApiContext> options) : DbContext(options)
{
public DbSet<Company> Companies { get; set; }
public DbSet<Product> Products { get; set; }
}

View File

@@ -0,0 +1,75 @@
using DigitalData.Swagger.MockAPI.Dtos;
using System;
namespace DigitalData.Swagger.MockAPI.Data
{
public class DBSeeder
{
public static void AddCompaniesData(WebApplication app)
{
var scope = app.Services.CreateScope();
var db = scope.ServiceProvider.GetService<ApiContext>();
db.Companies.AddRange(new List<Company>()
{
new()
{
ID = 1,
Name = "TechCorp",
Size = 500,
Products = new List<Product>
{
new() { ID = 1, CompanyID = 1, Name = "Laptop X1", Price = 1200.99m },
new() { ID = 2, CompanyID = 1, Name = "Smartphone S2", Price = 799.49m }
}
},
new Company
{
ID = 2,
Name = "AutoWorks",
Size = 300,
Products = new List<Product>
{
new Product { ID = 3, CompanyID = 2, Name = "Electric Car E1", Price = 35000m },
new() { ID = 4, CompanyID = 2, Name = "Hybrid SUV H2", Price = 42000m }
}
},
new Company
{
ID = 3,
Name = "MediHealth",
Size = 200,
Products = new List<Product>
{
new() { ID = 5, CompanyID = 3, Name = "Vitamin Pack V10", Price = 19.99m },
new() { ID = 6, CompanyID = 3, Name = "Protein Shake P5", Price = 25.49m }
}
},
new Company
{
ID = 4,
Name = "HomeStyle",
Size = 150,
Products = new List<Product>
{
new() { ID = 7, CompanyID = 4, Name = "Smart Blender B3", Price = 89.99m },
new() { ID = 8, CompanyID = 4, Name = "Air Purifier A1", Price = 149.99m }
}
},
new Company
{
ID = 5,
Name = "GadgetZone",
Size = 400,
Products = new List<Product>
{
new() { ID = 9, CompanyID = 5, Name = "Wireless Earbuds W1", Price = 99.99m },
new() { ID = 10, CompanyID = 5, Name = "Smart Watch G5", Price = 199.99m }
}
}
});
db.SaveChanges();
}
}
}

View File

@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OData" Version="9.1.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="9.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,6 @@
@DigitalData.Swagger.MockAPI_HostAddress = http://localhost:5055
GET {{DigitalData.Swagger.MockAPI_HostAddress}}/weatherforecast/
Accept: application/json
###

View File

@@ -0,0 +1,9 @@
namespace DigitalData.Swagger.MockAPI.Dtos;
public class Company
{
public int ID { get; set; }
public string? Name { get; set; }
public int Size { get; set; }
public List<Product>? Products { get; set; }
}

View File

@@ -0,0 +1,9 @@
namespace DigitalData.Swagger.MockAPI.Dtos;
public class Product
{
public int ID { get; set; }
public int CompanyID { get; set; }
public string? Name { get; set; }
public decimal Price { get; set; }
}

View File

@@ -0,0 +1,51 @@
using DigitalData.Swagger.MockAPI.Data;
using DigitalData.Swagger.MockAPI.Dtos;
using DigitalData.Swagger.MockAPI.Repos;
using Microsoft.AspNetCore.OData;
using Microsoft.EntityFrameworkCore;
using Microsoft.OData.ModelBuilder;
var builder = WebApplication.CreateBuilder(args);
var config = builder.Configuration;
// Add services to the container.
builder.Services.AddDbContext<ApiContext>(opt => opt.UseInMemoryDatabase(databaseName: "CompaniesDB"));
builder.Services.AddScoped<ICompanyRepo, CompanyRepo>();
ODataConventionModelBuilder oDataModelbuilder = new();
oDataModelbuilder.EntitySet<Company>("Companies");
builder.Services.AddControllers()
.AddOData(options => options
.AddRouteComponents("odata", oDataModelbuilder.GetEdmModel())
.Select()
.Filter()
.OrderBy()
.SetMaxTop(20)
.Count()
.Expand()
);
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
DBSeeder.AddCompaniesData(app);
// Configure the HTTP request pipeline.
if (config.GetValue<bool>("UseSwagger"))
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

View File

@@ -0,0 +1,41 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:53791",
"sslPort": 44384
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5055",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7248;http://localhost:5055",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@@ -0,0 +1,46 @@
using DigitalData.Swagger.MockAPI.Data;
using DigitalData.Swagger.MockAPI.Dtos;
using Microsoft.EntityFrameworkCore;
namespace DigitalData.Swagger.MockAPI.Repos;
public class CompanyRepo(ApiContext context) : ICompanyRepo
{
private readonly ApiContext _context = context;
public IQueryable<Company> GetAll()
{
return _context.Companies
.Include(a => a.Products)
.AsQueryable();
}
public IQueryable<Company> GetById(int id)
{
return _context.Companies
.Include(a => a.Products)
.AsQueryable()
.Where(c => c.ID == id);
}
public void Create(Company company)
{
_context.Companies
.Add(company);
_context.SaveChanges();
}
public void Update(Company company)
{
_context.Companies
.Update(company);
_context.SaveChanges();
}
public void Delete(Company company)
{
_context.Companies
.Remove(company);
_context.SaveChanges();
}
}

View File

@@ -0,0 +1,11 @@
using DigitalData.Swagger.MockAPI.Dtos;
namespace DigitalData.Swagger.MockAPI.Repos;
public interface ICompanyRepo
{
public IQueryable<Company> GetAll();
public IQueryable<Company> GetById(int id);
public void Create(Company company);
public void Update(Company company);
public void Delete(Company company);
}

View File

@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@@ -0,0 +1,10 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"UseSwagger": true
}

View File

@@ -0,0 +1,9 @@
namespace DigitalData.Swagger.Proxy.Configs
{
public class OriginServerParams
{
public required string Url { get; init; }
public Dictionary<string, string> DefaultHeaders { get; init; } = new();
}
}

View File

@@ -0,0 +1,13 @@
using DigitalData.Swagger.Proxy.Configs;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
namespace DigitalData.Swagger.Proxy.Controllers;
[Route("[controller]")]
[ApiController]
public class MetaController(IOptions<OriginServerParams> originServerParamsoptions) : ControllerBase
{
[HttpGet]
public IActionResult GetOriginServerParams() => Ok(originServerParamsoptions.Value);
}

View File

@@ -0,0 +1,58 @@
namespace DigitalData.Swagger.Proxy.Controllers;
using DigitalData.Swagger.Proxy.Configs;
using Flurl;
using Flurl.Http;
using Flurl.Util;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System.Threading.Tasks;
[ApiController]
public class ProxyController(IOptions<OriginServerParams> originServerParamsOptions) : ControllerBase
{
private readonly OriginServerParams _origin = originServerParamsOptions.Value;
[HttpPost("{servicetierName}/ODataV4/{webserviceName}_CreateInvoice")]
public async Task<IActionResult> Redir([FromRoute] string servicetierName, [FromRoute] string webserviceName, [FromQuery] string company, [FromBody] Dictionary<string, object>? body = null)
{
var req = _origin.Url
.AppendPathSegment(servicetierName)
.AppendPathSegment("ODataV4")
.AppendPathSegment(webserviceName + "_CreateInvoice")
.SetQueryParams(new { company })
.WithHeader("X-Forwarded-For", HttpContext.Connection.RemoteIpAddress?.ToString());
// Add default headers
foreach (var header in _origin.DefaultHeaders)
req = req.WithHeader(header.Key, header.Value);
// Add request headers
foreach (var header in HttpContext.Request.Headers)
req = req.WithHeader(header.Key, header.Value);
// Add reqest cookies
foreach (var cookie in HttpContext.Request.Cookies)
req = req.WithCookie(cookie.Key, cookie.Value);
//post request
var res = body is null ? await req.PostAsync() : await req.PostJsonAsync(body);
// set headers
foreach (var (Name, Value) in res.Headers)
HttpContext.Response.Headers[Name] = Value;
// set cookies
foreach (var (Name, Value) in res.Cookies.ToKeyValuePairs())
HttpContext.Response.Cookies.Append(Name, Value.ToString() ?? string.Empty);
var resBody = res.GetJsonAsync<dynamic>();
if (resBody is not null)
return new JsonResult(resBody)
{
StatusCode = res.StatusCode
};
return StatusCode(res.StatusCode);
}
}

View File

@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Flurl.Http" Version="4.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,31 @@
using DigitalData.Swagger.Proxy.Configs;
var builder = WebApplication.CreateBuilder(args);
var config = builder.Configuration;
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var originServerSection = config.GetRequiredSection("OriginServer")
?? throw new InvalidOperationException("The 'OriginServer' section is missing or incorrectly configured in the application configuration.");
builder.Services.Configure<OriginServerParams>(originServerSection);
var app = builder.Build();
// use swagger in production
app.UseSwagger();
app.UseSwaggerUI();
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

View File

@@ -0,0 +1,41 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:60947",
"sslPort": 44357
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5082",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7121;http://localhost:5082",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@@ -0,0 +1,15 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"OriginServer": {
"Url": "https://localhost:7248",
"DefaultHeaders": {
"Authorization": "Basic username:password"
}
}
}