@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
mdx 163 lines 4.59 KB
Raw
1 ---
2 title: "Part 4: Variables and Cleanup"
3 description: Store a Project variable, read it from the Service at runtime, and destroy the stack.
4 sidebar:
5 order: 4
6 ---
7
8 import Terminal from "../../../../components/Terminal.astro";
9 import PackageManagers from "../../../../components/PackageManagers.astro";
10
11 A secret only this Service needs from `.env` is
12 `Config.Redacted`. See [Variables](/railway/data/variables).
13
14 This part is for a value Railway should own: a **Variable** Railway
15 injects into services. You will read it with `Config`, then tear
16 the stack down so billing stops.
17
18 ## Declare a Variable
19
20 Add a Variable next to the Project. Wrap the value with
21 `Redacted.make` so it is never logged:
22
23 ```diff lang="typescript"
24 // src/project.ts
25 import * as Railway from "@alchemy.run/railway";
26 +import * as Redacted from "effect/Redacted";
27
28 export const Site = Railway.Project("Site");
29
30 export const Data = Railway.Volume("Data", { /* ... */ });
31 +
32 +export const ApiToken = Railway.Variable("ApiToken", {
33 + project: Site,
34 + name: "API_TOKEN",
35 + value: Redacted.make("not-a-real-token"),
36 +});
37 ```
38
39 `name` is the env-var Railway injects into services (`API_TOKEN`).
40 Omit it and Alchemy generates an ownership-stamped name instead.
41
42 ## Read it from the Service
43
44 Railway injects the Variable as env. Yield it in the constructor so it is
45 part of the graph, then read the name with `Config` — never the
46 plaintext:
47
48 ```diff lang="typescript"
49 // src/api.ts
50 -import { Data, Site } from "./project.ts";
51 +import { ApiToken, Data, Site } from "./project.ts";
52 +import * as Config from "effect/Config";
53
54 Effect.gen(function* () {
55 const mount = yield* Railway.MountVolume(Data, { path: "/data" });
56 const fs = yield* FileSystem.FileSystem;
57 + yield* ApiToken;
58 + const tokenName = "API_TOKEN";
59 + const token = yield* Config.String(tokenName).pipe(
60 + Effect.orElseSucceed(() => ""),
61 + );
62
63 return {
64 fetch: /* ... */,
65 };
66 }).pipe(Effect.provide(Railway.MountVolumeLive)),
67 ```
68
69 ## Serve the variable's name
70
71 Add a `/secret` route that returns the variable's **name**, never
72 the plaintext:
73
74 ```typescript
75 if (url.pathname === "/health") {
76 return HttpServerResponse.json({ ok: true });
77 }
78 if (url.pathname === "/secret") {
79 return HttpServerResponse.json({
80 name: tokenName,
81 set: token.length > 0,
82 });
83 }
84 const file = `${mount.path}${url.pathname}`;
85 ```
86
87 ## Yield the Variable from the Stack
88
89 ```diff lang="typescript"
90 // alchemy.run.ts
91 -import { Data, Site } from "./src/project.ts";
92 +import { ApiToken, Data, Site } from "./src/project.ts";
93
94 Effect.gen(function* () {
95 const site = yield* Site;
96 yield* Data;
97 + yield* ApiToken;
98 const api = yield* Api;
99 ```
100
101 ## Deploy
102
103 <PackageManagers run="alchemy deploy" />
104
105 <Terminal content={`[u]Plan[/u]: [g]1 to create[/g], [y]1 to update[/y]
106
107 [g]+[/g] [b]ApiToken[/b] [d](Railway.Variable)[/d]
108 [y]~[/y] [b]Api[/b] [d](Railway.Service)[/d]
109
110 Proceed?
111 ◉ Yes ○ No
112 [g]✓[/g] [b]ApiToken[/b] [d](Railway.Variable)[/d] [g]created[/g]
113 [g]✓[/g] [b]Api[/b] [d](Railway.Service)[/d] [y]updated[/y]`} />
114
115 ## Try it out
116
117 ```sh
118 curl https://myapp-api-dev-a1b2c3d4.up.railway.app/secret
119 # → {"name":"API_TOKEN","set":true}
120 ```
121
122 :::tip[Your own domain?]
123 Declare a [`Railway.CustomDomain`](/railway/networking#custom-domains)
124 for a hostname you control and point DNS at
125 `verificationDnsHost` / `verificationToken`. Postgres and Redis on
126 the private network stay on `{name}.railway.internal`; a
127 [`TcpProxy`](/railway/networking#tcp-proxies) is what a laptop uses.
128 :::
129
130 ## Clean up
131
132 You're done — tear everything down so Service and Volume billing
133 stops:
134
135 <PackageManagers run="alchemy destroy" />
136
137 Alchemy deletes everything in reverse dependency order — Service,
138 Variable, Volume, Project.
139
140 ## Recap
141
142 Over four parts you built a complete Railway deployment:
143
144 - A Project with a generated unique name and a production environment
145 - An HTTP Service bundled into a container, updated only when its
146 code hash changes
147 - A Volume mounted at `/data` whose contents outlive deploys
148 - A Variable readable from the Service, never logged
149
150 ## Where next
151
152 - [Services](/railway/compute/services) — background workers, env,
153 multiple Services per Project, image-based Services.
154 - [Volumes](/railway/data/volumes) — attach at create time, region
155 rules.
156 - [Variables](/railway/data/variables) — `Config.Redacted` vs
157 `Railway.Variable`.
158 - [Postgres](/railway/data/postgres) — bind `ConnectPostgres` and
159 query with Drizzle.
160 - [Testing](/testing) — deploy this stack from an integration test
161 and drive it over HTTP.
162 - [CI](/environments/ci) — run `alchemy deploy` from GitHub Actions
163 with `RAILWAY_API_TOKEN`.