The best way to control Claude Code, Codex and more from any computer or phone

main
mdx 124 lines 3.42 KB
Raw
1 ---
2 title: "Part 2: Configure the Service"
3 description: Pin the Service's region and port, add a health route, and ship a code change.
4 sidebar:
5 order: 2
6 ---
7
8 import Terminal from "../../../../components/Terminal.astro";
9 import PackageManagers from "../../../../components/PackageManagers.astro";
10
11 In [Part 1](/fly/tutorial/part-1) you deployed a Service on its own
12 `fly.dev` hostname. Now you will pin where it runs and which port it
13 listens on, add a health route, and ship a change.
14
15 ## Pin a region
16
17 Fly Machines live in a [region](/fly/compute/regions). The default
18 is `iad` (Ashburn). Set it explicitly so the Volume you add in
19 Part 3 can match:
20
21 ```diff lang="typescript"
22 export default class Api extends Fly.Service<Api>()(
23 "Api",
24 - { main: import.meta.url },
25 + { main: import.meta.url, region: "iad" },
26 ```
27
28 :::caution[Changing `region` moves the Machine]
29 A new Machine is created in the new region and the old one is
30 deleted; the hostname stays the same. [Regions](/fly/compute/regions)
31 lists the codes.
32 :::
33
34 ## Set the port
35
36 Give the Service an explicit port so the Fly proxy knows where to
37 forward:
38
39 ```diff lang="typescript"
40 export default class Api extends Fly.Service<Api>()(
41 "Api",
42 - { main: import.meta.url, region: "iad" },
43 + { main: import.meta.url, region: "iad", port: 3000 },
44 ```
45
46 The port is written to the process environment as `PORT` and used as
47 the Machine's `internal_port`. By default Alchemy publishes HTTP 80
48 and HTTPS 443 on the Fly proxy in front of it.
49
50 ## Add a health route
51
52 Add `/health` so you (and later, Fly checks) can probe the process:
53
54 ```diff lang="typescript"
55 // src/api.ts
56 +import { HttpServerRequest } from "effect/http/HttpServerRequest";
57 // ...
58 return {
59 - fetch: Effect.succeed(HttpServerResponse.text("Hello from Fly!")),
60 + fetch: Effect.gen(function* () {
61 + const request = yield* HttpServerRequest;
62 + const url = new URL(request.url, "http://service");
63 + if (url.pathname === "/health") {
64 + return HttpServerResponse.json({ ok: true });
65 + }
66 + return HttpServerResponse.text("Hello from Fly!");
67 + }),
68 };
69 ```
70
71 ## Deploy
72
73 <PackageManagers run="alchemy deploy" />
74
75 <Terminal content={`[u]Plan[/u]: [y]1 to update[/y]
76
77 [y]~[/y] [b]Api[/b] [d](Fly.Service)[/d]
78
79 Proceed?
80 ◉ Yes ○ No
81 [g]✓[/g] [b]Api[/b] [d](Fly.Service)[/d] [y]updated[/y]
82 {
83 [s2]appName: "myapp-api-dev-a1b2c3d4",
84 [s2]url: "https://myapp-api-dev-a1b2c3d4.fly.dev",
85 }`} />
86
87 `iad` and port `3000` match the defaults from Part 1, so the Machine
88 stays where it is. The new route changes the bundle hash, so Alchemy
89 builds a new image and updates the Machine in place. The App and its
90 URL stay the same.
91
92 ## Try it out
93
94 ```sh
95 curl https://myapp-api-dev-a1b2c3d4.fly.dev/health
96 # → {"ok":true}
97 ```
98
99 ## Ship a change
100
101 Edit the greeting in `src/api.ts` and deploy again:
102
103 <Terminal content={`[u]Plan[/u]: [y]1 to update[/y]
104
105 [y]~[/y] [b]Api[/b] [d](Fly.Service)[/d]
106
107 Proceed?
108 ◉ Yes ○ No
109 [g]✓[/g] [b]Api[/b] [d](Fly.Service)[/d] [y]updated[/y]`} />
110
111 Alchemy hashes the bundle: if your code didn't change, the Service
112 is a no-op; if it did, a new image is built, pushed, and the Machine
113 updated in place.
114
115 ## Recap
116
117 You now have:
118
119 - An HTTP Service running as a Fly Machine in `iad` on port 3000
120 - A `/health` route on its public `https://{appName}.fly.dev` URL
121 - A code-hash-based update loop — edit, deploy, new image
122
123 In [Part 3](/fly/tutorial/part-3), you'll attach a Volume and
124 persist data across deploys.