| | 1 | | using cat_cafe.Repositories; |
| | 2 | | using cat_cafe.WeSo; |
| | 3 | | using Microsoft.EntityFrameworkCore; |
| | 4 | | using System.Net.WebSockets; |
| | 5 | |
|
| 0 | 6 | | var builder = WebApplication.CreateBuilder(args); |
| | 7 | |
|
| 0 | 8 | | List<WebSocket> _sockets = new(); |
| | 9 | |
|
| | 10 | | // Add services to the container. |
| | 11 | |
|
| 0 | 12 | | builder.Services.AddLogging(configure => configure.AddFile("log.txt")); |
| 0 | 13 | | builder.Services.AddSingleton(x => _sockets); |
| 0 | 14 | | builder.Services.AddSingleton<WebSocketHandler>(); |
| 0 | 15 | | builder.Services.AddControllers(); |
| 0 | 16 | | builder.Services.AddDbContext<CatCafeContext>(opt => opt.UseSqlite("Data Source=cat_cafe.db")); |
| 0 | 17 | | builder.Services.AddEndpointsApiExplorer(); |
| 0 | 18 | | builder.Services.AddSwaggerGen(); |
| 0 | 19 | | builder.Services.AddAutoMapper(typeof(Program)); |
| 0 | 20 | | builder.Services.AddControllersWithViews(); |
| 0 | 21 | | builder.Services.AddApiVersioning(opt => { opt.ReportApiVersions = true; }); |
| 0 | 22 | | builder.Services.AddVersionedApiExplorer( |
| 0 | 23 | | opt => |
| 0 | 24 | | { |
| 0 | 25 | | opt.GroupNameFormat = "'v'VVV"; |
| 0 | 26 | | opt.SubstituteApiVersionInUrl = true; |
| 0 | 27 | | } |
| 0 | 28 | | ); |
| | 29 | |
|
| 0 | 30 | | var app = builder.Build(); |
| | 31 | |
|
| 0 | 32 | | using (var serviceScope = app.Services.CreateScope()) |
| 0 | 33 | | { |
| 0 | 34 | | var context = serviceScope.ServiceProvider.GetRequiredService<CatCafeContext>(); |
| 0 | 35 | | context.Database.EnsureCreated(); |
| 0 | 36 | | if (context.Database.GetPendingMigrations().Any()) |
| 0 | 37 | | { |
| 0 | 38 | | context.Database.Migrate(); |
| 0 | 39 | | } |
| 0 | 40 | | } |
| | 41 | |
|
| 0 | 42 | | app.UseHttpLogging(); |
| | 43 | |
|
| | 44 | | // Configure the HTTP request pipeline. |
| 0 | 45 | | if (app.Environment.IsDevelopment()) |
| 0 | 46 | | { |
| 0 | 47 | | app.UseSwagger(); |
| 0 | 48 | | app.UseSwaggerUI(); |
| 0 | 49 | | } |
| | 50 | |
|
| 0 | 51 | | app.UseHttpsRedirection(); |
| | 52 | |
|
| 0 | 53 | | app.UseAuthorization(); |
| | 54 | |
|
| 0 | 55 | | app.MapControllers(); |
| | 56 | |
|
| 0 | 57 | | app.UseWebSockets(); |
| | 58 | |
|
| 0 | 59 | | app.Use(async (context, next) => |
| 0 | 60 | | { |
| 0 | 61 | | if (context.Request.Path == "/ws") |
| 0 | 62 | | { |
| 0 | 63 | | if (context.WebSockets.IsWebSocketRequest) |
| 0 | 64 | | { |
| 0 | 65 | | WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync(); |
| 0 | 66 | |
|
| 0 | 67 | | _sockets.Add(webSocket); |
| 0 | 68 | |
|
| 0 | 69 | | var buffer = new byte[1024 * 4]; |
| 0 | 70 | | WebSocketReceiveResult result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationTok |
| 0 | 71 | | while (!result.CloseStatus.HasValue) |
| 0 | 72 | | { |
| 0 | 73 | | await webSocket.SendAsync(new ArraySegment<byte>(buffer, 0, result.Count), result.MessageType, result.En |
| 0 | 74 | |
|
| 0 | 75 | | result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None); |
| 0 | 76 | | } |
| 0 | 77 | | await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None); |
| 0 | 78 | | } |
| 0 | 79 | | else |
| 0 | 80 | | { |
| 0 | 81 | | context.Response.StatusCode = 400; |
| 0 | 82 | | } |
| 0 | 83 | | } |
| 0 | 84 | | else |
| 0 | 85 | | { |
| 0 | 86 | | await next(); |
| 0 | 87 | | } |
| 0 | 88 | | }); |
| | 89 | |
|
| 0 | 90 | | app.Run(); |