Skip to content

Commit

Permalink
signup-api -> api, finalize move to cloudflare
Browse files Browse the repository at this point in the history
  • Loading branch information
theoephraim committed Jan 15, 2025
1 parent 7bb1388 commit fe06632
Show file tree
Hide file tree
Showing 29 changed files with 279 additions and 1,413 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ ignore

.DS_Store

# Local Netlify folder
.netlify

config.local.ts
.env.local
.env.local.*
Expand Down
9 changes: 6 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,27 @@ Then install deps via `pnpm`:
## Packages

- `packages/core` - Core package for DMNO (includes CLI)
- `packages/docs-site` - Docs site (dmno.dev)
- `packages/chat.dmno.dev` - Redirects to Discord
- `packages/signup-api` - Netlify functions for email signup
- `packages/docs-site` - Docs site (https://dmno.dev)
- `packages/dmno-api` - Basic api for tracking email signups

## Plugins

- `plugins/1password` - DMNO plugin for 1Password
- `plugins/encrypted-vault` - DMNO plugin for encrypted values
- `plugins/bitwarden` - DMNO plugin for Bitwarden Secrets Manager
- `plugins/infisical` - DMNO plugin for Infisical

## Integrations
- `integrations/astro` - DMNO integration for Astro
- `integrations/vite` - DMNO integration for Vite
- `integrations/nextjs` - DMNO integration for Next.js
- `integrations/remix` - DMNO integration for Remix
- `integrations/fastify` - DMNO integration for Fastify

## Platforms
- `platforms/netlify` - DMNO platform for Netlify
- `platforms/vercel` - DMNO platform for Vercel
- `platforms/cloudflare` - DMNO platform for Cloudflare

## libs

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
},
"scripts": {
"build": "turbo build",
"build:libs": "turbo build --filter=\"!@dmno/docs-site\" --filter=\"!@dmno/signup-api\"",
"build:libs": "turbo build --filter=\"!@dmno/docs-site\" --filter=\"!@dmno/api\"",
"preinstall": "npx only-allow pnpm",
"dev": "turbo run dev --concurrency=40 --parallel --filter=\"!@dmno/docs-site\" --filter=\"!@dmno/signup-api\"",
"dev": "turbo run dev --concurrency=40 --parallel --filter=\"!@dmno/docs-site\" --filter=\"!@dmno/api\"",
"lint": "turbo lint",
"changeset:add": "changeset add",
"changeset:version": "dmno run -- changeset version",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/cli/commands/init.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ program.action(async (opts: {
// TODO: figure out how we want to disable this while we are building/testing this
const response = await tryCatch(async () => {
// TODO: would love to use dmno for this URL, but using dmno while _building_ dmno feels like it might be tricky
return await fetch('https://signup-api.dmno.dev/signup', {
return await fetch('https://api.dmno.dev/signup', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down
54 changes: 54 additions & 0 deletions packages/dmno-api/.dmno/config.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { DmnoBaseTypes, defineDmnoService, configPath, switchBy, pickFromSchemaObject } from 'dmno';
import { CloudflarePagesEnvSchema, CloudflareWranglerEnvSchema, DmnoWranglerEnvSchema } from '@dmno/cloudflare-platform';
import { EncryptedVaultDmnoPlugin, EncryptedVaultTypes } from '@dmno/encrypted-vault-plugin';
// import { OnePasswordDmnoPlugin } from '@dmno/1password-plugin';

// we'll just use a single vault for now since it only has the 1 key - can split prod/staging later
const EncryptedVault = new EncryptedVaultDmnoPlugin('vault', {
key: configPath('..', 'DMNO_VAULT_KEY'),
name: 'prod',
});

// const onepass = new OnePasswordDmnoPlugin('1pass', { fallbackToCliBasedAuth: true });


export default defineDmnoService({
name: 'api',
schema: {
// these may be useful if we move over to workers, or move to another CI
// ...pickFromSchemaObject(CloudflareWranglerEnvSchema, {
// CLOUDFLARE_ACCOUNT_ID: {
// value: onepass.itemByReference("op://Shared/Cloudflare/account id"),
// },
// CLOUDFLARE_API_TOKEN: {
// value: onepass.itemByReference("op://Shared/Cloudflare/workers api token"),
// },
// }),
DMNO_VAULT_KEY: {
extends: EncryptedVaultTypes.encryptionKey,
required: true,
},
MAILERLITE_TOKEN: {
sensitive: true,
required: true,
value: EncryptedVault.item(),
},
MAILERLITE_GROUP_ID: {
value: '112994107484276105',
description: "The MailerLite group ID to add new subscribers to",
externalDocs: {
url: 'https://dashboard.mailerlite.com/subscribers?rules=W1t7Im9wZXJhdG9yIjoiaW5fYW55IiwiY29uZGl0aW9uIjoiZ3JvdXBzIiwiYXJncyI6WyJncm91cHMiLFsiMTEyOTk0MTA3NDg0Mjc2MTA1Il1dfV1d&group=112994107484276105&status=active',
description: 'Group: All Subs'
}
},
...pickFromSchemaObject(CloudflarePagesEnvSchema, 'CF_PAGES_BRANCH'),

DMNO_ENV: {
value: () => {
if (DMNO_CONFIG.CF_PAGES_BRANCH === 'main') return 'production';
if (DMNO_CONFIG.CF_PAGES_BRANCH) return 'staging';
return 'local';
},
},
}
});
10 changes: 10 additions & 0 deletions packages/dmno-api/.dmno/prod.vault.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"version": "0.0.1",
"keyName": "prod-2024-05-04",
"items": {
"api!MAILERLITE_TOKEN": {
"encryptedValue": "58z4RMkF0F9r6pyZ3EFMBD1SV7lWEXyTGCYQsD2BbAS4HKYK4o1abSv0l3JinxTptmjV4Mr2iNK+KAp89pSl1Zsn8cm7nD7gQMBlJlSqO3QaZ5aANhID7RPtzP1WhLVdh8m57281iDQZscRNHJtKLwy5odbeN3mmXS0n9+xf2ttyeVR6eOiaWmXYbRimo1lSEEYxMSns3fx5tytgYWghnHN3igZuLK1jdq4wML5FpJ/cVCeoNpSzW/IF+IkZgh5wF7sS+Qpu33S55NklilLglRoENulX0DUN8TzOEX8xN/a0bbaa1B/5zsAEE3y4zaqnGmljOvZtJW7joCM6e0hGeqKGPQMOQeUdsVfLELVc/xgVC3xyCGbXNAOc5d0r6BZGfsF98ea+/63Ocw+9tWqWbhkuH3fcKRw8NPtZXNAt129fdF12ufWdgcxc+xX8WBD6PbR8mEbAClwpwOE7BoHMBJG3pVDhCydRlN56+UCNOALM4T2iiPPZoCLraDYMG0na+Nz8m19XarX7y6CM21oyWeiz4mNS58Zuui4+5lGgOVuumarDcoVLtVXdlmTbrjfdcdG9Mc37jP0xr2V3hsuP+JOjJGSjV1jE5CCJ/1+Oqd+PekUHLm7jHmAnveXx8LLAS0gUvxnL69AwdjP01ArVqlHaPM7AacxPBvnO46Ap4uZ42VV9CdGhvlmZexQi2nE4KTGI1Yfqs5JxIdc5HmHCzQXg8F9shCifATvcORA+CmlZjn44DsTXGfeomtyMc99vrLxqXUywvIq7RaM6KACgq6NWsKAKOV2wGI4tv1axWv6ORKTLZuCL1gTZ7+bmhI8xA83xXCwXnv6QmYwrFHvUJ15xcvn21Htg7aWr5+qnt5SQdoQLTIiTfmE/yGo5Wy5YP2xEBc0vLqSNSHa3TDbsoE61z7cHsEc0qLy/VQw5kwjOHwzipiFikB0V+q5yg0qJup03GJzire1PaYzQjm7kjKt+7F6tWyABxGh3iJtz2wYUZUhOPIKDFTZGMQ/lhlDj+Y8yaOaKgArwYJYyREokUIOr2ItqbqoCHWGAoknA4rmOfVnQIM/ca3I3lMMsJp2G5wHg25WABeq+v3dGaz+P8jo7m/8PZlBFApI5VnLYTmo3N8DJrMHR6JYHpX+eiJvMLbVj+kvVA2ZK6lKHQNQyzu4ouBmJLqYJQiBLe02CiWaTeBhXCKp+N1nnV9Tmwh1P9SoTq1zUlBYbYZKcjssXJc2HH4xxDJSFoklZ/BNqyWa5jsDciQr8cuLzESBZUmG7KHLdfATGtSwv5FTZN/H29nSt0WcdWsL9VaWzgrJyo5e5roGBuxt6nm3s5AIXXV9RNF6lfrUOgjAJ",
"updatedAt": "2024-05-04T20:04:43.857Z"
},
}
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
functions-dist
functions/*.js
functions/*.map
.wrangler
Expand Down
10 changes: 10 additions & 0 deletions packages/dmno-api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# DMNO cloud api

A simple api designed to run on Cloudflare Pages.

Currently this just handles a single endpoint to subscribe folks to our mailing list, but this will be where we can start to deal with user logins and persisting settings/state.


### Why not Cloudflare Workers?

Ideally this would be running on Workers, but the Pages CI dev ex is much better. We will migrate over when the [Workers builds](https://developers.cloudflare.com/workers/ci-cd/builds/) catches up to pages.
3 changes: 3 additions & 0 deletions packages/dmno-api/functions-src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const onRequest: PagesFunction = async (context) => {
return new Response(JSON.stringify({ apiStatus: 'ok' }), { status: 200 });
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import EmailValidation from 'emailvalid';
import { PagesFunction, type Response as CFResponse } from '@cloudflare/workers-types';
import mailerlite from './lib/mailerlite';

const ev = new EmailValidation({ allowFreemail: true });

export const onRequestOptions: PagesFunction = async () => {
return new Response(null, {
status: 204,
}) as unknown as CFResponse;
});
};

// Set CORS to all responses
Expand All @@ -21,19 +20,18 @@ export const onRequest: PagesFunction = async (context) => {
};



export const onRequestPost: PagesFunction = async (context) => {
// TODO: type reqBody
let reqBody: any;
try {
reqBody = await context.request.json();
} catch (err: any) {
return new Response(`Error parsing JSON body - ${err.message}`, { status: 400 }) as unknown as CFResponse;
return new Response(`Error parsing JSON body - ${err.message}`, { status: 400 });
}

if (!reqBody.email) return new Response('email is missing', { status: 400 }) as unknown as CFResponse;
if (!reqBody.email) return new Response('email is missing', { status: 400 });
const checkEmail = ev.check(reqBody.email);
if (!checkEmail.valid) return new Response('email is invalid', { status: 400 }) as unknown as CFResponse;
if (!checkEmail.valid) return new Response('email is invalid', { status: 400 });

const clientIp = context.request.headers.get('cf-connecting-ip');

Expand All @@ -49,8 +47,8 @@ export const onRequestPost: PagesFunction = async (context) => {
try {
await mailerlite.createSubscriber(signupObj);
} catch (err: any) {
return new Response(`Error creating subscriber - ${err.message}`, { status: 500 }) as unknown as CFResponse;
return new Response(`Error creating subscriber - ${err.message}`, { status: 500 });
}

return new Response('ok', { status: 200 }) as unknown as CFResponse;
return new Response('ok', { status: 200 });
};
8 changes: 8 additions & 0 deletions packages/dmno-api/functions-src/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"lib": [ "esnext" ],
"types": [ "@cloudflare/workers-types" ]
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,37 +1,31 @@
{
"name": "@dmno/signup-api",
"name": "@dmno/api",
"private": true,
"type": "module",
"scripts": {
"build": "dmno run -- tsup",
"dev": "pnpm --filter=\"@dmno/signup-api\" run --parallel \"/dev:*/\" ",
"build-for-deploy": "turbo run --filter=@dmno/api... --filter=\"!@dmno/api\" build && pnpm run build && mkdir dist",
"dev": "pnpm --filter=\"@dmno/api\" run --parallel \"/dev:*/\" ",
"dev:ts": "dmno run -w -- pnpm run build --watch",
"dev:netlify": "netlify dev",
"dev:wrangler": "wrangler pages dev",
"deploy": "dwrangler pages deploy",
"lint": "eslint src --ext .ts,.cjs,.astro,.md,.mdx",
"lint:fix": "pnpm run lint --fix"
},
"dependencies": {
"@dmno/ts-lib": "workspace:*",
"async": "^3.2.5",
"axios": "^1.6.8",
"emailvalid": "^1.0.4",
"google-auth-library": "^9.9.0",
"google-spreadsheet": "^4.1.1"
"emailvalid": "^1.0.4"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20241112.0",
"@cloudflare/workers-types": "^4.20250109.0",
"@dmno/cloudflare-platform": "workspace:*",
"@dmno/encrypted-vault-plugin": "workspace:*",
"@dmno/1password-plugin": "workspace:*",
"@dmno/eslint-config": "workspace:*",
"@dmno/tsconfig": "workspace:*",
"@netlify/functions": "^2.6.3",
"@types/async": "^3.2.24",
"dmno": "workspace:*",
"netlify-cli": "^17.1.0",
"tsup": "catalog:",
"typescript": "catalog:",
"wrangler": "^3.96.0"
"wrangler": "^3.102.0"
}
}
9 changes: 9 additions & 0 deletions packages/dmno-api/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"exclude": [ "functions-src/**/*.ts" ],
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"lib": [ "esnext" ],
"baseUrl": ".",
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default defineConfig({
// Entry points
entry: [
'functions-src/signup.ts',
'functions-src/index.ts',
],

sourcemap: true, // Generate sourcemaps
Expand Down
2 changes: 2 additions & 0 deletions packages/dmno-api/wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name = "dmno-api"
pages_build_output_dir = "dist"
2 changes: 1 addition & 1 deletion packages/docs-site/.dmno/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default defineDmnoService({
},

SIGNUP_API_URL: {
value: 'https://signup-api.dmno.dev',
value: 'https://api.dmno.dev',
},
}
});
79 changes: 0 additions & 79 deletions packages/signup-api/.dmno/config.mts

This file was deleted.

22 changes: 0 additions & 22 deletions packages/signup-api/.dmno/prod.vault.json

This file was deleted.

1 change: 0 additions & 1 deletion packages/signup-api/dist/howdy.txt

This file was deleted.

5 changes: 0 additions & 5 deletions packages/signup-api/public/_routes.json

This file was deleted.

Loading

0 comments on commit fe06632

Please sign in to comment.