Skip to content

Commit

Permalink
feat: add home route and information handler
Browse files Browse the repository at this point in the history
  • Loading branch information
bob2402 committed Apr 3, 2024
1 parent b01f863 commit bd1755e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
6 changes: 6 additions & 0 deletions deploy/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ const relay = await run({
allowed_kinds: "all", // or none,
},
password: Deno.env.get("relayed_pw"),
information: {
name: "Relayed Example",
description: "A lightweight relay written in Deno.",
supported_nips: [1],
software: "git+https://github.com/BlowaterNostr/relayed.git",
},
});
if (relay instanceof Error) {
console.error(relay);
Expand Down
26 changes: 25 additions & 1 deletion main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { NostrEvent, NostrKind, parseJSON, PublicKey, verifyEvent } from "./_lib
import { PolicyStore } from "./resolvers/policy.ts";
import { Policies } from "./resolvers/policy.ts";
import { interface_GetEventsByAuthors } from "./resolvers/event.ts";
import Home from "./routes/home.tsx";

const schema = gql.buildSchema(gql.print(typeDefs));

Expand All @@ -37,6 +38,7 @@ export async function run(args: {
port: number;
admin?: PublicKey;
password?: string;
information?: RelayInformation;
default_policy: DefaultPolicy;
kv?: Deno.Kv;
}): Promise<Error | Relay> {
Expand Down Expand Up @@ -109,6 +111,7 @@ export type EventReadWriter = {
const root_handler = (
args: {
password: string;
information?: RelayInformation;
connections: Map<WebSocket, SubscriptionMap>;
default_policy: DefaultPolicy;
resolvePolicyByKind: func_ResolvePolicyByKind;
Expand All @@ -119,18 +122,39 @@ const root_handler = (
async (req: Request, info: Deno.ServeHandlerInfo) => {
console.log(info.remoteAddr);

const { pathname } = new URL(req.url);
const { pathname, protocol } = new URL(req.url);
if (pathname == "/api") {
return graphql_handler(args)(req);
}
if (pathname == "/") {
if (protocol == "http:" || protocol == "https:") {
if (req.headers.get("accept")?.includes("application/nostr+json")) {
return information_handler(args);
}
return home_handler(args);
}
return ws_handler(args)(req, info);
}
const resp = new Response(render(Error404()), { status: 404 });
resp.headers.set("content-type", "html");
return resp;
};

const home_handler = (args: { information?: RelayInformation }) => {
const resp = new Response(render(Home(args.information)), { status: 200 });
resp.headers.set("content-type", "html");
return resp
}

const information_handler = (args: { information?: RelayInformation }) => {
const resp = new Response(JSON.stringify(args.information), { status: 200 });
resp.headers.set("content-type", "application/json; charset=utf-8");
resp.headers.set("Access-Control-Allow-Origin", "*");
resp.headers.set("Access-Control-Allow-Methods", "GET");
resp.headers.set("Access-Control-Allow-Headers", "accept,content-type");
return resp;
};

const graphql_handler =
(args: { password: string; kv: Deno.Kv; policyStore: PolicyStore }) => async (req: Request) => {
const { password, policyStore } = args;
Expand Down
22 changes: 22 additions & 0 deletions routes/home.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { RelayInformation } from "../main.tsx";

export default function Home(information?: RelayInformation) {
return (
<>
<div class="px-4 py-8 mx-auto bg-[#86efac]">
<div class="max-w-screen-md mx-auto flex flex-col items-center justify-center">
<a class="text-4xl font-bold" href="https://github.com/BlowaterNostr/relayed" target="_blank">Relayed</a>
<ul class="my-4">
<li><span class="font-bold">Name:</span> {information?.name}</li>
<li><span class="font-bold">Description:</span> {information?.description}</li>
<li><span class="font-bold">Pubkey:</span> {information?.pubkey}</li>
<li><span class="font-bold">Contact:</span> {information?.contact}</li>
<li><span class="font-bold">Supported NIPs:</span> {information?.supported_nips?.join(", ")}</li>
<li><span class="font-bold">Software:</span> {information?.software}</li>
<li><span class="font-bold">Version:</span> {information?.version}</li>
</ul>
</div>
</div>
</>
);
}

0 comments on commit bd1755e

Please sign in to comment.