-
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.
- Loading branch information
1 parent
62186ef
commit b10455c
Showing
14 changed files
with
248 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# prod | ||
dist/ | ||
|
||
# dev | ||
.yarn/ | ||
!.yarn/releases | ||
.vscode/* | ||
!.vscode/launch.json | ||
!.vscode/*.code-snippets | ||
.idea/workspace.xml | ||
.idea/usage.statistics.xml | ||
.idea/shelf | ||
|
||
# deps | ||
node_modules/ | ||
.wrangler | ||
|
||
# env | ||
.env | ||
.env.production | ||
.dev.vars | ||
|
||
# logs | ||
logs/ | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
pnpm-debug.log* | ||
lerna-debug.log* | ||
|
||
# misc | ||
.DS_Store |
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,8 @@ | ||
``` | ||
npm install | ||
npm run dev | ||
``` | ||
|
||
``` | ||
npm run deploy | ||
``` |
Binary file not shown.
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,11 @@ | ||
type Env = { | ||
DB: D1Database, | ||
ENVIRONMENT: string | ||
} | ||
|
||
type Variables = {} | ||
|
||
export type Context = { | ||
Bindings: Env, | ||
Variables: Variables | ||
} |
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,8 @@ | ||
{ | ||
"schema": "src/db/schema.ts", | ||
"out": "drizzle/migrations", | ||
"dialect": "sqlite", | ||
"dbCredentials": { | ||
"url": ".wrangler/state/v3/d1/miniflare-D1DatabaseObject/b60beec082e9a6bc4b6e34c6f10a8802ed0446b589791da93455ac6f46c2fc22.sqlite" | ||
} | ||
} |
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,5 @@ | ||
CREATE TABLE `secrets` ( | ||
`id` text PRIMARY KEY NOT NULL, | ||
`data` text NOT NULL, | ||
`timestamp` integer NOT NULL | ||
); |
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,47 @@ | ||
{ | ||
"version": "6", | ||
"dialect": "sqlite", | ||
"id": "843329b2-d147-4e87-a9c7-798df6776fa7", | ||
"prevId": "00000000-0000-0000-0000-000000000000", | ||
"tables": { | ||
"secrets": { | ||
"name": "secrets", | ||
"columns": { | ||
"id": { | ||
"name": "id", | ||
"type": "text", | ||
"primaryKey": true, | ||
"notNull": true, | ||
"autoincrement": false | ||
}, | ||
"data": { | ||
"name": "data", | ||
"type": "text", | ||
"primaryKey": false, | ||
"notNull": true, | ||
"autoincrement": false | ||
}, | ||
"timestamp": { | ||
"name": "timestamp", | ||
"type": "integer", | ||
"primaryKey": false, | ||
"notNull": true, | ||
"autoincrement": false | ||
} | ||
}, | ||
"indexes": {}, | ||
"foreignKeys": {}, | ||
"compositePrimaryKeys": {}, | ||
"uniqueConstraints": {} | ||
} | ||
}, | ||
"enums": {}, | ||
"_meta": { | ||
"schemas": {}, | ||
"tables": {}, | ||
"columns": {} | ||
}, | ||
"internal": { | ||
"indexes": {} | ||
} | ||
} |
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,13 @@ | ||
{ | ||
"version": "7", | ||
"dialect": "sqlite", | ||
"entries": [ | ||
{ | ||
"idx": 0, | ||
"version": "6", | ||
"when": 1724615729588, | ||
"tag": "0000_lush_ultimates", | ||
"breakpoints": true | ||
} | ||
] | ||
} |
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,22 @@ | ||
{ | ||
"name": "secret-worker", | ||
"scripts": { | ||
"dev": "wrangler dev src/index.ts --env dev", | ||
"deploy": "wrangler deploy --minify src/index.ts --env production", | ||
"db:generate": "drizzle-kit generate", | ||
"db:up": "drizzle-kit up" | ||
}, | ||
"dependencies": { | ||
"@hono/zod-validator": "^0.2.2", | ||
"drizzle-orm": "^0.33.0", | ||
"drizzle-zod": "^0.5.1", | ||
"hono": "^4.5.8", | ||
"zod": "^3.23.8" | ||
}, | ||
"devDependencies": { | ||
"@cloudflare/workers-types": "^4.20240529.0", | ||
"@libsql/client": "^0.9.0", | ||
"drizzle-kit": "^0.24.1", | ||
"wrangler": "^3.57.2" | ||
} | ||
} |
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,22 @@ | ||
import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core"; | ||
import { createInsertSchema } from "drizzle-zod"; | ||
import { z } from "zod"; | ||
|
||
export const secrets = sqliteTable('secrets', { | ||
id: text('id').primaryKey(), | ||
data: text('data').notNull(), | ||
timestamp: integer('timestamp').notNull() | ||
}); | ||
|
||
export const insertSecretSchema = createInsertSchema(secrets, { | ||
id: (schema) => schema.id.uuid(), | ||
data: (schema) => schema.data.max(4096), | ||
timestamp: (schema) => schema.timestamp.min(0) | ||
}); | ||
|
||
export const requestSecretSchema = insertSecretSchema.pick({ | ||
data: true | ||
}) | ||
|
||
export type InsertSecretSchema = z.infer<typeof insertSecretSchema>; | ||
export type RequestSecretSchema = z.infer<typeof requestSecretSchema>; |
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,44 @@ | ||
import { Hono } from 'hono' | ||
import { Context } from '../context' | ||
import { drizzle } from 'drizzle-orm/d1'; | ||
import { requestSecretSchema, secrets } from './db/schema'; | ||
import { createSecretId } from './lib/utils'; | ||
import { eq } from 'drizzle-orm'; | ||
import { zValidator } from '@hono/zod-validator'; | ||
import { HTTPException } from 'hono/http-exception'; | ||
import { logger } from 'hono/logger'; | ||
|
||
const UUID_REGEX = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"; | ||
|
||
const app = new Hono<Context>() | ||
app.use(logger()) | ||
|
||
const api = new Hono<Context>(); | ||
|
||
api.get(`/secret/:id{${UUID_REGEX}}`, async (c) => { | ||
const { id } = c.req.param(); | ||
const db = drizzle(c.env.DB); | ||
const result = await db.delete(secrets).where(eq(secrets.id, id)).returning(); | ||
if (result.length === 0) { | ||
return c.notFound(); | ||
} | ||
return c.json({ ...result[0], timestamp: undefined }, 200); | ||
}); | ||
|
||
api.post('secret/new', zValidator('json', requestSecretSchema), async (c) => { | ||
const { data } = c.req.valid("json"); | ||
const db = drizzle(c.env.DB); | ||
const result = await db.insert(secrets).values({ | ||
id: createSecretId(crypto), | ||
data: data, | ||
timestamp: new Date().getTime() | ||
}).returning() | ||
|
||
if (result.length === 0) { | ||
throw new HTTPException(500, { message: 'Unable to create secret.' }) | ||
} | ||
return c.json(result[0], 201); | ||
}); | ||
|
||
app.route('/api', api); | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const createSecretId = (crypto: Crypto) => { | ||
return crypto.randomUUID(); | ||
} |
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 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ESNext", | ||
"module": "ESNext", | ||
"moduleResolution": "Bundler", | ||
"strict": true, | ||
"skipLibCheck": true, | ||
"lib": [ | ||
"ESNext" | ||
], | ||
"types": [ | ||
"@cloudflare/workers-types/2023-07-01" | ||
], | ||
"jsx": "react-jsx", | ||
"jsxImportSource": "hono/jsx" | ||
}, | ||
} |
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,15 @@ | ||
name = "secret-worker" | ||
compatibility_date = "2024-08-25" | ||
workers_dev = false | ||
|
||
[env.dev] | ||
vars = { ENVIRONMENT = "dev" } | ||
d1_databases = [ { binding = "DB", database_name = "cfw-d1-secret", database_id = "6f5f390d-58b7-4221-a11e-3da9c4ae9b67" }] | ||
|
||
[env.production] | ||
vars = { ENVIRONMENT = "production" } | ||
d1_databases = [ { binding = "DB", database_name = "cfw-d1-secret", database_id = "6f5f390d-58b7-4221-a11e-3da9c4ae9b67" }] | ||
routes = [ | ||
{ pattern = "secret.ahh.bet/api", zone_name = "ahh.bet" }, | ||
{ pattern = "secret.ahh.bet/api/*", zone_name = "ahh.bet" } | ||
] |