105 lines
2.8 KiB
C#
105 lines
2.8 KiB
C#
using System.Text.Json.Serialization;
|
|
using Microsoft.OpenApi.Models;
|
|
using OrderSupportTicket.TestServer;
|
|
using OrderSupportTicket.TestServer.Contracts;
|
|
using OrderSupportTicket.TestServer.Data;
|
|
using OrderSupportTicket.TestServer.Endpoints;
|
|
using OrderSupportTicket.TestServer.Services;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
builder.Services.AddSingleton<TicketEventHub>();
|
|
builder.Services.AddSingleton<InMemoryTicketStore>();
|
|
builder.Services.AddCors(options =>
|
|
{
|
|
options.AddDefaultPolicy(policy => policy
|
|
.AllowAnyOrigin()
|
|
.AllowAnyHeader()
|
|
.AllowAnyMethod());
|
|
});
|
|
builder.Services.ConfigureHttpJsonOptions(options =>
|
|
{
|
|
options.SerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.Never;
|
|
});
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
builder.Services.AddSwaggerGen(options =>
|
|
{
|
|
options.SwaggerDoc("v1", new OpenApiInfo
|
|
{
|
|
Title = "OrderSupportTicket 测试服务端",
|
|
Version = "v1",
|
|
Description = "纯内存实现客户工单 API v2;重启服务后数据恢复为初始种子数据。"
|
|
});
|
|
});
|
|
|
|
var app = builder.Build();
|
|
|
|
app.UseCors();
|
|
|
|
app.Use(async (context, next) =>
|
|
{
|
|
try
|
|
{
|
|
await next();
|
|
}
|
|
catch (ApiException exception)
|
|
{
|
|
if (context.Response.HasStarted)
|
|
{
|
|
throw;
|
|
}
|
|
|
|
context.Response.Clear();
|
|
context.Response.StatusCode = exception.StatusCode;
|
|
context.Response.ContentType = "application/json; charset=utf-8";
|
|
await context.Response.WriteAsJsonAsync(new ApiErrorResponse
|
|
{
|
|
Success = false,
|
|
Message = exception.Message,
|
|
Error = exception.Message,
|
|
Code = exception.Code
|
|
});
|
|
}
|
|
catch (BadHttpRequestException exception)
|
|
{
|
|
if (context.Response.HasStarted)
|
|
{
|
|
throw;
|
|
}
|
|
|
|
context.Response.Clear();
|
|
context.Response.StatusCode = StatusCodes.Status400BadRequest;
|
|
context.Response.ContentType = "application/json; charset=utf-8";
|
|
await context.Response.WriteAsJsonAsync(new ApiErrorResponse
|
|
{
|
|
Success = false,
|
|
Message = exception.Message,
|
|
Error = exception.Message,
|
|
Code = "bad_request"
|
|
});
|
|
}
|
|
});
|
|
|
|
app.UseDefaultFiles();
|
|
app.UseStaticFiles();
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI(options =>
|
|
{
|
|
options.SwaggerEndpoint("/swagger/v1/swagger.json", "OrderSupportTicket Test API v1");
|
|
options.DocumentTitle = "OrderSupportTicket 测试 API";
|
|
});
|
|
|
|
app.MapGet("/health", () => Results.Ok(new
|
|
{
|
|
success = true,
|
|
service = "OrderSupportTicket.TestServer",
|
|
storage = "in-memory",
|
|
utcNow = DateTimeOffset.UtcNow.ToString("O")
|
|
}));
|
|
|
|
app.MapTicketApiEndpoints();
|
|
|
|
app.Run();
|
|
|
|
public partial class Program { }
|