Skip to content

Commit

Permalink
style
Browse files Browse the repository at this point in the history
  • Loading branch information
uriva committed Dec 24, 2023
1 parent 2f47fa7 commit dbd3993
Showing 1 changed file with 72 additions and 77 deletions.
149 changes: 72 additions & 77 deletions server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,40 +36,82 @@ type ApiTokenAction = { action: "create" } | {
tokenId: string;
};

const rootHandler = async (request: Request, uid: string) => {
const { method, params } = await request.json();
console.log("incoming request", method, params);
if (method === "get") {
const { cacheId, key } = params;
return new Response(
(await redisClient.get(`${uid}:${cacheId}:${key}`)) ||
JSON.stringify(null),
);
}
if (method === "delete") {
const { cacheId, key } = params;
return new Response(
JSON.stringify(await redisClient.del(`${uid}:${cacheId}:${key}`)),
);
}
if (method === "set") {
const { cacheId, key, value, ttl } = params;
await redisClient.set(
`${uid}:${cacheId}:${key}`,
JSON.stringify(value),
{
ex: (ttl > oneWeekInSeconds || !ttl) ? oneWeekInSeconds : ttl,
},
);
return new Response(JSON.stringify({}));
}
return new Response("Unknown method", { status: 400 });
};

const apiTokenPostHandler = (request: Request, uid: string) =>
request.json().then(
(call: ApiTokenAction) => {
if (call.action === "create") {
const token = crypto.randomUUID();
const tx = redisClient.tx();
tx.rpush(redisKey.userToApiTokenSet(uid), token);
tx.set(redisKey.apiTokenToUser(token), uid);
return tx.flush().then(() => new Response(JSON.stringify(token)));
}
if (call.action === "delete") {
return getApiTokens(uid).then((tokens) => {
const tokensToDelete = tokens.filter((t) =>
t.startsWith(call.tokenId)
);
if (tokensToDelete.length == 0) {
return Response404();
} else if (tokensToDelete.length > 1) {
return new Response("Ambiguous token ID", { status: 403 });
}
const deletedToken = tokensToDelete[0];
const tx = redisClient.tx();
tx.lrem(
redisKey.userToApiTokenSet(uid),
1,
deletedToken,
);
tx.del(redisKey.apiTokenToUser(deletedToken));
return tx.flush().then(() =>
new Response(JSON.stringify(deletedToken))
);
});
}
return new Response("Unknown command", { status: 403 });
},
);

const getApiTokens = (uid: string) =>
redisClient.lrange(redisKey.userToApiTokenSet(uid), 0, -1);

serve(
app({
"/": {
POST: authenticated(
memCache({ ttl: 60 * 5 })(verifyApiToken),
async (request, uid) => {
const { method, params } = await request.json();
console.log("incoming request", method, params);
if (method === "get") {
const { cacheId, key } = params;
return new Response(
(await redisClient.get(`${uid}:${cacheId}:${key}`)) ||
JSON.stringify(null),
);
}
if (method === "delete") {
const { cacheId, key } = params;
return new Response(
JSON.stringify(await redisClient.del(`${uid}:${cacheId}:${key}`)),
);
}
if (method === "set") {
const { cacheId, key, value, ttl } = params;
await redisClient.set(
`${uid}:${cacheId}:${key}`,
JSON.stringify(value),
{
ex: (ttl > oneWeekInSeconds || !ttl) ? oneWeekInSeconds : ttl,
},
);
return new Response(JSON.stringify({}));
}
return new Response("Unknown method", { status: 400 });
},
rootHandler,
),
},
"/api-token/": {
Expand All @@ -80,55 +122,8 @@ serve(
new Response(JSON.stringify(tokens))
),
),
POST: authenticated(
verifyAuth0,
(request, uid) =>
request.json().then(
(call: ApiTokenAction) => {
if (call.action === "create") {
const token = crypto.randomUUID();
const tx = redisClient.tx();
tx.rpush(redisKey.userToApiTokenSet(uid), token);
tx.set(redisKey.apiTokenToUser(token), uid);
return tx.flush().then(() =>
new Response(JSON.stringify(token))
);
}
if (call.action === "delete") {
return getApiTokens(uid).then((tokens) => {
const tokensToDelete = tokens.filter((t) =>
t.startsWith(call.tokenId)
);
if (tokensToDelete.length == 0) {
return Response404();
} else if (tokensToDelete.length > 1) {
return new Response("Ambiguous token ID", { status: 403 });
}
const deletedToken = tokensToDelete[0];
const tx = redisClient.tx();
tx.lrem(
redisKey.userToApiTokenSet(uid),
1,
deletedToken,
);
tx.del(redisKey.apiTokenToUser(deletedToken));
return tx.flush().then(() =>
new Response(JSON.stringify(deletedToken))
);
});
}
return new Response("Unknown command", { status: 403 });
},
),
),
POST: authenticated(verifyAuth0, apiTokenPostHandler),
},
}),
{ port: parseInt(Deno.env.get("PORT") as string) },
);

const getApiTokens = (uid: string) =>
redisClient.lrange(
redisKey.userToApiTokenSet(uid),
0,
-1,
);

0 comments on commit dbd3993

Please sign in to comment.