Browse the docs

Run your system locally

You will start every service of your workspace with one command, open each one by name in a browser, let the services call each other at stable addresses, and take out a service the others call to run it in a debugger.

putnami serve starts the system: the services you select, the services they call, and their databases. It comes with @putnami/dev, which every language extension installs.

Steps

1) Start the system

From anywhere in the workspace:

putnami serve

With no argument, putnami serve starts every serveable project of the workspace: every project it can serve, which leaves out libraries, images and projects whose serve job is disabled, and, as for every command that names no project and passes no --tag, the projects that carry a tag the workspace's disable.tags lists. In a workspace where some serveable projects are not services, such as command-line programs or extensions, the command starts them too. A program that exits is reported down; one that neither exits nor reports ready holds the start up to its ready timeout, 60 s by default (--ready-timeout). The run then exits 1 when you stop it, since a member is down. For now, start a named system with a tag, or a project, as the next steps show.

It prints the system once every member is ready or has failed, then keeps it running until Ctrl-C. Here is the output of putnami serve /go/samples/service-to-service/consumer-ts, a TypeScript consumer and the Go provider it calls:

  system 7f925d29a8502a21: 2 member(s) (isolation none, primary run)
    index http://localhost:3000/
    /go/samples/service-to-service  http://127.0.0.1:20473  ready in 1057ms  (no injected configuration)
      http://service-to-service.samples.go.localhost:3000/
      http://service-to-service.localhost:3000/
    /go/samples/service-to-service/consumer-ts  http://127.0.0.1:28685  ready in 130ms  (clients, watch)
      http://consumer-ts.service-to-service.samples.go.localhost:3000/
      http://consumer-ts.localhost:3000/

Each member has three ways in: its address, http://127.0.0.1:<port>, for programs, and two names under localhost for people. Each member's own log, on stderr, may print the port its process listens on behind the address, or 0: that port changes at each restart, so use the address or the names.

A member that fails leaves the others running: its address answers 503 and the error names it. A member that watches restarts on your next edit; the others stay down until the next putnami serve.

2) Name a system with a tag

A system is a tag carried by its entry points. Tag each service a person or a test reaches first, in its own putnami.json:

{
  "name": "@acme/storefront",
  "tags": ["journey"]
}

or with the CLI:

putnami projects tag @acme/storefront journey

Then start that system:

putnami serve --tag journey

You do not tag the services the entry points call: putnami serve adds them, as the next step explains. Adding a project to a system touches only that project's files; no list of the system lives in the workspace file.

To start one project and what it calls, name it:

putnami serve @acme/storefront

--projects takes the same selectors as every other command, such as --projects @acme/storefront,@acme/admin.

3) Let putnami bring the upstream

A member's upstream joins the system with it, transitively:

  • the provider of each generated client the member depends on directly: the project whose committed contract declares the client's service;
  • the projects its runsWith names, in its putnami.json (or under putnami.runsWith in a TypeScript package.json), for a service it calls any other way.

A library the member imports adds nothing. The projects you select watch your edits; the upstream they bring runs production-mode without watch. --no-watch turns watch off for every member.

4) Open the index and the names

http://localhost:<front port>/ is the index of the system: each member, its names, its address, its state and its notes. The front port is chosen from 3000 at the first putnami serve of the worktree, and kept for the next ones while it is free; when another program holds it, the run picks the next free port and records it.

Each member answers at two names on that port:

  • its full name, the project's path read backwards: /go/samples/service-to-service/consumer-ts answers at http://consumer-ts.service-to-service.samples.go.localhost:3000/;
  • its short name, the last segment of the path alone, http://consumer-ts.localhost:3000/, when no other serveable project of the workspace ends with the same segment. The index lists the projects that share one; their full names keep working.

No project declares a name or a port. An unknown name answers 404 with the index.

5) Call one service from another

Services do not call each other by name. Each member receives the addresses of its upstream in its configuration, through CONFIG_DATA, under clients.services: under the provider's service id, and under its project name. A generated client reads its URL there, so the consumer needs no URL of its own:

{"clients": {"services": {"items": {"url": "http://127.0.0.1:20473"}}}}

