Skip to content

Commit

Permalink
Make server api client server only
Browse files Browse the repository at this point in the history
  • Loading branch information
omfj committed Sep 24, 2024
1 parent 7052c98 commit 427ed05
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 13 deletions.
3 changes: 2 additions & 1 deletion apps/api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Hono } from "hono";
import { cors } from "hono/cors";
import { logger } from "hono/logger";

import { route } from "./lib/hono";
import adminApp from "./services/admin";
import degreesApp from "./services/degrees";
import feedbackApp from "./services/feedback";
Expand All @@ -21,7 +22,7 @@ app.use(
}),
);

app.route("/", healthApp);
route("/", app, healthApp);
app.route("/", adminApp);
app.route("/", happeningApp);
app.route("/", feedbackApp);
Expand Down
20 changes: 20 additions & 0 deletions apps/api/src/lib/hono.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Hono } from "hono";

import { admin } from "../middleware/admin";

export const createApp = () => {
const publicApp = new Hono();
const privateApp = new Hono();

privateApp.use(admin());

return {
public: publicApp,
private: privateApp,
};
};

export const route = (path: string, app: Hono, ppApp: ReturnType<typeof createApp>) => {
app.route(path, ppApp.public);
app.route(path, ppApp.private);
};
6 changes: 3 additions & 3 deletions apps/api/src/services/health.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Hono } from "hono";
import { createApp } from "../lib/hono";

const app = new Hono();
const app = createApp();

app.get("/", async (c) => {
app.public.get("/", async (c) => {
return c.text("OK");
});

Expand Down
14 changes: 6 additions & 8 deletions apps/api/src/services/shopping-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ import { Hono } from "hono";

import { db } from "@echo-webkom/db";

import { admin } from "../middleware/admin";

const app = new Hono();

app.get("/shopping", async (c) => {
const items = await db.query.shoppingListItems
.findMany({
with: { likes: true, user: true },
})
.catch(() => {
return [];
});
app.get("/shopping", admin(), async (c) => {
const items = await db.query.shoppingListItems.findMany({
with: { likes: true, user: true },
});

return c.json(items);
});
Expand Down
2 changes: 2 additions & 0 deletions apps/web/src/api/server.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import "server-only";

import ky from "ky";

export const apiServer = ky.extend({
Expand Down
2 changes: 1 addition & 1 deletion packages/db/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ let pool;

const createPool = () => {
return postgres(process.env.DATABASE_URL!, {
max: 3,
max: 1,
prepare: false,
idle_timeout: 10000, // 10 seconds
connect_timeout: 1000, // 1 seconds
Expand Down

0 comments on commit 427ed05

Please sign in to comment.