mirror of
https://github.com/nestriness/nestri.git
synced 2025-12-12 08:45:38 +02:00
⭐feat: Add a websocket party (#152)
This adds functionality to connect to remote server thru the party
This commit is contained in:
@@ -1,116 +1,65 @@
|
||||
import "zod-openapi/extend";
|
||||
import type * as Party from "partykit/server";
|
||||
// import { Resource } from "sst";
|
||||
import { ZodError } from "zod";
|
||||
import { Hono } from "hono";
|
||||
import { logger } from "hono/logger";
|
||||
// import { subjects } from "../subjects";
|
||||
import { VisibleError } from "../error";
|
||||
// import { ActorContext } from '@nestri/core/actor';
|
||||
import { Hono, type MiddlewareHandler } from "hono";
|
||||
import { HTTPException } from "hono/http-exception";
|
||||
import { AuthApi } from "./auth";
|
||||
import type { HonoBindings } from "./types";
|
||||
import { ApiSession } from "./session";
|
||||
import { openAPISpecs } from "hono-openapi";
|
||||
|
||||
|
||||
const app = new Hono().basePath('/parties/main/:id');
|
||||
// const auth: MiddlewareHandler = async (c, next) => {
|
||||
// const client = createClient({
|
||||
// clientID: "api",
|
||||
// issuer: "http://auth.nestri.io" //Resource.Urls.auth
|
||||
// });
|
||||
|
||||
// const authHeader =
|
||||
// c.req.query("authorization") ?? c.req.header("authorization");
|
||||
// if (authHeader) {
|
||||
// const match = authHeader.match(/^Bearer (.+)$/);
|
||||
// if (!match || !match[1]) {
|
||||
// throw new VisibleError(
|
||||
// "input",
|
||||
// "auth.token",
|
||||
// "Bearer token not found or improperly formatted",
|
||||
// );
|
||||
// }
|
||||
// const bearerToken = match[1];
|
||||
|
||||
// const result = await client.verify(subjects, bearerToken!);
|
||||
// if (result.err)
|
||||
// throw new VisibleError("input", "auth.invalid", "Invalid bearer token");
|
||||
// if (result.subject.type === "user") {
|
||||
// // return ActorContext.with(
|
||||
// // {
|
||||
// // type: "user",
|
||||
// // properties: {
|
||||
// // accessToken: result.subject.properties.accessToken,
|
||||
// // userID: result.subject.properties.userID,
|
||||
// // auth: {
|
||||
// // type: "oauth",
|
||||
// // clientID: result.aud,
|
||||
// // },
|
||||
// // },
|
||||
// // },
|
||||
// // next,
|
||||
// // );
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
const app = new Hono<{ Bindings: HonoBindings }>().basePath('/parties/main/:room');
|
||||
|
||||
app
|
||||
.use(logger(), async (c, next) => {
|
||||
c.header("Cache-Control", "no-store");
|
||||
return next();
|
||||
})
|
||||
// .use(auth)
|
||||
|
||||
|
||||
app
|
||||
.route("/auth", AuthApi.route)
|
||||
// .get("/parties/main/:id", (c) => {
|
||||
// const id = c.req.param();
|
||||
// const env = c.env as any
|
||||
// const party = env.room as Party.Room
|
||||
// party.broadcast("hello from hono")
|
||||
|
||||
// return c.text(`Hello there, ${id.id} 👋🏾`)
|
||||
// })
|
||||
.onError((error, c) => {
|
||||
console.error(error);
|
||||
if (error instanceof VisibleError) {
|
||||
try {
|
||||
await next();
|
||||
} catch (e: any) {
|
||||
return c.json(
|
||||
{
|
||||
code: error.code,
|
||||
message: error.message,
|
||||
},
|
||||
error.kind === "auth" ? 401 : 400,
|
||||
);
|
||||
}
|
||||
if (error instanceof ZodError) {
|
||||
const e = error.errors[0];
|
||||
if (e) {
|
||||
return c.json(
|
||||
{
|
||||
code: e?.code,
|
||||
message: e?.message,
|
||||
error: {
|
||||
message: e.message || "Internal Server Error",
|
||||
status: e.status || 500,
|
||||
},
|
||||
400,
|
||||
);
|
||||
}
|
||||
}
|
||||
if (error instanceof HTTPException) {
|
||||
return c.json(
|
||||
{
|
||||
code: "request",
|
||||
message: "Invalid request",
|
||||
},
|
||||
400,
|
||||
e.status || 500
|
||||
);
|
||||
}
|
||||
return c.json(
|
||||
{
|
||||
code: "internal",
|
||||
message: "Internal server error",
|
||||
})
|
||||
|
||||
const routes = app
|
||||
.get("/health", (c) => {
|
||||
return c.json({
|
||||
status: "healthy",
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
})
|
||||
.route("/session", ApiSession.route)
|
||||
|
||||
app.get(
|
||||
"/doc",
|
||||
openAPISpecs(routes, {
|
||||
documentation: {
|
||||
info: {
|
||||
title: "Nestri Realtime API",
|
||||
description:
|
||||
"The Nestri realtime API gives you the power to connect to your remote machine and relays from a single station",
|
||||
version: "0.3.0",
|
||||
},
|
||||
500,
|
||||
);
|
||||
});
|
||||
|
||||
components: {
|
||||
securitySchemes: {
|
||||
Bearer: {
|
||||
type: "http",
|
||||
scheme: "bearer",
|
||||
bearerFormat: "JWT",
|
||||
},
|
||||
},
|
||||
},
|
||||
security: [{ Bearer: [] }],
|
||||
servers: [
|
||||
{ description: "Production", url: "https://api.nestri.io" },
|
||||
],
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
export type Routes = typeof routes;
|
||||
export default app
|
||||
Reference in New Issue
Block a user