elysia is the truth
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
"build": "vite build"
|
||||
},
|
||||
"dependencies": {
|
||||
"@elysiajs/eden": "^1.3.2",
|
||||
"@solidjs/router": "^0.15.3",
|
||||
"hono": "^4.8.12",
|
||||
"solid-js": "^1.9.5"
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { hc } from "hono/client";
|
||||
import { type ApiType } from "../../server/src/api";
|
||||
import { type Api } from "../../server/src/api";
|
||||
import { treaty } from "@elysiajs/eden";
|
||||
|
||||
export default hc<ApiType>("http://localhost:5001/api");
|
||||
const { api } = treaty<Api>("http://localhost:5001");
|
||||
export default api;
|
||||
|
||||
@@ -1,23 +1,8 @@
|
||||
import {
|
||||
Accessor,
|
||||
createContext,
|
||||
createEffect,
|
||||
createResource,
|
||||
JSX,
|
||||
Show,
|
||||
Suspense,
|
||||
} from "solid-js";
|
||||
import Card from "./Card";
|
||||
import { Accessor, createContext, createResource, Show } from "solid-js";
|
||||
import { GameState } from "../../../shared/types/cards";
|
||||
import api from "../api";
|
||||
import Hand from "./Hand";
|
||||
import Pile from "./Pile";
|
||||
import {
|
||||
GameState,
|
||||
newDeck,
|
||||
shuffle,
|
||||
Hand as THand,
|
||||
} from "../../../shared/types/cards";
|
||||
import { createStore, produce, SetStoreFunction, Store } from "solid-js/store";
|
||||
import api from "../api";
|
||||
|
||||
export const GameContext = createContext<{
|
||||
gameState: Accessor<GameState | undefined>;
|
||||
@@ -25,17 +10,18 @@ export const GameContext = createContext<{
|
||||
}>();
|
||||
|
||||
export default (props: { instanceId: number }) => {
|
||||
const [gameState, { refetch }] = createResource<GameState>(() =>
|
||||
api.gameState[":gameId"]
|
||||
.$get({ param: { gameId: props.instanceId.toString() } })
|
||||
.then((res) => res.json())
|
||||
const [gameState, { refetch }] = createResource(() =>
|
||||
api
|
||||
.gameState({ gameId: props.instanceId.toString() })
|
||||
.get()
|
||||
.then((res) => res.data as GameState)
|
||||
);
|
||||
|
||||
const setGameState = (state: GameState) =>
|
||||
api.gameState[":gameId"]
|
||||
.$put({
|
||||
param: { gameId: props.instanceId.toString() },
|
||||
json: state,
|
||||
api
|
||||
.gameState({ gameId: props.instanceId.toString() })
|
||||
.put({
|
||||
gameState: state,
|
||||
})
|
||||
.then(refetch);
|
||||
|
||||
|
||||
@@ -8,8 +8,7 @@ export default () => {
|
||||
|
||||
const [instances, { refetch }] = createResource(
|
||||
() => param.game,
|
||||
async () =>
|
||||
api.instances.$get({ query: param }).then((res) => res.json())
|
||||
async () => api.instances.get({ query: param }).then((res) => res.data)
|
||||
);
|
||||
|
||||
return (
|
||||
|
||||
@@ -4,7 +4,7 @@ import api from "../api";
|
||||
|
||||
export default () => {
|
||||
const [games] = createResource(async () =>
|
||||
api.games.$get().then((res) => res.json())
|
||||
api.games.get().then((res) => res.data)
|
||||
);
|
||||
|
||||
return (
|
||||
|
||||
@@ -2,15 +2,18 @@
|
||||
"name": "server",
|
||||
"scripts": {
|
||||
"dev": "concurrently 'pnpm run devserver' 'pnpm run dbstudio'",
|
||||
"devserver": "NODE_ENV=development bun run --hot --port 5001 src/index.ts",
|
||||
"devserver": "NODE_ENV=development PORT=5001 bun run --hot src/index.ts",
|
||||
"dbstudio": "pnpm dlx prisma studio --browser none",
|
||||
"dbdeploy": "pnpm dlx prisma migrate deploy",
|
||||
"dbsync": "concurrently 'pnpm dlx prisma generate' 'pnpm dlx prisma migrate dev'",
|
||||
"start": "NODE_ENV=production bun run --port 5001 src/index.ts"
|
||||
"start": "NODE_ENV=production PORT=5001 bun run src/index.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
"@elysiajs/cors": "^1.3.3",
|
||||
"@elysiajs/static": "^1.3.0",
|
||||
"@hono/zod-validator": "^0.7.2",
|
||||
"@prisma/client": "6.13.0",
|
||||
"elysia": "^1.3.8",
|
||||
"hono": "^4.8.12",
|
||||
"zod": "^4.0.15"
|
||||
},
|
||||
|
||||
@@ -1,68 +1,46 @@
|
||||
import { Hono } from "hono";
|
||||
import { prisma } from "./db/db";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { z } from "zod";
|
||||
import { Elysia, t } from "elysia";
|
||||
|
||||
const api = new Hono()
|
||||
.get("/ping", (c) => c.text("pong"))
|
||||
const api = new Elysia({ prefix: "/api" })
|
||||
.get("/games", () => prisma.game.findMany())
|
||||
|
||||
.get("/games", async (c) => {
|
||||
const games = await prisma.game.findMany();
|
||||
return c.json(games);
|
||||
})
|
||||
|
||||
.get(
|
||||
"/instances",
|
||||
zValidator("query", z.object({ game: z.string() })),
|
||||
async (c) => {
|
||||
const { game } = c.req.valid("query");
|
||||
const instances = await prisma.instance.findMany({
|
||||
where: {
|
||||
game: {
|
||||
name: game,
|
||||
},
|
||||
.get("/instances", ({ query: { game } }) =>
|
||||
prisma.instance.findMany({
|
||||
where: {
|
||||
game: {
|
||||
name: game,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
},
|
||||
});
|
||||
return c.json(instances);
|
||||
}
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
},
|
||||
})
|
||||
)
|
||||
|
||||
.get(
|
||||
"/gameState/:gameId",
|
||||
zValidator("param", z.object({ gameId: z.string() })),
|
||||
async (c) => {
|
||||
const { gameId } = c.req.valid("param");
|
||||
const instance = await prisma.instance.findUnique({
|
||||
.get("/gameState/:gameId", ({ params: { gameId } }) =>
|
||||
prisma.instance
|
||||
.findUnique({
|
||||
where: {
|
||||
id: Number(gameId),
|
||||
},
|
||||
});
|
||||
return c.json(instance?.gameState);
|
||||
}
|
||||
})
|
||||
.then((game) => game?.gameState)
|
||||
)
|
||||
|
||||
.put(
|
||||
"/gameState/:gameId",
|
||||
zValidator("param", z.object({ gameId: z.string() }), (result) => {
|
||||
console.log(JSON.stringify(result, null, 2));
|
||||
}),
|
||||
zValidator("json", z.any()),
|
||||
async (c) => {
|
||||
const { gameId } = c.req.valid("param");
|
||||
const gameState = c.req.valid("json");
|
||||
|
||||
await prisma.instance.update({
|
||||
({ params: { gameId }, body: { gameState } }) =>
|
||||
prisma.instance.update({
|
||||
data: { gameState },
|
||||
where: {
|
||||
id: Number(gameId),
|
||||
},
|
||||
});
|
||||
|
||||
return c.text("", 200);
|
||||
}),
|
||||
{
|
||||
body: t.Object({
|
||||
gameState: t.Any(),
|
||||
}),
|
||||
}
|
||||
);
|
||||
export default api;
|
||||
export type ApiType = typeof api;
|
||||
export type Api = typeof api;
|
||||
|
||||
@@ -1,28 +1,21 @@
|
||||
import { Hono } from "hono";
|
||||
import { serveStatic } from "hono/bun";
|
||||
import api from "./api";
|
||||
import { cors } from "hono/cors";
|
||||
import { createBunWebSocket } from "hono/bun";
|
||||
import { Elysia, env } from "elysia";
|
||||
import { cors } from "@elysiajs/cors";
|
||||
import { staticPlugin } from "@elysiajs/static";
|
||||
|
||||
const app = new Hono();
|
||||
app.use("*", async (c, next) => {
|
||||
console.log(c.req.method, c.req.url);
|
||||
await next();
|
||||
console.log(">>", c.res.status);
|
||||
});
|
||||
const port = env.PORT || 5001;
|
||||
|
||||
const isDev = Bun.env.NODE_ENV === "development";
|
||||
const isProd = Bun.env.NODE_ENV === "production";
|
||||
const app = new Elysia()
|
||||
.use(cors())
|
||||
.onRequest(({ request }) => {
|
||||
console.log(request.method, request.url);
|
||||
})
|
||||
.onError(({ code, error }) => {
|
||||
console.error(code, error);
|
||||
})
|
||||
.get("/ping", () => "pong")
|
||||
.use(api)
|
||||
.use(staticPlugin({ assets: "./dist", prefix: "" }))
|
||||
.listen(port);
|
||||
|
||||
isProd &&
|
||||
app.use(
|
||||
"*",
|
||||
serveStatic({
|
||||
root: "./dist",
|
||||
})
|
||||
);
|
||||
|
||||
app.use("*", cors());
|
||||
app.route("/api", api);
|
||||
|
||||
export default app;
|
||||
console.log("server started on", port);
|
||||
|
||||
Reference in New Issue
Block a user