|
1 | 1 | import { Hono, type Context } from "hono" |
2 | 2 | import { describeRoute, resolver, validator } from "hono-openapi" |
3 | 3 | import { streamSSE } from "hono/streaming" |
| 4 | +import { Effect } from "effect" |
4 | 5 | import z from "zod" |
5 | 6 | import { BusEvent } from "@/bus/bus-event" |
6 | 7 | import { SyncEvent } from "@/sync" |
7 | 8 | import { GlobalBus } from "@/bus/global" |
| 9 | +import { AppRuntime } from "@/effect/app-runtime" |
8 | 10 | import { AsyncQueue } from "@/util/queue" |
9 | 11 | import { Instance } from "../../project/instance" |
10 | 12 | import { Installation } from "@/installation" |
@@ -290,25 +292,41 @@ export const GlobalRoutes = lazy(() => |
290 | 292 | }), |
291 | 293 | ), |
292 | 294 | async (c) => { |
293 | | - const method = await Installation.method() |
294 | | - if (method === "unknown") { |
295 | | - return c.json({ success: false, error: "Unknown installation method" }, 400) |
296 | | - } |
297 | | - const target = c.req.valid("json").target || (await Installation.latest(method)) |
298 | | - const result = await Installation.upgrade(method, target) |
299 | | - .then(() => ({ success: true as const, version: target })) |
300 | | - .catch((e) => ({ success: false as const, error: e instanceof Error ? e.message : String(e) })) |
301 | | - if (result.success) { |
302 | | - GlobalBus.emit("event", { |
303 | | - directory: "global", |
304 | | - payload: { |
305 | | - type: Installation.Event.Updated.type, |
306 | | - properties: { version: target }, |
307 | | - }, |
308 | | - }) |
309 | | - return c.json(result) |
| 295 | + const result = await AppRuntime.runPromise( |
| 296 | + Installation.Service.use((svc) => |
| 297 | + Effect.gen(function* () { |
| 298 | + const method = yield* svc.method() |
| 299 | + if (method === "unknown") { |
| 300 | + return { success: false as const, status: 400 as const, error: "Unknown installation method" } |
| 301 | + } |
| 302 | + |
| 303 | + const target = c.req.valid("json").target || (yield* svc.latest(method)) |
| 304 | + const result = yield* Effect.catch( |
| 305 | + svc.upgrade(method, target).pipe(Effect.as({ success: true as const, version: target })), |
| 306 | + (err) => |
| 307 | + Effect.succeed({ |
| 308 | + success: false as const, |
| 309 | + status: 500 as const, |
| 310 | + error: err instanceof Error ? err.message : String(err), |
| 311 | + }), |
| 312 | + ) |
| 313 | + if (!result.success) return result |
| 314 | + return { ...result, status: 200 as const } |
| 315 | + }), |
| 316 | + ), |
| 317 | + ) |
| 318 | + if (!result.success) { |
| 319 | + return c.json({ success: false, error: result.error }, result.status) |
310 | 320 | } |
311 | | - return c.json(result, 500) |
| 321 | + const target = result.version |
| 322 | + GlobalBus.emit("event", { |
| 323 | + directory: "global", |
| 324 | + payload: { |
| 325 | + type: Installation.Event.Updated.type, |
| 326 | + properties: { version: target }, |
| 327 | + }, |
| 328 | + }) |
| 329 | + return c.json({ success: true, version: target }) |
312 | 330 | }, |
313 | 331 | ), |
314 | 332 | ) |
0 commit comments