initial commit
This commit is contained in:
commit
c92383721d
16 changed files with 786 additions and 0 deletions
55
src/Oh.My.Stitcher/Program.cs
Normal file
55
src/Oh.My.Stitcher/Program.cs
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
using System.IO.Pipelines;
|
||||
using Microsoft.AspNetCore.Http.Json;
|
||||
using NetVips;
|
||||
using Oh.My.Stitcher;
|
||||
using Validation;
|
||||
using ZLogger;
|
||||
|
||||
WebApplicationBuilder builder = WebApplication.CreateSlimBuilder(args);
|
||||
builder.Logging.ClearProviders().AddZLoggerConsole();
|
||||
builder.Services.Configure<JsonOptions>(options =>
|
||||
{
|
||||
options.SerializerOptions.TypeInfoResolver = StitchSerializerContext.Default;
|
||||
});
|
||||
WebApplication app = builder.Build();
|
||||
|
||||
ILoggerFactory loggerFactory = app.Services.GetRequiredService<ILoggerFactory>();
|
||||
ILogger logger = loggerFactory.CreateLogger<Program>();
|
||||
|
||||
string? tilesDirectory = Environment.GetEnvironmentVariable("ASSET_PATH_RO");
|
||||
|
||||
// sanity check
|
||||
Assumes.NotNullOrEmpty(tilesDirectory);
|
||||
Assumes.True(File.Exists(Path.Combine(tilesDirectory, "A1.png")));
|
||||
Assumes.True(File.Exists(Path.Combine(tilesDirectory, "AE55.png")));
|
||||
|
||||
app.UseDefaultFiles();
|
||||
app.UseStaticFiles();
|
||||
app.MapPost("/api/image/generate", (Stitch request) =>
|
||||
{
|
||||
Pipe pipe = new();
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
List<Image> images = [];
|
||||
try
|
||||
{
|
||||
using Image image = Tile.Create(in request, tilesDirectory, images, logger);
|
||||
image.WriteToStream(pipe.Writer.AsStream(), ".png");
|
||||
}
|
||||
catch( Exception e )
|
||||
{
|
||||
logger.ZLogError(e, $"Error when generating image");
|
||||
using Image errorImage = Tile.CreateError(e);
|
||||
errorImage.WriteToStream(pipe.Writer.AsStream(), ".png");
|
||||
}
|
||||
finally
|
||||
{
|
||||
foreach( Image img in images )
|
||||
img.Dispose();
|
||||
await pipe.Writer.CompleteAsync();
|
||||
}
|
||||
});
|
||||
return Results.Stream(pipe.Reader.AsStream(), "image/png");
|
||||
});
|
||||
|
||||
app.Run();
|
||||
Loading…
Add table
Add a link
Reference in a new issue