feat: add custom address (#13)

* feat: add custom address

* feat: update readme
This commit is contained in:
Dream Hunter
2023-09-07 13:21:36 +08:00
committed by GitHub
parent a6b29073f5
commit 536cd98df2
6 changed files with 106 additions and 27 deletions

View File

@@ -18,14 +18,31 @@ api.get('/api/settings', async (c) => {
return c.json(c.get("jwtPayload"));
})
api.get('/open_api/settings', async (c) => {
return c.json({
"prefix": c.env.PREFIX,
"domain": c.env.DOMAIN,
});
})
api.get('/api/new_address', async (c) => {
// insert new address
const name = Math.random().toString(36).substring(2, 15)
const { success } = await c.env.DB.prepare(
`INSERT INTO address (name) VALUES (?)`
).bind(name).run();
if (!success) {
return c.json({ "error": "Failed to create address" }, 500)
let { name } = await c.req.query();
if (!name) {
name = Math.random().toString(36).substring(2, 15)
}
try {
const { success } = await c.env.DB.prepare(
`INSERT INTO address (name) VALUES (?)`
).bind(name).run();
if (!success) {
return c.text("Failed to create address", 500)
}
} catch (e) {
if (e.message && e.message.includes("UNIQUE")) {
return c.text("Please retry a new address", 400)
}
return c.text("Failed to create address", 500)
}
// create jwt
const jwt = await Jwt.sign({