Skip to content

Commit

Permalink
feat: fail more gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
stepaniukm committed May 10, 2024
1 parent f123e2f commit 11caf80
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 26 deletions.
5 changes: 3 additions & 2 deletions apps/api/.example.env
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
EDGEDB_AUTH_BASE_URL=
PORT=
BASE_URL=
PORT=3001
BASE_URL=http://localhost:${PORT}
LOG_LEVEL=info
2 changes: 1 addition & 1 deletion apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"dependencies": {
"@effect/platform": "^0.53.2",
"@effect/platform-bun": "^0.34.7",
"@effect/platform-bun": "^0.34.8",
"@effect/schema": "^0.66.16",
"edgedb": "^1.5.5",
"effect": "^3.1.3",
Expand Down
14 changes: 14 additions & 0 deletions apps/api/src/cors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { HttpServer } from '@effect/platform';
import { Effect } from 'effect';

export const corsMiddleware = HttpServer.middleware.make((app) => {
return app.pipe(
Effect.andThen(
HttpServer.response.setHeaders({
'Access-Control-Allow-Credentials': 'true',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Origin': 'http://localhost:3000',
}),
),
);
});
11 changes: 11 additions & 0 deletions apps/api/src/health/router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { HttpServer } from '@effect/platform';
import { Config, Effect } from 'effect';

export const HealthRouter = HttpServer.router.empty.pipe(
HttpServer.router.get(
'/health',
Effect.map(Config.string('xD'), (v) =>
HttpServer.response.text(v, { status: 204 }),
),
),
);
42 changes: 24 additions & 18 deletions apps/api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { BunHttpServer, BunRuntime } from '@effect/platform-bun';
import { HttpServer } from '@effect/platform';
import { Config, Console, Effect, Layer } from 'effect';
import { Config, Console, Effect, Layer, LogLevel, Logger } from 'effect';
import { EDGEDB_AUTH_TOKEN_COOKIE } from './auth/consts';
import { AuthRouter } from './auth/router';
import { corsMiddleware } from './cors';
import { HealthRouter } from './health/router';
import { LogLevelLive } from './logging';

const ServerLive = BunHttpServer.server.layerConfig({
port: Config.number('PORT').pipe(Config.withDefault(3001)),
Expand All @@ -24,30 +27,33 @@ const MainRouter = HttpServer.router.empty.pipe(
HttpServer.response.text('Slept for 1 second'),
),
),
HttpServer.router.get('/health', HttpServer.response.empty({ status: 204 })),
);

const myMiddleware = HttpServer.middleware.make((app) => {
return app.pipe(
Effect.andThen(
HttpServer.response.setHeaders({
'Access-Control-Allow-Credentials': 'true',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Origin': 'http://localhost:3000',
}),
),
);
});
const routers = [MainRouter, AuthRouter, HealthRouter];

const WholeRouter = MainRouter.pipe(HttpServer.router.concat(AuthRouter));
const WholeRouter = routers.reduce((acc, router) =>
acc.pipe(HttpServer.router.concat(router)),
);

const HttpLive = WholeRouter.pipe(
myMiddleware,
const runnable = WholeRouter.pipe(
corsMiddleware,
Effect.catchAll((error) =>
Effect.gen(function* () {
yield* Console.error('Error', error);
return HttpServer.response.text('Error', { status: 500 });
}),
),
Effect.catchAllDefect((defect) =>
Effect.gen(function* () {
yield* Console.error('Defect', defect);
return HttpServer.response.text('Defect', { status: 500 });
}),
),
HttpServer.server.serve(HttpServer.middleware.logger),
HttpServer.server.withLogAddress,
Layer.provide(ServerLive),
Layer.launch,
Effect.provide(LogLevelLive),
);

const runnable = Layer.launch(HttpLive);

BunRuntime.runMain(runnable);
7 changes: 7 additions & 0 deletions apps/api/src/logging.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Config, Effect, Layer, LogLevel, Logger } from 'effect';

export const LogLevelLive = Config.logLevel('LOG_LEVEL').pipe(
Config.withDefault(LogLevel.Info),
Effect.andThen((level) => Logger.minimumLogLevel(level)),
Layer.unwrapEffect,
);
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 11caf80

Please sign in to comment.