Crash fixes

This commit is contained in:
2024-08-06 22:11:06 +02:00
parent 4210f2168a
commit a5b16cbdf9
6 changed files with 23 additions and 18 deletions

1
.gitignore vendored
View File

@@ -1,3 +1,4 @@
db.sqlite3
dist dist
node_modules node_modules
storage storage

View File

@@ -1,5 +1,5 @@
import { RequestId } from "common"; import { RequestId } from "common";
import { Effect, Exit, Layer, Option as O, pipe, Scope, unsafeCoerce } from "effect"; import { Effect, Exit, Layer, ManagedRuntime, Option as O, pipe, Scope, unsafeCoerce } from "effect";
import { ulid } from "ulid"; import { ulid } from "ulid";
import { app } from "./app"; import { app } from "./app";
import { DatabaseLive } from "./services/database"; import { DatabaseLive } from "./services/database";
@@ -8,16 +8,11 @@ import { StorageFilesystem } from "./services/storage";
const scope = Effect.runSync(Scope.make()); const scope = Effect.runSync(Scope.make());
const layers = pipe( const runtime = pipe(
Layer.merge( StorageFilesystem("storage"),
StorageFilesystem("storage"), Layer.merge(DatabaseLive()),
pipe( Layer.provide(Layer.succeed(Scope.Scope, scope)),
DatabaseLive(), ManagedRuntime.make,
Layer.provide(Layer.succeed(Scope.Scope, scope)),
),
),
Layer.memoize,
Effect.provideService(Scope.Scope, scope),
); );
const server = Bun.serve({ const server = Bun.serve({
@@ -50,10 +45,9 @@ const server = Bun.serve({
}); });
const fiber = pipe( const fiber = pipe(
layers, app,
Effect.flatMap((layers) => Effect.provide(app, layers)),
Effect.provideService(Request, requestInterface), Effect.provideService(Request, requestInterface),
Effect.runFork, runtime.runFork,
); );
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@@ -69,6 +63,9 @@ const server = Bun.serve({
}); });
process.on("SIGINT", () => { process.on("SIGINT", () => {
Effect.withFiberRuntime((fiber) => Scope.close(scope, Exit.interrupt(fiber.id()))); Effect.runFork(Effect.gen(function* () {
yield* runtime.disposeEffect;
yield* Scope.close(scope, Exit.void);
}));
server.stop(); server.stop();
}); });

View File

@@ -62,7 +62,7 @@ export const DatabaseLive = (filePath: string = "db.sqlite3") => Layer.effect(Da
db.exec(`CREATE TABLE IF NOT EXISTS Session ( db.exec(`CREATE TABLE IF NOT EXISTS Session (
sessionId TEXT NOT NULL PRIMARY KEY, sessionId TEXT NOT NULL PRIMARY KEY,
userId TEXT NOT NULL REFERENCES User (userId) ON DELETE CASCADE ON UPDATE CASCADE, userId TEXT NOT NULL REFERENCES User (userId) ON DELETE CASCADE ON UPDATE CASCADE,
expiresAt TEXT NOT NULL, expiresAt TEXT NOT NULL
)`); )`);
db.exec(`CREATE TABLE IF NOT EXISTS Attachment ( db.exec(`CREATE TABLE IF NOT EXISTS Attachment (

View File

@@ -29,7 +29,7 @@ export default () => {
Effect.orDie, Effect.orDie,
); );
const res = yield* Effect.promise((signal) => fetch("/api/login", { const res = yield* Effect.promise((signal) => fetch("http://localhost:3000/login", {
method: "POST", method: "POST",
body: requestJson, body: requestJson,
headers: { "Content-Type": "application/json" }, headers: { "Content-Type": "application/json" },

View File

@@ -1,6 +1,13 @@
import { vanillaExtractPlugin } from "@vanilla-extract/vite-plugin"; import { vanillaExtractPlugin } from "@vanilla-extract/vite-plugin";
import path from "node:path";
/** @type {import("vite").UserConfig} */ /** @type {import("vite").UserConfig} */
export default { export default {
plugins: [vanillaExtractPlugin()], plugins: [vanillaExtractPlugin()],
resolve: {
alias: {
"common": path.resolve(__dirname, "../common/src"),
"make-api": path.resolve(__dirname, "../make-api/src"),
},
},
}; };

View File

@@ -30,7 +30,7 @@
"common": ["./packages/common/src/index.ts"], "common": ["./packages/common/src/index.ts"],
"common/api": ["./packages/common/src/api.ts"], "common/api": ["./packages/common/src/api.ts"],
"common/db": ["./packages/common/src/db.ts"], "common/db": ["./packages/common/src/db.ts"],
"make-api": ["./packages/make-api/index.ts"], "make-api": ["./packages/make-api/src/index.ts"],
}, },
}, },
"include": ["${configDir}/src"], "include": ["${configDir}/src"],