diff --git a/src/builtin/flecs.rest.zig b/src/builtin/flecs.rest.zig new file mode 100644 index 0000000..f5f65d2 --- /dev/null +++ b/src/builtin/flecs.rest.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; diff --git a/src/builtin/flecs.zig b/src/builtin/flecs.zig index d364127..8a04266 100644 --- a/src/builtin/flecs.zig +++ b/src/builtin/flecs.zig @@ -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"); diff --git a/src/world.zig b/src/world.zig index 8d5926e..4e0e8e2 100644 --- a/src/world.zig +++ b/src/world.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(); +}