Skip to content

Commit

Permalink
scaffold separation core/itty sample
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrrt committed Nov 25, 2023
1 parent 63c303c commit 8b25eda
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 72 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@
"graphql-yoga": "^5.0.0",
"itty-durable": "^2.1.0",
"jest": "^29.7.0",
"node-fetch": "^3.3.2"
"node-fetch": "2.7.0"
}
}
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@authdog/hydra-core",
"version": "1.0.0",
"description": "",
"main": "index.js",
"main": "build/index.js",
"scripts": {
"dev": "tsc -w -p tsconfig.json",
"build": "tsc -p tsconfig.json",
Expand Down
28 changes: 28 additions & 0 deletions packages/core/src/handlers/graphql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { createYoga } from "graphql-yoga";
import { schema } from "../schema";

export const GraphQLHandler = async (req: Request, env: any, ctx: any) => {
const yoga = createYoga({
schema,
context: async (event) => {
const { request } = event;
const authorization =
(request.headers.get("Authorization") ||
request.headers.get("authorization")) ??
null;

const forwardedFor = request.headers.get("x-forwarded-for");
// "x-forwarded-for": "\"[2a02:1210:5274:9f00:b94e:d462:5886:5cc2]\", 140.248.74.113, 140.248.74.113",
// extract ip address in [] in forwardedFor
const remoteIp = forwardedFor?.match(/\[(.*?)\]/)?.[1] ?? null;

return {
authorization,
remoteIp,
userAgent: request.headers.get("user-agent") ?? null,
};
},
landingPage: false,
});
return yoga(req, env, ctx);
};
30 changes: 1 addition & 29 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1 @@
import { createYoga } from "graphql-yoga";
import { schema } from "./schema";

export const GraphQLHandler = async (req: Request, env: any, ctx: any) => {
const yoga = createYoga({
schema,
context: async (event) => {
const { request } = event;
const authorization =
(request.headers.get("Authorization") ||
request.headers.get("authorization")) ??
null;

const forwardedFor = request.headers.get("x-forwarded-for");
// "x-forwarded-for": "\"[2a02:1210:5274:9f00:b94e:d462:5886:5cc2]\", 140.248.74.113, 140.248.74.113",
// extract ip address in [] in forwardedFor
const remoteIp = forwardedFor?.match(/\[(.*?)\]/)?.[1] ?? null;

return {
// request: modifiedRequest, // Pass the modified request to the context
authorization,
remoteIp,
userAgent: request.headers.get("user-agent") ?? null,
};
},
landingPage: false,
});
return yoga(req, env, ctx);
};
export {GraphQLHandler} from "./handlers/graphql";
66 changes: 30 additions & 36 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions services/itty-hydra/handlers/hydraHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
interface Context {

}

export const Hydra = async (request: Request, env: any, ctx: Context

) => {
return new Response("Hydra handler", {
status: 200,
});
}
12 changes: 11 additions & 1 deletion services/itty-hydra/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createCors } from "itty-cors";
import { withDurables } from "itty-durable";
import { NotFound } from "./handlers/notFound";
import { Health } from "./handlers/health";
import {GraphQLHandler} from "@authdog/hydra-core";

const { preflight, corsify } = createCors();

Expand All @@ -12,11 +13,20 @@ router
.options("*", preflight)
.get("/", Health)
.get("/health", Health)
.get("/graphql", GraphQLHandler)
.get("*", NotFound);

const handleRequest = (req, env, ctx) => {
const {HYDRA_ACME} = env;

const enrichedContext = {
...ctx,
kv: HYDRA_ACME,
rateLimiter: null,
};

return router
.handle(req, env, ctx)
.handle(req, env, enrichedContext)
.catch(
(err) => () =>
new Response(err.stack, {
Expand Down
4 changes: 3 additions & 1 deletion services/itty-hydra/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
"author": "",
"license": "ISC",
"dependencies": {
"@authdog/hydra-core": "workspace:*",
"itty-cors": "^0.3.6",
"itty-router": "^4.0.23",
"keylab": "^0.1.30"
"keylab": "^0.1.30",
"node-fetch": "2.7"
},
"devDependencies": {
"@esbuild-plugins/node-globals-polyfill": "0.2.3",
Expand Down
7 changes: 4 additions & 3 deletions services/itty-hydra/wrangler.base.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ workers_dev = true
compatibility_date = "2023-10-10"
compatibility_flags = ["nodejs_compat"]

kv_namespaces = [
{ binding = "HYDRA_ACME", id = "c63f48a0f29843e8ab8251ef533e1c9c" }
]

[build]
command = "npm run build"

Expand All @@ -17,6 +21,3 @@ ip = "0.0.0.0"
local_protocol="http"
upstream_protocol="https"

kv_namespaces = [
{ binding = "HYDRA_ACME", id = "c63f48a0f29843e8ab8251ef533e1c9c" }
]

0 comments on commit 8b25eda

Please sign in to comment.