Skip to content

Commit

Permalink
ipの取得方法変更
Browse files Browse the repository at this point in the history
  • Loading branch information
kgtkr committed Feb 10, 2024
1 parent 9a19f82 commit 48f1843
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
19 changes: 9 additions & 10 deletions packages/server/src/server/context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { prisma } from "../prisma-client";
import { array, option } from "fp-ts";
import { none, some } from "fp-ts/lib/Option";
import { none, some, Option } from "fp-ts/lib/Option";
import { TokenRepo } from "../adapters";
import { AtAuthError } from "../at-error";
import { ITokenRepo, Ports } from "../ports";
Expand Down Expand Up @@ -30,15 +30,14 @@ async function createToken(raw: unknown, tokenRepo: ITokenRepo) {
);
}

export async function createContext(
headers: Record<string, unknown>
): Promise<AppContext> {
const xRealIp = headers["x-real-ip"];
const ip = typeof xRealIp === "string" ? some(xRealIp) : none;
const token = await createToken(
headers["x-token"] || headers["X-Token"],
new TokenRepo(prisma)
);
export async function createContext({
rawToken,
ip,
}: {
rawToken: unknown;
ip: Option<string>;
}): Promise<AppContext> {
const token = await createToken(rawToken, new TokenRepo(prisma));

return {
ports: createPorts({
Expand Down
10 changes: 8 additions & 2 deletions packages/server/src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ import { makeExecutableSchema } from "@graphql-tools/schema";
import bodyParser from "koa-bodyparser";
import { GraphQLError } from "graphql";
import { startCron } from "../cron";
import { none, some } from "fp-ts/lib/Option";

export async function serverRun() {
const app = new Koa();
app.proxy = true;
app.use(cors());
app.use(bodyParser());

Expand All @@ -43,7 +45,8 @@ export async function serverRun() {
schema,
context: ({ connectionParams }) => {
return createContext({
"x-token": connectionParams?.["token"],
rawToken: connectionParams?.["token"],
ip: none,
});
},
},
Expand Down Expand Up @@ -110,7 +113,10 @@ export async function serverRun() {
koaMiddleware(server, {
context: (params): Promise<AppContext> => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
return createContext(params.ctx.request.headers);
return createContext({
rawToken: params.ctx.request.headers["x-token"],
ip: some(params.ctx.ip),
});
},
})
);
Expand Down

0 comments on commit 48f1843

Please sign in to comment.