-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add token generation (fixes #1)
- Loading branch information
1 parent
834bfa2
commit 8b6ef81
Showing
9 changed files
with
119 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { deleteItem, put } from "./database"; | ||
|
||
export const acquireLock = async (key, ttl = 900) => { | ||
const lockKey = `lock#${key}`; | ||
const expires = Math.floor(Date.now() / 1000) + ttl; | ||
try { | ||
await put( | ||
{ | ||
pk: lockKey, | ||
sk: lockKey, | ||
_expires: expires, | ||
}, | ||
true, | ||
{ | ||
ConditionExpression: "attribute_not_exists(pk)", | ||
}, | ||
); | ||
return true; | ||
} catch (error) { | ||
if (error.name === "ConditionalCheckFailedException") { | ||
return false; | ||
} | ||
throw error; | ||
} | ||
}; | ||
|
||
export const releaseLock = async (key) => { | ||
const lockKey = `lock#${key}`; | ||
try { | ||
await deleteItem({ | ||
pk: lockKey, | ||
sk: lockKey, | ||
}); | ||
return true; | ||
} catch (e) { | ||
console.log(e); | ||
return false; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Hono } from "hono"; | ||
import { autoTrace } from "../../events/auto-trace"; | ||
|
||
const app = new Hono(); | ||
|
||
app.post("/", async (c) => { | ||
const body = await c.req.json(); | ||
if (body.token !== process.env.TRACER_TOKEN) { | ||
return c.json({ error: "Invalid token" }, 401); | ||
} | ||
|
||
await autoTrace(); | ||
|
||
return c.json({ success: true }); | ||
}); | ||
|
||
export default app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters