Support Flecs REST API

main
copygirl 2 months ago
parent 2b7c8528d3
commit b170082b51
  1. 6
      src/builtin/flecs.rest.zig
  2. 1
      src/builtin/flecs.zig
  3. 33
      src/world.zig

@ -0,0 +1,6 @@
//! The Flecs REST API enables clients to inspect the contents of the
//! ECS database, by remotely running queries and requesting entities.
const c = @import("../c.zig");
pub const Rest = c.EcsRest;

@ -3,4 +3,5 @@
pub const core = @import("./flecs.core.zig");
pub const doc = @import("./flecs.doc.zig");
pub const pipeline = @import("./flecs.pipeline.zig");
pub const rest = @import("./flecs.rest.zig");
pub const system = @import("./flecs.system.zig");

@ -49,6 +49,10 @@ pub fn World(comptime ctx: anytype) type {
flecszigble.allocator.destroy(self);
}
pub fn enableRest(self: *Self, options: struct { port: u16 = 0, addr: ?[:0]const u8 = null }) !void {
try self.set(flecs.rest.Rest, .{ .port = options.port, .ipaddr = @constCast(options.addr) });
}
pub fn progress(self: *Self, delta_time: f32) bool {
return c.ecs_progress(self.raw, delta_time);
}
@ -285,3 +289,32 @@ pub fn World(comptime ctx: anytype) type {
}
};
}
test "World REST API" {
const expect = @import("./test/expect.zig");
const alloc = std.testing.allocator;
flecszigble.init(alloc);
var world = try World(void).init();
defer world.deinit();
try world.enableRest(.{ .port = 42666 });
const Runner = struct {
pub fn run(w: *World(void)) void {
while (w.progress(0.0)) {}
}
};
var thread = try std.Thread.spawn(.{ .allocator = alloc }, Runner.run, .{world});
defer thread.join();
const url = "http://localhost:42666/entity/flecs/core/World";
var client = std.http.Client{ .allocator = alloc };
defer client.deinit();
var result = try client.fetch(alloc, .{ .location = .{ .url = url } });
defer result.deinit();
try expect.equalStrings("{\"path\":\"flecs.core.World\", \"ids\":[]}", result.body);
world.quit();
}

Loading…
Cancel
Save