Go and net/http
Two Go programs in one module, written with the standard library. devlanes runs api from go.mod and web through a Taskfile task that runs air, which rebuilds web on every save. The web page calls api over HTTPS through the proxy and trusts the devlanes CA without turning certificate checks off.
Requirements
Section titled “Requirements”- devlanes, with
devlanes setuprun once - Go 1.26 or later
- Task (
taskonPATH)
The Taskfile runs a pinned air with go run github.com/air-verse/air@v1.67.4, which fetches and builds it on the first start.
Run it
Section titled “Run it”cd languages-go-apidevlanes startdevlanes start --no-ui prints the start as text:
example-go-api@0 (main): 2/2 readyready web http://127.0.0.1:20621 https://example-go-api.localhostready api http://127.0.0.1:20620 https://api.example-go-api.localhostOpen https://example-go-api.localhost. The page says This is lane 0 of example-go-api., lists the lane values, then shows the answer of the api service with its status, the latency of the call and the SSL_CERT_FILE path.
The first start downloads and builds air and both programs. If devlanes start stops waiting before that ends (after 180s by default), it prints ! timed out before every unit was ready and the build goes on; devlanes wait example-go-api@0 --timeout 10m waits for it.
During a start
Section titled “During a start”- devlanes runs
go run ./cmd/apiin this folder.run_scriptpoints atgo.modand a bare name is a folder undercmd/. The file also makes the platformgo, soapigetsSSL_CERT_FILEpointing at~/.devlanes/ca/bundle.pem(system roots plus the devlanes CA). - At the same time it runs
task --taskfile Taskfile.yml dev:web. Thego.modnext to the Taskfile makes this service’s platformgotoo, sowebgets the sameSSL_CERT_FILE, andBUILD_DIRis the lane’s data folder. - The task runs air, which builds
./cmd/webintoBUILD_DIRand starts it. air keeps its own files there too, so nothing is written to the worktree and two lanes running from this folder never share a binary. - Both listen on
HOSTandPORT. Theready.httpcheck on/healthzgives each 10 minutes for the first build. webanswers at https://example-go-api.localhost andapiat https://api.example-go-api.localhost.API_URLis${{ services.api.public_url }}, so each page load callshttps://api.example-go-api.localhost/itemsthrough the proxy.
Trusting the devlanes CA from Go
Section titled “Trusting the devlanes CA from Go”Go’s default HTTP client verifies certificates with the operating system. On macOS and Windows that is the system store, which devlanes setup fills. On Linux Go reads the file in SSL_CERT_FILE instead of the distribution’s bundle, and the go platform points it at the devlanes bundle, so the call works on a box or in CI where the CA is not in the system store.
On Linux, a Go program built without cgo looks the api’s hostname up in DNS. If the page shows no such host, Fix programs that cannot resolve *.localhost makes *.localhost resolve. On macOS and Windows Go asks the operating system.
Try next
Section titled “Try next”- Change the heading in
internal/web/templates/home.htmland save. air logshas changed, rebuildsweband restarts it; reload the page to see the change. - Change the stock of
Kettleininternal/api/items.gofrom 12 to 11 and rundevlanes restart api.go runrebuilds only what changed, and the next page load shows"stock": 11. - Load the page a few times, then run
devlanes logs web --tail 3. Each page load logs the api’s status and how long the call took. devlanes lane new --name example-go-api-review --startstarts a second lane from this same folder. https://example-go-api1.localhost answers with lane 1, and air builds its ownwebbinary in that lane’s data folder, so the two lanes never share one.- Stop the api with
devlanes stop apiand reload the page. The card shows503 Service Unavailableand the proxy’sService not runningpage as the body.devlanes start apibrings it back.go runexits with 1 when its program is stopped, sodevlanes stopreportsexit=1. - Start a lane in a second worktree:
git worktree add ../examples-checkout -b feat/checkout, thencd ../examples-checkout/languages-go-api && devlanes start. With the review lane running it takes the next free index, 2, so the page is at https://example-go-api2.localhost and its api call goes tohttps://api.example-go-api2.localhost. Go’s build cache is shared, so it builds quickly. devlanes lane clean example-go-api-review --yesstops the review lane and deletes its data folder, with thewebbinary in it.devlanes lane clean 0 --yesdoes the same for lane 0, and the second worktree’s lane has its own index.
devlanes.yaml: thewebandapiservices, their run scripts, hostnames and ready checks, andBUILD_DIRandAPI_URL.Taskfile.yml: thedev:webtask that runs air.go.mod: the module, with no dependencies.cmd/: themainpackage of each program, which only starts the server.internal/:configreads the environment,apiandwebhold the handlers,web/client.gothe HTTPS call andweb/templates/the page.