sokol-zig icon indicating copy to clipboard operation
sokol-zig copied to clipboard

Add a zig webserver

Open ringtailsoftware opened this issue 2 months ago • 8 comments

I'm not sure whether this is useful to others, but offering a PR just in case. I've added a pure Zig static webserver to serve files generated by the wasm examples, including an index.html with a list to pick from, rather than relying on emrun.py to serve a single example.

ringtailsoftware avatar Nov 07 '25 16:11 ringtailsoftware

Heh, nice. I wonder if we should merge this:

zig build examples -Dtarget=wasm32-emscripten && zig build serve -- zig-out/web -p 8000

...into an 'all-in-one-step' like this (couldn't come up with a better step-name)...

zig build serve-wasm

E.g. this would build the examples for wasm32-emscripten and the web server for the host platform and then run server with zig-out/web -p 8000...

floooh avatar Nov 07 '25 16:11 floooh

Oki thanks! It might take a few days until I get around to merging, most likely Monday.

floooh avatar Nov 07 '25 19:11 floooh

There seems to be some chicken-egg-situation now when running zig run serve-wasm where the output directories don't exist yet when the web server is trying to start:

unable to open directory 'zig-out/web': FileNotFound
serve-wasm
└─ run exe serve failure
error: the following command exited with error code 1:
./.zig-cache/o/32ecab056c7c68975b793aee9d22d5da/serve zig-out/web -p 8000

...maybe a missing dependency between building the examples and starting the web server?

floooh avatar Nov 10 '25 09:11 floooh

I've split things up, so that the main -Dtarget= controls the target for the examples, but the web server is always built for the host target.

zig build -Dtarget=wasm32-emscripten serve-wasm is a bit wordier, but makes sense as it would allow web serving of a different target too.

ringtailsoftware avatar Nov 13 '25 16:11 ringtailsoftware

Oki doki, I'll give it another whirl 'soon-ish' (not today though).

floooh avatar Nov 13 '25 17:11 floooh

Hmm I still see a weird problem. All the sample links get a 404.

Here's what I do (on macOS):

> rm -rf zig-out .zig-cache
> zig build -Dtarget=wasm32-emscripten serve-wasm
Listening at http://127.0.0.1:8000/

...click on the link so the browser opens, and I see the list of samples.

But clicking on any of the links produces a 404 and instead of the sample I see the index.html webpage again:

image

Looking into zig-out/web the sample files are definitely there though.

When I do zig build -Dtarget=wasm32-emscripten serve-wasm a second time it works as expected though...

Not sure what's going on there though, skimming over the code the step-dependencies look correct to me...

Can you reproduce what I'm seeing?

floooh avatar Nov 14 '25 18:11 floooh

Building the examples and serve-wasm targets works for me. Apologies for the multiple attempts at this. If it's still a problem, feel free to close the PR. zig build -Dtarget=wasm32-emscripten examples serve-wasm

ringtailsoftware avatar Nov 14 '25 23:11 ringtailsoftware

Hmm same problem also when running zig build -Dtarget=wasm32-emscripten examples serve-wasm, I'll keep the PR open, maybe I'll get around at a later time to investigate the issue. In general I like the idea of a 'table-of-content' page for the web samples, I'm just not sure yet about the added build complexity and the additional dependency.

In general I'd like to move some things out of the sokol-zig project because the actual bindings would not require the emsdk and sokol-shdc dependencies, but I think the only realistic way is to move the examples into a separate build.zig and build.zig.zon, maybe even a separate github repo... but I haven't made up my mind yet :)

floooh avatar Nov 15 '25 11:11 floooh