Skip to content

Commit

Permalink
feat: add sentry to function
Browse files Browse the repository at this point in the history
  • Loading branch information
cptchloroplast committed Aug 28, 2024
1 parent ac6357f commit 8c78fb7
Show file tree
Hide file tree
Showing 11 changed files with 216 additions and 119 deletions.
200 changes: 124 additions & 76 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@cloudflare/workers-types": "^4.20240620.0",
"@giscus/svelte": "^3.0.0",
"@okkema/worker": "^2.0.0",
"@sentry/cloudflare": "^8.26.0",
"@types/js-yaml": "^4.0.9",
"astro": "^4.11.3",
"autoprefixer": "^10.4.19",
Expand Down
4 changes: 3 additions & 1 deletion src/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ export type Environment = {
MAILJET_API_KEY: string
MAILJET_SECRET_KEY: string
ADMIN_EMAIL: string
BUCKET: R2Bucket
BLOG: R2Bucket
RSA_PUBLIC_KEY: string
RSA_PRIVATE_KEY: string
CF_PAGES_COMMIT_SHA: string;
STRAVA: R2Bucket
DB: D1Database
SENTRY_DSN: string
WORKER_SCHEDULE: string
}

type Runtime = import("@astrojs/cloudflare").Runtime<Environment>
Expand Down
78 changes: 44 additions & 34 deletions src/functions/index.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,55 @@
import type { ExecutionContext, ScheduledController } from "@cloudflare/workers-types"
import type { D1Database, ExecutionContext, R2Bucket, ScheduledController } from "@cloudflare/workers-types"
import type { Environment } from "@env"
import type { Gear } from "@schemas/strava"
import { GearService, PostService } from "@services"
import { parseMarkdown } from "@utils"
import * as Sentry from "@sentry/cloudflare"

const GearRegex = /gear\/([b0-9]+).meta.json/
const PostsRegex = /posts\/([A-Za-z0-9\-]+).md/

export default {
async scheduled(controller: ScheduledController, env: Environment, ctx: ExecutionContext) {
const gear = await env.STRAVA.list({ prefix: "gear" })
for (const object of gear.objects) {
const key = object.key
const id = GearRegex.exec(key)?.[1]
if (!id) {
console.log("Unable to extract id from key", key)
continue
}
const body = await env.STRAVA.get(key)
if (!body) {
console.log("This should never happen...", key)
continue
}
const gear = await body.json<Gear>()
const service = GearService(env.DB)
await service.upsert(gear)
async function processBuckets(blog: R2Bucket, strava: R2Bucket, db: D1Database) {
const posts = await blog.list({ prefix: "posts" })
for (const object of posts.objects) {
const key = object.key
const slug = PostsRegex.exec(key)?.[1]
if (!slug) {
console.log("Unable to extract slug from key", key)
continue
}

const posts = await env.BUCKET.list({ prefix: "posts" })
for (const object of posts.objects) {
const key = object.key
const slug = PostsRegex.exec(key)?.[1]
if (!slug) {
console.log("Unable to extract slug from key", key)
continue
}
const body = await env.BUCKET.get(key)
const raw = await body!.text()
const post = parseMarkdown(raw, slug)
const service = PostService(env.DB)
await service.upsert(post)
const body = await blog.get(key)
const raw = await body!.text()
const post = parseMarkdown(raw, slug)
const service = PostService(db)
await service.upsert(post)
}

const gear = await strava.list({ prefix: "gear" })
for (const object of gear.objects) {
const key = object.key
const id = GearRegex.exec(key)?.[1]
if (!id) {
console.log("Unable to extract id from key", key)
continue
}
const body = await strava.get(key)
if (!body) {
console.log("This should never happen...", key)
continue
}
const gear = await body.json<Gear>()
const service = GearService(db)
await service.upsert(gear)
}
}

export default Sentry.withSentry(function(env) {
return {
dsn: env.SENTRY_DSN
}
}, {
async scheduled(controller: ScheduledController, env: Environment, ctx: ExecutionContext) {
const db = Sentry.instrumentD1WithSentry(env.DB)
ctx.waitUntil(processBuckets(env.BLOG, env.STRAVA, db))
}
}
})
2 changes: 1 addition & 1 deletion src/pages/api/subscribe/confirm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export async function GET(context: APIContext) {
ok: false,
message: "Something doesn't look right here....",
})
const subscribers = R2Repository<Subscriber>(env.BUCKET, "subscribers")
const subscribers = R2Repository<Subscriber>(env.BLOG, "subscribers")
const subscriber = await subscribers.get(email)
if (!subscriber || id !== subscriber.id) {
return json({
Expand Down
2 changes: 1 addition & 1 deletion src/pages/api/subscribe/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { Subscriber } from "@schemas"
export async function POST(context: APIContext) {
const { env } = context.locals.runtime
const site = context.site?.hostname
const subscribers = R2Repository<Subscriber>(env.BUCKET, "subscribers")
const subscribers = R2Repository<Subscriber>(env.BLOG, "subscribers")
const data = await context.request.json<{ email: string }>()
const { email } = data
const subscriber = await subscribers.get(email)
Expand Down
2 changes: 1 addition & 1 deletion src/pages/img/[...slug].ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { APIContext } from "astro"

export async function GET(context: APIContext) {
const slug = context.params.slug
const object = await context.locals.runtime.env.BUCKET.get(`img/${slug}`)
const object = await context.locals.runtime.env.BLOG.get(`img/${slug}`)
if (!object) return new Response("Not Found", { status: 404 })
const headers = new Headers()
// https://github.com/cloudflare/workers-sdk/issues/6047
Expand Down
23 changes: 23 additions & 0 deletions terraform/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 16 additions & 4 deletions terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ module "page" {
}

production_buckets = {
BUCKET = var.github_repository
BLOG = var.github_repository
STRAVA = "strava"
}

Expand Down Expand Up @@ -83,13 +83,25 @@ module "worker" {
env_vars = [
{ name = "WORKER_SCHEDULE", value = var.WORKER_SCHEDULE }
]
secrets = [
{ name = "SENTRY_DSN", value = module.sentry.dsn }
]
buckets = [
{ binding = "STRAVA", name = "strava" },
{ binding = "BUCKET", name = var.github_repository }
{ binding = "BLOG", name = var.github_repository },
{ binding = "STRAVA", name = "strava" }
]
databases = [
{ binding = "DB", id = module.database.id }
]

depends_on = [module.database]
depends_on = [module.database, module.sentry]
}

module "sentry" {
source = "app.terraform.io/okkema/project/sentry"
version = "~> 0.3"

github_organization = var.github_owner
github_repository = var.github_repository
default_user = var.sentry_default_user
}
1 change: 1 addition & 0 deletions terraform/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ variable "cloudflare_zone_id" {}
variable "github_owner" {}
variable "github_repository" {}
variable "pages_hostname" {}
variable "sentry_default_user" {}

# Environment Variables
variable "HCAPTCHA_SITEKEY" {}
Expand Down
2 changes: 1 addition & 1 deletion wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ compatibility_date = "2024-07-01"
compatibility_flags = [ "nodejs_compat" ]

[[r2_buckets]]
binding = "BUCKET"
binding = "BLOG"
preview_bucket_name = "blog"
bucket_name = "blog"

Expand Down

0 comments on commit 8c78fb7

Please sign in to comment.