POST /links mints a short code and GET /s/:code redirects to the
original URL.
Create the project
A iii project is a directory with aconfig.yaml file that describes your system. Create one:
Add the workers you’ll need
Later in this chapter you’ll serve thelink worker over HTTP (provided by http) and stash
short-code → URL mappings in a key-value store (provided by state). Declare both now so they’re
ready when the link worker reaches for them:
worker-compose.yaml
engine: section makes this Compose invocation own the engine. Project workers belong only
under containers:.
state uses an in-memory store by default, so every restart starts clean. That’s what we want for
this chapter. A worker’s settings live in a per-worker file under config/, and the engine only
generates those files the first time it starts, so you’ll make that default explicit in
config/state.yaml when Compose starts the worker, a few steps from now.
Since the store is in-memory, every restart clears the data we’re storing. That’s fine here;
Ch. 3: Persist everything swaps in durable storage.
The exact versions in worker-compose.yaml make starts repeatable.
Create the link worker
Create a TypeScript worker directory inside the project. This worker will handle storing and retrieving short links:Configure the entrypoints
A worker is a self-contained service. Here, thelink worker is a Node package but it could be any
language or runtime.
link/iii.worker.yaml is the manifest that describes how the worker runs itself. Create it with
the following content:
iii.worker.yaml
Compose runs the worker from
scripts.start, but a worker can also be an ordinary service. Any
process that uses a iii SDK and calls registerWorker() is a worker.Learn more about the iii.worker.yaml manifest.
link/package.json with this content. The start script uses tsx watch, which runs the
TypeScript source directly and reloads the worker whenever you save a change.
package.json
Write the worker entry point
link/src/index.ts is the worker’s entry point. You’ll build it up in a few small steps rather than
pasting one large file at once.
The command above created an empty
index.ts. Add the first snippet below, then append each later
snippet to the end of the file.Open the connection to the engine
registerWorker opens the connection to the engine. Replace the template’s example code in
index.ts with the connection setup and a small helper that generates random short codes:
src/index.ts
Add link::create
registerFunction publishes a function under a name like link::create that anything else on the
engine can call. This one stores the mapping by calling state::set on the state worker through
worker.trigger. Worker-to-worker calls always flow through the engine, so the link worker
doesn’t import anything from state; it knows the function name. Append it:
src/index.ts
Add link::resolve
link::resolve looks the mapping back up with state::get, returning the URL or null when the
code is unknown. Append it, with a final log line so you can see the worker come up:
src/index.ts
state::set / state::get calls pass a scope (links) and a key (the short code). Scopes
keep different kinds of data in state from colliding; later chapters add more.
Start the engine
From the project root, start the engine and Compose project. The workers register their functions with the engine.--up is a iii compose flag, as shown in the generated
CLI reference:
config/state.yaml and config/http.yaml
from each Compose container’s defaults and config_override. From here on, edit a worker’s settings
in its config/<worker>.yaml file; the worker hot-reloads supported changes.
Make the in-memory store explicit
Now that Compose generatedconfig/state.yaml, open it. The config_override you declared is
present under value:
config/state.yaml
Register the worker
Keep the engine/Compose terminal open. From another terminal at the rootlinkly directory, add the
local link worker. The -n flag is the documented short form of --namespace for
iii trigger:
link worker register link::create and
link::resolve. The output will look something like this:
Call the functions
iii trigger invokes a function on the running engine. Create a link with a custom code:
null:
You have a working domain worker.
link::create and link::resolve are registered with the
engine and callable from anywhere within your iii system. Next let’s put them behind HTTP so that
external 3rd party systems could use them.As you’ll see later, unless you’re supporting 3rd party systems it isn’t necessary to expose
services over http since iii can even run browser tabs as workers.
Expose your functions over HTTP
A function becomes an HTTP endpoint when you bind it to anhttp trigger. That trigger type is
served by the http worker you added at the start of the chapter.
Create a function to handle new links
Addhttp::create to the bottom of link/src/index.ts. It validates the request body, calls
link::create through the engine with worker.trigger, and returns the new link:
src/index.ts
Bind your create function to a Trigger
In the same file (link/src/index.ts) at the end bind http::create to POST /links with a new
trigger. This Trigger has the http worker listen for POST requests to /links and when it
receives one it will run the function specified by function_id.
src/index.ts
This is the first Trigger you’ve registered yourself. In iii, Triggers control what causes
something to happen. In this case an http request causes a function to run. Learn more about
Using iii / Triggers.
Every function registered comes with its own Trigger which is why
worker.trigger worked earlier
without a declaration.Mint a link over HTTP
Save the file and the worker reloads with the new route registered. In this project,config/http.yaml configures the http container to listen on 127.0.0.1:3111. This Compose
setup does not start a separate in-process engine HTTP server. The http container provides the
project HTTP API and owns the route below. Now try out your new Trigger:
state now, but GET /s/demo has nowhere to go yet. There’s no handler; add one
next.
Create a function to handle redirects
Addhttp::redirect to the bottom of link/src/index.ts. It looks up the short code via
link::resolve, returns a 404 when there’s no match, and a 302 to the original URL otherwise:
src/index.ts
Bind your redirect function to a Trigger
Like before, bindhttp::redirect to GET /s/:code with a new Trigger:
src/index.ts
Follow the short code
state is in-memory in this chapter, so each time the engine restarts the previous link is gone.
Chapter 3 swaps in durable storage. For now create a fresh link and try it out:
404:
Conclusion
You have built a real link shortener: a domain worker exposed over HTTP, where the samelink::create and link::resolve functions serve both the command line and the web. Restarting the
engine still clears every link, though: state is in-memory until Chapter 3 swaps it for durable
storage.
Next, in Ch. 2: Observe everything, you will add logs and traces
and watch invocations flow through the engine in the console.