summary refs log tree commit diff
path: root/GitRepoViewer.CorsProxy
diff options
context:
space:
mode:
Diffstat (limited to 'GitRepoViewer.CorsProxy')
-rw-r--r--GitRepoViewer.CorsProxy/Controllers/SimpleProxyController.cs50
-rw-r--r--GitRepoViewer.CorsProxy/GitRepoViewer.CorsProxy.csproj14
-rw-r--r--GitRepoViewer.CorsProxy/Program.cs33
-rw-r--r--GitRepoViewer.CorsProxy/Properties/launchSettings.json41
-rw-r--r--GitRepoViewer.CorsProxy/appsettings.Development.json8
-rw-r--r--GitRepoViewer.CorsProxy/appsettings.json9
6 files changed, 155 insertions, 0 deletions
diff --git a/GitRepoViewer.CorsProxy/Controllers/SimpleProxyController.cs b/GitRepoViewer.CorsProxy/Controllers/SimpleProxyController.cs
new file mode 100644

index 0000000..725c375 --- /dev/null +++ b/GitRepoViewer.CorsProxy/Controllers/SimpleProxyController.cs
@@ -0,0 +1,50 @@ +using Microsoft.AspNetCore.Mvc; + +namespace GitRepoViewer.CorsProxy.Controllers; + +[ApiController] +// [Route("[controller]")] +[Route("/")] +public class SimpleProxyController : ControllerBase +{ + private readonly ILogger<SimpleProxyController> _logger; + + public SimpleProxyController(ILogger<SimpleProxyController> logger) + { + _logger = logger; + } + + [HttpGet("get/{*url}")] + public async Task Get(string url) + { + _logger.LogInformation("Proxying url: " + url); + + var request = new HttpRequestMessage(HttpMethod.Get, url); + + var client = new HttpClient(); + + var response = await client.SendAsync(request); + + if(!response.IsSuccessStatusCode) + _logger.LogWarning("Got status code: " + response.StatusCode + " for url: " + url); + //throw new Exception("Failed to fetch file: " + url); + + Response.StatusCode = (int) response.StatusCode; + Response.ContentType = response.Content.Headers.ContentType?.MediaType ?? "application/octet-stream"; + //copy headers + foreach (var (key, value) in response.Headers) + { + Response.Headers.Add(key, value.ToArray()); + } + //override cors + Response.Headers.Add("Access-Control-Allow-Origin", "*"); + Response.Headers.Add("Access-Control-Allow-Headers", "*"); + Response.Headers.Add("Access-Control-Allow-Methods", "*"); + Response.Headers.Add("Access-Control-Allow-Credentials", "true"); + Response.Headers.Add("Access-Control-Max-Age", "86400"); + + await response.Content.CopyToAsync(Response.Body); + + //return new FileStreamResult(await response.Content.ReadAsStreamAsync(), response.Content.Headers.ContentType?.MediaType ?? "application/octet-stream"); + } +} \ No newline at end of file diff --git a/GitRepoViewer.CorsProxy/GitRepoViewer.CorsProxy.csproj b/GitRepoViewer.CorsProxy/GitRepoViewer.CorsProxy.csproj new file mode 100644
index 0000000..460f7d2 --- /dev/null +++ b/GitRepoViewer.CorsProxy/GitRepoViewer.CorsProxy.csproj
@@ -0,0 +1,14 @@ +<Project Sdk="Microsoft.NET.Sdk.Web"> + + <PropertyGroup> + <TargetFramework>net7.0</TargetFramework> + <Nullable>enable</Nullable> + <ImplicitUsings>enable</ImplicitUsings> + </PropertyGroup> + + <ItemGroup> + <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.5" /> + <PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" /> + </ItemGroup> + +</Project> diff --git a/GitRepoViewer.CorsProxy/Program.cs b/GitRepoViewer.CorsProxy/Program.cs new file mode 100644
index 0000000..10b061b --- /dev/null +++ b/GitRepoViewer.CorsProxy/Program.cs
@@ -0,0 +1,33 @@ +var builder = WebApplication.CreateBuilder(args); + +// 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(); +builder.Services.AddCors(options => +{ + options.AddPolicy( + "Open", + builder => builder.AllowAnyOrigin().AllowAnyHeader()); +}); + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); +} + +app.UseHttpsRedirection(); + +app.UseCors("Open"); + +app.UseAuthorization(); + +app.MapControllers(); + +app.Run(); \ No newline at end of file diff --git a/GitRepoViewer.CorsProxy/Properties/launchSettings.json b/GitRepoViewer.CorsProxy/Properties/launchSettings.json new file mode 100644
index 0000000..2d53cb3 --- /dev/null +++ b/GitRepoViewer.CorsProxy/Properties/launchSettings.json
@@ -0,0 +1,41 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:46468", + "sslPort": 44315 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": false, + "launchUrl": "swagger", + "applicationUrl": "http://localhost:5025", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": false, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:7237;http://localhost:5025", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": false, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/GitRepoViewer.CorsProxy/appsettings.Development.json b/GitRepoViewer.CorsProxy/appsettings.Development.json new file mode 100644
index 0000000..0c208ae --- /dev/null +++ b/GitRepoViewer.CorsProxy/appsettings.Development.json
@@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/GitRepoViewer.CorsProxy/appsettings.json b/GitRepoViewer.CorsProxy/appsettings.json new file mode 100644
index 0000000..10f68b8 --- /dev/null +++ b/GitRepoViewer.CorsProxy/appsettings.json
@@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +}