Add basic http router

Update api
This commit is contained in:
Yadunand Prem 2025-08-05 14:26:28 +08:00
parent 3f78eb1a32
commit b24ced7bc9
5 changed files with 92 additions and 1 deletions

1
.gitignore vendored
View File

@ -4,6 +4,7 @@
!devenv.nix
!devenv.yaml
!docker-compose.yml
!biome.json
!api/
!api/package.json

View File

@ -1 +0,0 @@
console.log("Hello via Bun!");

49
api/src/index.ts Normal file
View File

@ -0,0 +1,49 @@
import {
HttpApi,
HttpApiBuilder,
HttpApiEndpoint,
HttpApiGroup,
} from "@effect/platform";
import { BunHttpServer, BunRuntime } from "@effect/platform-bun";
import { Console, Context, Effect, Layer, Schema } from "effect";
const MyApi = HttpApi.make("MyAPI").add(
HttpApiGroup.make("Greetings")
.add(HttpApiEndpoint.get("hello-world")`/`.addSuccess(Schema.String))
.add(HttpApiEndpoint.get("hello-failure")`/fail`.addError(Schema.String)),
);
class UsersRepository extends Context.Tag("UsersRepository")<
UsersRepository,
{
readonly findById: (id: number) => Effect.Effect<string>;
}
>() {}
// const repo = UsersRepository.of({ findById: (id) => Effect.succeed(`${id}`) });
const GreetingsLive = HttpApiBuilder.group(MyApi, "Greetings", (handlers) =>
handlers
.handle("hello-world", () =>
Effect.gen(function* () {
const repository = yield* UsersRepository;
yield* Console.log("<- Hello World handler invoked");
return yield* repository.findById(42);
}),
)
.handle("hello-failure", () => Effect.fail("Hello fail")),
);
const MyApiLive = HttpApiBuilder.api(MyApi).pipe(Layer.provide(GreetingsLive));
const userRepoLive = Layer.succeed(UsersRepository, {
findById: (id) => Effect.succeed(`${id}`),
});
const ServerLive = HttpApiBuilder.serve().pipe(
Layer.provide(MyApiLive),
Layer.provide(BunHttpServer.layer({ port: 3000 })),
Layer.provide(userRepoLive),
);
Layer.launch(ServerLive).pipe(BunRuntime.runMain);

0
api/src/services/auth.ts Normal file
View File

42
biome.json Normal file
View File

@ -0,0 +1,42 @@
{
"$schema": "https://biomejs.dev/schemas/2.1.2/schema.json",
"vcs": {
"enabled": false,
"clientKind": "git",
"useIgnoreFile": false
},
"files": {
"ignoreUnknown": false
},
"formatter": {
"enabled": true,
"indentStyle": "tab"
},
"linter": {
"enabled": true,
"rules": {
"recommended": true,
"correctness": {
"noUnusedImports": {
"level": "warn",
"fix": "safe",
"options": {}
}
}
}
},
"javascript": {
"formatter": {
"quoteStyle": "double",
"semicolons": "always"
}
},
"assist": {
"enabled": true,
"actions": {
"source": {
"organizeImports": "on"
}
}
}
}