Skip to content

Commit

Permalink
feat: add health endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
s99100532 committed Jun 23, 2024
1 parent e694fa0 commit 64dff6e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

# Getting Started

1. Start the database and app. The db container will create table automatically.
1. Start the database and app. The db container will create table automatically according to [here](./db_migration/init.sql).

```sh
docker compose up -d
Expand All @@ -27,14 +27,14 @@ docker compose exec app bash
deno task db:migration
```

4. Start calling the API
4. test the API using endpoint http://localhost:8080/health

# Testing

1. Enter app container as above
2. Run `deno task test`

> [!WARNING]
> [!WARNING]
> Run test will TRUNCATE all the tables, make sure to use dev database.
# Remark
Expand Down
1 change: 1 addition & 0 deletions constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ export const ROUTES = {
SIGNUP: "/signup",
ORDER_CREATE: "/orders",
MY_ORDERS: "/orders/me",
HEALTH: "/health",
};
4 changes: 3 additions & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ services:
args:
- PORT=8080
env_file: .env.docker_local
ports:
- 8080:8080

database:
image: mysql
Expand All @@ -18,7 +20,7 @@ services:
- 3306:3306
volumes:
# Create tables
- ./db_migration:/docker-entrypoint-initdb.d
- ./db_migration/init.sql:/docker-entrypoint-initdb.d/init.sql
command:
[
"mysqld",
Expand Down
17 changes: 11 additions & 6 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ router.post(ROUTES.SIGNUP, async (context) => {
const payload = SignupValidator.parse(body);

await userService.signup(payload.username, payload.password);
const redirect_url = `${getEnv("APP_URL")}:${
getEnv("PORT")
}${ROUTES.LOGIN}`;
const redirect_url = `${getEnv("APP_URL")}:${getEnv(
"PORT",
)}${ROUTES.LOGIN}`;

helper.setAPIResponse<SignupResponseData>(context, {
success: true,
Expand Down Expand Up @@ -103,15 +103,20 @@ router.get(ROUTES.MY_ORDERS, authMiddleware, async (context) => {
});
});

router.get(ROUTES.HEALTH, (context) => {
helper.setAPIResponse(context, {
success: true,
});
});

app.use(router.routes());
app.use(router.allowedMethods());

if (import.meta.main) {
const port = parseInt(helper.getEnv("PORT"));

app.addEventListener(
"listen",
() => console.log(`Server listensing on PORT ${port}`),
app.addEventListener("listen", () =>
console.log(`Server listensing on PORT ${port}`),
);
await app.listen({ port });

Expand Down

0 comments on commit 64dff6e

Please sign in to comment.