A member's address is derived from its project id, 20000 plus a hash of the id, and kept for the worktree, so it is the same from one putnami serve to the next, and across every restart of the member. A remembered port that another program holds when the system starts is replaced by the next free one, and the member's notes say its address moved. While the member starts or restarts, its address answers 503 with the readiness envelope, so a caller sees an unavailable service rather than a refused connection.

6) Debug a service the others call

Exclude the service you want to run yourself, here the Go provider the consumer calls:

putnami serve /go/samples/service-to-service/consumer-ts --exclude /go/samples/service-to-service

putnami serve starts the rest of the system and not the provider, but keeps its address: the services that call it still receive that address. The output and the index name the command that starts it there:

    /go/samples/service-to-service  http://127.0.0.1:20473  excluded: start it there with `putnami serve /go/samples/service-to-service --port 20473`

This works for a project a selected member calls: an excluded project that no member calls gets no address and no command. Run that command in a second shell: it is a secondary run (step 7), so the service gets a database that is dropped when it stops, and the services it calls start again on ephemeral addresses. Or start it on that port in your debugger, with its configuration in your hands: putnami serve does not yet start an excluded service with the system's configuration.

7) Run a second system in the same worktree

The first putnami serve of a worktree is its primary run: it holds the worktree's addresses, its front port and its databases. A second putnami serve in the same worktree, while the first runs, is a secondary run: its members take ephemeral ports, it opens no index and no names, and each of its datasources gets a database of its own that it drops when it stops. It says secondary run on its first line. putnami qualify --target local always runs that way, so you can qualify a workload while your system is served.

Another worktree of the same repository is primary on its own: it gets its own front port, its own addresses and its own databases, so two branches run side by side.

8) Keep or reset the databases

Each datasource a member declares in its committed infra/requirements.json gets a PostgreSQL database for the worktree, which the next putnami serve finds again, with its data, as long as the database server lives. Workloads apply their own migrations at boot. To start the selected members from empty databases:

putnami serve --tag journey --reset-databases

With PUTNAMI_TEST_PG_URL set, members share the database you provide, nothing is created or dropped, and --reset-databases is refused.

9) Reach a service from a container

The addresses listen on 127.0.0.1 only. A program in a Docker Desktop container reaches the host as host.docker.internal, so it calls a member at http://host.docker.internal:<member port>, such as http://host.docker.internal:20473: the address accepts that name. The .localhost names do not resolve inside a container, and the index refuses host.docker.internal, so use the member's port. On Linux, a container on Docker's default bridge network does not reach a host listener on 127.0.0.1; run it with --network host and call http://127.0.0.1:20473.

How the .localhost names work

No DNS server and no file of yours is involved. RFC 6761 reserves the localhost name and every name under it for the machine itself, and browsers and curl send every *.localhost request to the machine's loopback, 127.0.0.1 or ::1, on their own; putnami serve listens on 127.0.0.1 and picks only ports that are also free on ::1, so a client that tries ::1 first falls back to 127.0.0.1. So consumer-ts.localhost:3000 and service-to-service.localhost:3000 both reach the same port, the index's.

One listener on that port serves every member. A request keeps the name it was sent to in its Host header, and the listener reads that header to pick the member, then forwards the request to the member's address. localhost:3000 with no name in front answers the index.

Services call each other at 127.0.0.1:<port> instead because not every runtime promises to resolve *.localhost: Go, Bun and containers without a resolver rule may not. An address with a number in it resolves everywhere.

Both the index and the addresses refuse a request whose Host is not local, with 421. A web page on another site can point its own name at 127.0.0.1 (DNS rebinding) to reach services on your machine; its requests still carry its own name in Host, so they reach no member and read no index. The addresses accept 127.0.0.1, localhost, ::1, any *.localhost and host.docker.internal; the index accepts localhost, 127.0.0.1, ::1 and any *.localhost.

Result

You now have one command that starts your system with its databases, a page that lists every service, a name for each in your browser, addresses that stay the same between runs, and a way to take out a service the others call to debug it.

See Qualify a workload for the proof that runs before a pull request.