Skip to content

Instantly share code, notes, and snippets.

  • npx wrangler kv namespace list
  • npx wrangler kv namespace create api-and-kv-example
@mhart
mhart / 0001_create-db.sql
Created August 8, 2024 02:12
d1-movie-example
CREATE TABLE IF NOT EXISTS movies (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
release_date TEXT NOT NULL,
rating INT NOT NULL
);
@mhart
mhart / functions-index.js
Last active June 26, 2024 02:42
PPR w/ CF Pages and HTMLRewriter
// functions/index.js
export async function onRequestGet({ request, env }) {
// Start getting something from "the DB"
const dbPromise = scheduler.wait(1000).then(() => new Date().toDateString());
// Fetch index.html from cache (body will stream below)
const staticShell = await env.ASSETS.fetch(request);
// The browser will immediately render up until the body closing tag,
// then we insert dom modification scripts as the data is resolved
aws cloudformation deploy \
--stack-name pdf2png-app \
--template-file template.yml \
--parameter-overrides ImageUri=<accountid>.dkr.ecr.us-east-1.amazonaws.com/pdf2png-app:v1 \
--capabilities CAPABILITY_IAM
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Parameters:
ImageUri:
Type: String
Resources:
ConversionFunction:
Type: AWS::Serverless::Function
docker run -v "$HOME/.aws":/root/.aws \
-e AWS_REGION=us-east-1 -e AWS_PROFILE=myprofile \
-p 9000:8080 \
pdf2png-test
FROM alpine:3.12 AS rie-image
RUN apk add --no-cache curl && \
cd / && \
curl -sSL -O https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/download/v1.0/aws-lambda-rie && \
chmod +x /aws-lambda-rie
FROM pdf2png
FROM golang:1-alpine3.12 AS build-image
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY *.go ./
RUN go build -tags lambda.norpc -ldflags="-s -w" main.go
func main() {
os.Chdir("/tmp") // Change working directory to /tmp so we can write files
if len(os.Args) > 1 && os.Args[1] == "--test" {
pdf2png()
} else {
lambda.Start(handler)
}
}
FROM golang:1-alpine3.12 AS build-image
WORKDIR /app
COPY *.go ./
RUN go build main.go
FROM alpine:3.12