Part 3: Keep Files on a Volume
Your Service from Part 2 keeps nothing of its own: what it writes to the server’s disk is gone when the server is replaced. In this part you attach a volume, Better Stack Cloud’s NVMe block storage, and mount it into the Service. A volume is attached to one server at a time and can move to another, so its files outlive deploys and the server.
Declare a volume
Section titled “Declare a volume”Add a volume next to the server in src/server.ts:
}; }),);
export const Data = BetterStack.Cloud.Volume( "Data", Effect.gen(function* () { const server = yield* Box; return { serverId: server.id, sizeGb: 10, format: "ext4" as const }; }),);sizeGb is in GiB, from 1 to 3072, and the volume bills per allocated GiB whether it is attached or not. It is created attached to serverId. format: "ext4" gives it a filesystem, which a mount needs. A volume grows in place but never shrinks. See Volumes.
Mount it into the Service
Section titled “Mount it into the Service”Inside the Service’s program, bind the volume with Cloud.MountVolume, and provide its layer:
import * as Effect from "effect/Effect";import { HttpServerRequest } from "effect/http/HttpServerRequest";import * as HttpServerResponse from "effect/http/HttpServerResponse";import { Box, Data } from "./server.ts";import { Box } from "./server.ts";
export default class Api extends BetterStack.Cloud.Service<Api>()( "Api", { server: Box, main: import.meta.url, port: 3000 }, Effect.gen(function* () { const mount = yield* BetterStack.Cloud.MountVolume(Data, { path: "/data" });
return { fetch: Effect.gen(function* () { const request = yield* HttpServerRequest;// … return HttpServerResponse.text("Hello from Better Stack Cloud!"); }), }; }).pipe(Effect.provide(BetterStack.Cloud.MountVolumeLive)), }),) {}MountVolume is a binding. On deploy it tells the Service to attach the volume to its server and mount it at /data; while the Service runs, it gives you mount.path. MountVolumeLive is the layer that implements it, provided once on the program.
Write files with PUT
Section titled “Write files with PUT”Use the mount from the handler. A volume is just a directory, so Effect’s FileSystem writes to it:
import * as BetterStack from "betterstack-alchemy";import * as Effect from "effect/Effect";import * as FileSystem from "effect/FileSystem";import { HttpServerRequest } from "effect/http/HttpServerRequest";import * as HttpServerResponse from "effect/http/HttpServerResponse";import { Box, Data } from "./server.ts";// … if (url.pathname === "/health") { return yield* HttpServerResponse.json({ ok: true }); } const fs = yield* FileSystem.FileSystem; const file = `${mount.path}${url.pathname}`;
if (request.method === "PUT") { const body = yield* request.text; yield* fs.writeFileString(file, body); return HttpServerResponse.empty({ status: 204 }); }
return HttpServerResponse.text("Hello from Better Stack Cloud!"); }).pipe(Effect.orDie), }), }; }).pipe(Effect.provide(BetterStack.Cloud.MountVolumeLive)),) {}PUT /hello.txt writes the request body to /data/hello.txt. Effect.orDie turns a filesystem failure into a defect, so the handler has no typed error to handle.
Read files with GET
Section titled “Read files with GET”Add the read branch, with a 404 for a file that isn’t there:
return HttpServerResponse.empty({ status: 204 }); }
if (request.method === "GET") { const text = yield* fs.readFileString(file).pipe(Effect.orElseSucceed(() => undefined)); if (text === undefined) { return HttpServerResponse.text("Not found", { status: 404 }); } return HttpServerResponse.text(text); }
return HttpServerResponse.text("Hello from Better Stack Cloud!"); }).pipe(Effect.orDie),};Surface the volume
Section titled “Surface the volume”Yield the volume in the Stack and return its id:
import * as BetterStack from "betterstack-alchemy";import * as Effect from "effect/Effect";import Api from "./src/api.ts";import { Box, Data } from "./src/server.ts";import { Box } from "./src/server.ts";
export default Alchemy.Stack( "MyApp", { providers: BetterStack.providers(), state: Alchemy.localState() }, Effect.gen(function* () { const server = yield* Box; const volume = yield* Data; const api = yield* Api;
return { ipv4: server.ipv4, url: api.url, volumeId: volume.id, }; }),);Deploy
Section titled “Deploy”bun alchemy deploypnpm alchemy deploynpx alchemy deployThe plan creates Data and updates Api. During the Service’s deploy, Alchemy makes sure the volume is attached to the server, then over SSH creates /data, mounts the device, and adds an /etc/fstab line so the mount survives reboots. The systemd unit gets RequiresMountsFor=/data, so it starts only once the volume is mounted. Every step is idempotent.
Try it out
Section titled “Try it out”# Store a file on the volumecurl -X PUT http://<ipv4>:3000/hello.txt -d 'Hello, volume!'
# Read it backcurl http://<ipv4>:3000/hello.txt# → Hello, volume!Ship another code change and deploy: the file is still there. The volume is its own resource with its own lifecycle, and deleting the Service unmounts it but leaves it attached.
You now have:
- A 10 GiB volume attached to your server, mounted at
/data, with an fstab line for reboots - A Service reading and writing it through Effect’s
FileSystem - Files that outlive deploys
Next, Part 4 puts Better Stack to work on its own product: a monitor, a status page, a heartbeat, the app’s own traces, logs and metrics, and error tracking for the app you built.