126 lines
3.1 KiB
C#
126 lines
3.1 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.OpenApi.Models;
|
|
using Squiddler_Server_Lite;
|
|
using Squiddler_Server_Lite.Middleware;
|
|
using Squiddler_Server_Lite.TransferObjects;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// Add services to the container.
|
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
builder.Services.AddSwaggerGen(options =>
|
|
{
|
|
options.SupportNonNullableReferenceTypes();
|
|
options.MapType<GameCode>(() => new OpenApiSchema
|
|
{
|
|
Type = "string",
|
|
MinLength = 4,
|
|
MaxLength = 4,
|
|
});
|
|
});
|
|
|
|
var app = builder.Build();
|
|
|
|
// Configure the HTTP request pipeline.
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI();
|
|
}
|
|
|
|
app.UseHttpsRedirection();
|
|
app.UseWebSockets();
|
|
|
|
var apiPrefix = app.MapGroup("/api");
|
|
|
|
apiPrefix.MapGet("/test/card/{text}/{value}", Api.GetTestCard)
|
|
.Produces<Card>(200, "application/json")
|
|
.WithName("Test_GetCard")
|
|
.WithOpenApi();
|
|
|
|
apiPrefix.MapPost("/create_game", Api.CreateGame)
|
|
.Produces<GameCode>(200, "text/plain")
|
|
.Produces<string>(400, "text/plain")
|
|
.WithName("CreateNewGame")
|
|
.WithOpenApi();
|
|
|
|
var playPrefix = apiPrefix.MapGroup("/play")
|
|
.WithOpenApi(op =>
|
|
{
|
|
OpenApiOperation newOp = new(op);
|
|
newOp.Parameters.Add(new OpenApiParameter
|
|
{
|
|
Required = true,
|
|
In = ParameterLocation.Header,
|
|
Style = ParameterStyle.Simple,
|
|
AllowEmptyValue = false,
|
|
Name = Api.Headers.GAMECODE,
|
|
Schema = new OpenApiSchema
|
|
{
|
|
Type = "string",
|
|
MaxLength = 4,
|
|
MinLength = 4
|
|
},
|
|
});
|
|
newOp.Parameters.Add(new OpenApiParameter
|
|
{
|
|
Required = true,
|
|
In = ParameterLocation.Header,
|
|
Style = ParameterStyle.Simple,
|
|
AllowEmptyValue = false,
|
|
Name = Api.Headers.PLAYERNAME,
|
|
Schema = new OpenApiSchema { Type = "string" },
|
|
});
|
|
return newOp;
|
|
});
|
|
|
|
playPrefix.MapPut("/draw_card", (Delegate)Api.DrawCard)
|
|
.Produces<Card>(200, "application/json")
|
|
.Produces<string>(400, "text/plain")
|
|
.WithName("DrawCard")
|
|
.WithOpenApi();
|
|
|
|
playPrefix.MapPut("/join_game", (Delegate)Api.JoinGame)
|
|
.Produces(200)
|
|
.Produces<string>(400, "text/plain")
|
|
.Produces<string>(409, "text/plain")
|
|
.WithName("JoinGame")
|
|
.WithOpenApi();
|
|
|
|
playPrefix.MapPut("/play_cards", (Delegate)Api.PlayCards)
|
|
.Produces(200)
|
|
.Produces<string>(400, "text/plain")
|
|
.WithName("PlayCards")
|
|
.WithOpenApi();
|
|
|
|
|
|
app.Run();
|
|
|
|
/*
|
|
var summaries = new[]
|
|
{
|
|
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
|
|
};
|
|
|
|
app.MapGet("/weatherforecast", () =>
|
|
{
|
|
var forecast = Enumerable.Range(1, 5).Select(index =>
|
|
new WeatherForecast
|
|
(
|
|
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
|
|
Random.Shared.Next(-20, 55),
|
|
summaries[Random.Shared.Next(summaries.Length)]
|
|
))
|
|
.ToArray();
|
|
return forecast;
|
|
})
|
|
.WithName("GetWeatherForecast")
|
|
.WithOpenApi();
|
|
|
|
internal record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
|
|
{
|
|
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
|
|
}
|
|
*/
|