Skip to content

Commit

Permalink
docs: update readme walkthrough (#21)
Browse files Browse the repository at this point in the history
* docs: update readme walkthrough

* chore: typo fix in readme

* docs: minor change to build example
  • Loading branch information
nilslice authored Jan 3, 2024
1 parent 21cf17f commit 2b512f9
Showing 1 changed file with 34 additions and 18 deletions.
52 changes: 34 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,32 @@ This library can be used to write [Extism Plug-ins](https://extism.org/docs/conc

## Install

Generate a `exe` project with Zig:
Create a new Zig project:

```bash
mkdir my-plugin
cd ./my-plugin
zig init-exe
cd my-plugin
zig init
```

Add the library as a dependency:

```bash
mkdir -p libs
cd libs
git clone https://github.com/extism/zig-pdk.git
```zig
// build.zig.zon
.{
.name = "my-plugin",
.version = "0.0.0",
.dependencies = .{
.extism_pdk = .{
.url = "https://github.com/extism/zig-pdk/archive/<git-ref-here>.tar.gz",
},
},
.paths = .{""},
}
```

Change your `build.zig` so that it references `extism-pdk`:
Expand All @@ -39,24 +51,23 @@ pub fn build(b: *std.Build) void {
const optimize = b.standardOptimizeOption(.{});
const target = b.standardTargetOptions(.{
.default_target = .{ .abi = .musl, .os_tag = .freestanding, .cpu_arch = .wasm32 },
// If you need to include WASI, update the `.os_tag` field above to .wasi:
// .default_target = .{ .abi = .musl, .os_tag = .wasi, .cpu_arch = .wasm32 },
});
const pdk_module = b.addModule("extism-pdk", .{
.source_file = .{ .path = "libs/zig-pdk/src/main.zig" },
});
var basic_example = b.addExecutable(.{
const pdk_module = b.dependency("extism_pdk", .{ .target = target, .optimize = optimize }).module("extism-pdk");
var plugin = b.addExecutable(.{
.name = "my-plugin",
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
basic_example.addModule("extism-pdk", pdk_module);
basic_example.rdynamic = true;
plugin.addModule("extism-pdk", pdk_module);
plugin.rdynamic = true;
b.installArtifact(basic_example);
const basic_example_step = b.step("my-plugin", "Build my-plugin");
basic_example_step.dependOn(b.getInstallStep());
b.installArtifact(plugin);
const plugin_step = b.step("my-plugin", "Build my-plugin");
plugin_step.dependOn(b.getInstallStep());
}
```

Expand All @@ -82,6 +93,9 @@ export fn greet() i32 {
}
```

> Note: if you started with the generated project files from `zig init`, you should delete `src/root.zig`
and any references to it if they are in your `build.zig` file.

Then run:
```sh
zig build
Expand Down Expand Up @@ -260,6 +274,8 @@ extism call ./zig-out/bin/my-plugin.wasm log_stuff --log-level=debug
Sometimes it is useful to let a plug-in [make HTTP calls](https://pkg.go.dev/github.com/extism/go-pdk#HTTPRequest.Send). [See this example](example/http.go)

```zig
const http = extism_pdk.http;
export fn http_get() i32 {
const plugin = Plugin.init(allocator);
// create an HTTP request via Extism built-in function (doesn't require WASI)
Expand All @@ -281,7 +297,7 @@ export fn http_get() i32 {
}
```

By default, Extism modules cannot make HTTP requests unless you specify which hosts it can connect to. You can use `--alow-host` in the Extism CLI to set this:
By default, Extism modules cannot make HTTP requests unless you specify which hosts it can connect to. You can use `--allow-host` in the Extism CLI to set this:

```
extism call ./zig-out/bin/my-plugin.wasm http_get --allow-host='*.typicode.com'
Expand Down

0 comments on commit 2b512f9

Please sign in to comment.