Skip to content

Commit

Permalink
cf worker
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishekg999 committed Aug 25, 2024
1 parent 62186ef commit b10455c
Show file tree
Hide file tree
Showing 14 changed files with 248 additions and 0 deletions.
33 changes: 33 additions & 0 deletions secret-worker/.gitignore
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
8 changes: 8 additions & 0 deletions secret-worker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
```
npm install
npm run dev
```

```
npm run deploy
```
Binary file added secret-worker/bun.lockb
Binary file not shown.
11 changes: 11 additions & 0 deletions secret-worker/context.ts
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
}
8 changes: 8 additions & 0 deletions secret-worker/drizzle.config.json
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"
}
}
5 changes: 5 additions & 0 deletions secret-worker/drizzle/migrations/0000_lush_ultimates.sql
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
);
47 changes: 47 additions & 0 deletions secret-worker/drizzle/migrations/meta/0000_snapshot.json
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": {}
}
}
13 changes: 13 additions & 0 deletions secret-worker/drizzle/migrations/meta/_journal.json
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
}
]
}
22 changes: 22 additions & 0 deletions secret-worker/package.json
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"
}
}
22 changes: 22 additions & 0 deletions secret-worker/src/db/schema.ts
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>;
44 changes: 44 additions & 0 deletions secret-worker/src/index.ts
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;
3 changes: 3 additions & 0 deletions secret-worker/src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const createSecretId = (crypto: Crypto) => {
return crypto.randomUUID();
}
17 changes: 17 additions & 0 deletions secret-worker/tsconfig.json
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"
},
}
15 changes: 15 additions & 0 deletions secret-worker/wrangler.toml
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" }
]

0 comments on commit b10455c

Please sign in to comment.