Some ish that works (datastream webapp)
This commit is contained in:
1 parent
aba1760296
commit
0650dba678
16 files changed
+751
-126
No files matched your search
@@ -1,30 +1,210 @@
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
using BCG;
|
||||
|
||||
// Add services to the container.
|
||||
using bts.api.core;
|
||||
|
||||
builder.Services.AddControllers();
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
using Bts.DataStreamExpressions;
|
||||
using Bts.DataStreamWebApp.Server.Controllers;
|
||||
|
||||
var app = builder.Build();
|
||||
using CommandLine;
|
||||
|
||||
app.UseDefaultFiles();
|
||||
app.UseStaticFiles();
|
||||
using System.Net.WebSockets;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
var parser = new Parser(ps => {
|
||||
ps.CaseInsensitiveEnumValues = true;
|
||||
ps.HelpWriter = Console.Out;
|
||||
});
|
||||
|
||||
await parser.ParseArguments<Arguments>(args).
|
||||
MapResult(run, errors => Task.FromResult(1));
|
||||
|
||||
async Task<int> run(Arguments parsed_args) {
|
||||
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();
|
||||
|
||||
var app = builder.Build();
|
||||
var wsOpts = new WebSocketOptions {
|
||||
KeepAliveInterval = TimeSpan.FromMinutes(2)
|
||||
};
|
||||
app.UseWebSockets(wsOpts);
|
||||
app.UseDefaultFiles();
|
||||
app.UseStaticFiles();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment()) {
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
|
||||
// app.UseHttpsRedirection();
|
||||
// app.UseAuthorization();
|
||||
// app.MapControllers();
|
||||
app.MapFallbackToFile("/index.html");
|
||||
|
||||
app.Use(async (context, next) => {
|
||||
if (context.Request.Path == "/ws") {
|
||||
if (context.WebSockets.IsWebSocketRequest) {
|
||||
using var webSocket = await context.WebSockets.AcceptWebSocketAsync();
|
||||
await runWebSocketServer(
|
||||
parsed_args.sparkd, parsed_args.user!, parsed_args.password!, webSocket);
|
||||
} else {
|
||||
context.Response.StatusCode = StatusCodes.Status400BadRequest;
|
||||
}
|
||||
} else {
|
||||
await next(context);
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
await app.RunAsync();
|
||||
} catch (Exception ex) {
|
||||
Console.WriteLine($"Exception on DataStreamWebsocketServer thread: {ex}");
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
async Task<int> runWebSocketServer(string? sparkd, string user, string password, WebSocket webSocket) {
|
||||
var context = ServiceContext.create(sparkd);
|
||||
await context.login(user, password);
|
||||
var buffer = new byte[1024 * 4];
|
||||
var receiveResult = await webSocket.ReceiveAsync(
|
||||
new ArraySegment<byte>(buffer), CancellationToken.None);
|
||||
|
||||
app.UseAuthorization();
|
||||
try {
|
||||
while (!receiveResult.CloseStatus.HasValue) {
|
||||
receiveResult = await webSocket.ReceiveAsync(
|
||||
new ArraySegment<byte>(buffer), CancellationToken.None);
|
||||
var msg = Encoding.UTF8.GetString(buffer, 0, receiveResult.Count);
|
||||
var queryArgs = new QueryArguments { Query = msg };
|
||||
// var queryArgs = JsonSerializer.Deserialize<QueryArguments>(msg);
|
||||
// if (queryArgs is null) {
|
||||
// Console.WriteLine("Invalid queryArgs.");
|
||||
// continue;
|
||||
// }
|
||||
var exp = DataStreamExpressionsInterpreter.Evaluate(queryArgs.Query!);
|
||||
if (exp is null) {
|
||||
Console.WriteLine($"Invalid query expression: {queryArgs.Query}.");
|
||||
continue;
|
||||
}
|
||||
var vs = exp.getView(new Dictionary<string, StatementExpression>(0));
|
||||
vs.onChange += onStreamChanged;
|
||||
|
||||
app.MapControllers();
|
||||
await webSocket.SendAsync(
|
||||
new ArraySegment<byte>(buffer, 0, receiveResult.Count),
|
||||
receiveResult.MessageType,
|
||||
receiveResult.EndOfMessage,
|
||||
CancellationToken.None);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
Console.WriteLine($"Exception on WebsocketServer thread: {ex}");
|
||||
return 1;
|
||||
}
|
||||
|
||||
app.MapFallbackToFile("/index.html");
|
||||
return 0;
|
||||
|
||||
app.Run();
|
||||
async void onStreamChanged(object newValue) {
|
||||
if (newValue is not DataPacket dp) {
|
||||
Console.WriteLine(
|
||||
$"OnStreamChanged delegate expected DataPacket (actually received {newValue?.GetType()}.");
|
||||
return;
|
||||
}
|
||||
var added_rows_str = dp.addedRows.get_printed_rows(val => val.Length);
|
||||
var msg = $"Received new rows:\n{added_rows_str}";
|
||||
// var payload = new ReadOnlyMemory<byte>([.. msg]);
|
||||
var encoded = Encoding.UTF8.GetBytes(msg);
|
||||
var payload = new ArraySegment<byte>(encoded, 0, encoded.Length);
|
||||
await webSocket.SendAsync(
|
||||
payload,
|
||||
receiveResult.MessageType,
|
||||
receiveResult.EndOfMessage,
|
||||
CancellationToken.None);
|
||||
}
|
||||
}
|
||||
|
||||
class QueryArguments {
|
||||
public string? Query { get; set; }
|
||||
}
|
||||
|
||||
class Arguments {
|
||||
[Option("sparkd")]
|
||||
public string? sparkd { get; set; }
|
||||
|
||||
[Option("user", Default = "bts")]
|
||||
public string? user { get; set; }
|
||||
|
||||
[Option("password", Default = "")]
|
||||
public string? password { get; set; }
|
||||
}
|
||||
|
||||
|
||||
// using Bts.DataStreamWebApp.Server.Controllers;
|
||||
|
||||
// using Microsoft.Extensions.FileProviders;
|
||||
|
||||
// var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// builder.Services.AddControllers();
|
||||
// builder.Services.AddDirectoryBrowser();
|
||||
// builder.Services.AddEndpointsApiExplorer();
|
||||
// builder.Services.AddSwaggerGen();
|
||||
|
||||
// var app = builder.Build();
|
||||
|
||||
// // <snippet_UseWebSockets>
|
||||
// var webSocketOptions = new WebSocketOptions {
|
||||
// KeepAliveInterval = TimeSpan.FromMinutes(2)
|
||||
// };
|
||||
|
||||
// app.UseWebSockets(webSocketOptions);
|
||||
// // </snippet_UseWebSockets>
|
||||
|
||||
// app.UseDefaultFiles();
|
||||
// app.UseStaticFiles();
|
||||
|
||||
// app.MapControllers();
|
||||
// app.MapDefaultControllerRoute();
|
||||
|
||||
// app.UseHttpsRedirection();
|
||||
// app.UseAuthorization();
|
||||
|
||||
// var fileProvider = new PhysicalFileProvider(
|
||||
// Path.Combine(builder.Environment.WebRootPath, "images"));
|
||||
// var requestPath = "/MyImages";
|
||||
|
||||
// // Enable displaying browser links.
|
||||
// app.UseStaticFiles(new StaticFileOptions {
|
||||
// FileProvider = fileProvider,
|
||||
// RequestPath = requestPath
|
||||
// });
|
||||
|
||||
// app.UseDirectoryBrowser(new DirectoryBrowserOptions {
|
||||
// FileProvider = fileProvider,
|
||||
// RequestPath = requestPath
|
||||
// });
|
||||
|
||||
// // Configure the HTTP request pipeline.
|
||||
// if (app.Environment.IsDevelopment()) {
|
||||
// app.UseSwagger();
|
||||
// app.UseSwaggerUI();
|
||||
// }
|
||||
|
||||
// app.Use(async (context, next) => {
|
||||
// if (context.Request.Path == "/ws") {
|
||||
// if (context.WebSockets.IsWebSocketRequest) {
|
||||
// using var webSocket = await context.WebSockets.AcceptWebSocketAsync();
|
||||
// await DataStreamSubscriptionController.Echo(webSocket);
|
||||
// } else {
|
||||
// context.Response.StatusCode = StatusCodes.Status400BadRequest;
|
||||
// }
|
||||
// } else {
|
||||
// await next(context);
|
||||
// }
|
||||
// });
|
||||
|
||||
// app.Run();
|
||||
Reference in new issue
Block a user