Skip to content
webtypPublic

About

GoFlare is a lightweight handler for building and deploying Go-based WebAssembly and JavaScript modules to Cloudflare Workers, Pages, or Functions. Designed to integrate seamlessly into existing build pipelines and tools.

Resources

Stars

3 stars

Watchers

0 watching

Forks

Latest commit

 

History

769 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GoFlare

GoFlare is a self-contained Go tool (library + CLI) for deploying Go projects to Cloudflare Workers and static assets: the edge compiles to JavaScript with goesm (never WebAssembly). No Node.js, no Wrangler. Pure Go, direct Cloudflare API. Deploy runs in GitHub Actions — secrets never touch the developer's machine.

When to use

  • Cloudflare Worker with Static Assets (recommended) — static site + Go edge function deployed in a single Cloudflare Worker.
  • Standalone Cloudflare Workers in Go, compiled to JavaScript (goesm).
  • Static Cloudflare Sites (Go frontends compiled to ES Modules by sitec).

See BUILD_WORKER_ASSETS.md.

Static assets with a content-hashed name automatically receive an immutable Cache-Control header via Cloudflare's _headers policy generated from pwa.CacheControl's rule; /artifacts/ is refused until R2 serving exists (D-PWA-16).

Project layout

my-project/
├── .env                       # credentials — gitignored (NEVER tokens)
├── .env.example
├── routes/
│   └── routes.go              # build-agnostic — func Register(r router.Router)
├── modules/
│   └── contact/
│       ├── model.go           # build-agnostic — model + Validate()
│       └── handler.go         # build-agnostic — func Handle(ctx router.Context)
├── web/
│   ├── client.go              # //go:build wasm — frontend (browser)
│   ├── server.go              # //go:build !wasm — local dev server
│   └── public/                # static assets — committed; produced by webtyp framework
│       ├── index.html
│       ├── client.wasm
│       ├── script.js
│       └── style.css
├── edge/
│   └── main.go                # entrypoint, imports webtyp/cloudflare/edge or cloudflare/workers
└── web/                    # generated by goflare
    └── edge/
        ├── edge.js            # main module (webtyp/cloudflare/assets/worker.mjs)
        └── app.js             # web/edge/main.go compiled to JavaScript by goesm

.env

PROJECT_NAME=my-app
# DOMAIN=example.com             # optional custom domain

CLOUDFLARE_API_TOKEN and CLOUDFLARE_ACCOUNT_ID are secrets — they live in GitHub Secrets, never in .env.

CLI

Install the CLI:

go install webtyp.com/goflare/cmd/goflare@latest
  • goflare auth --check: Validate CLOUDFLARE_API_TOKEN from environment.
  • goflare build: Build edge function into web/ and static site assets into web/public/.
  • goflare deploy: Single path deployment to Cloudflare Workers with assets. ⚠️ Designed for CI/CD environments.
  • goflare d1-id: Print the ID of the D1 database named by D1_NAME (exit 1 when not found).
  • goflare imports: Lista los imports prohibidos de la librería estándar que el edge alcanza.
  • goflare tinygo: Instala TinyGo si falta e imprime su directorio bin y su versión (lo usa el build del sitio para los Web Workers que declaran //go:build wasm && tinygo; el edge no lo usa).

Deploy target

A webtyp project deploys with goflare by declaring Target: goflare.Worker in config/deploy.go. The person provides one bootstrap token; goflare creates the D1 database, the R2 bucket, the scoped account token and the CI secrets, and writes the Worker secrets. See DEPLOY_TARGET.md.

GitHub Setup

Deployment is designed to run in CI with a single action line:

- uses: actions/checkout@v4
- uses: webtyp/goflare@v1
  with:
    worker: mi-worker
    domain: mi-worker.ejemplo.cl
    d1-binding: DB
    d1-name: mi-worker-db
    r2-binding: ASSETS
    r2-bucket: mi-worker-assets
  env:
    CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
    CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}

For more details see CI_GITHUB_ACTIONS.md and CI_D1_SECRETS.md.

Edge function entrypoint

web/edge/main.go (runtime lives in webtyp/cloudflare):

package main

import (
    "webtyp.com/cloudflare/edge"
    "github.com/your-project/routes"
)

func main() {
    r := edge.NewRouter(edge.Config{})
    routes.Register(r)
    edge.Serve(r)
}

The edge runtime (edge, workers, d1, r2, log, cloudflare.Env) is now webtyp.com/cloudflare — goflare only bundles it via cloudflare/assets. Legacy webtyp.com/goflare/edge is still accepted by mode.go during migration.

Rate limiting

Every deploy that includes a custom domain automatically provisions a Cloudflare rate-limiting rule in the zone's http_ratelimit phase. It defaults to 100 requests per 10 seconds per IP, and blocks for 10 seconds. This ensures APIs are secure against unbounded resource consumption by default.

  • Set RATE_LIMIT_PER_10S in .env to override the limit. It must be an integer >= 1.
  • Required permission: The Cloudflare API token must have the "Zone → Zone WAF → Edit" permission.
  • The rule utilizes cf.colo.id and ip.src characteristics, which fits within the Cloudflare Free plan constraints, making it available for all deployments.
  • Deployments without a custom domain skip this step.

Migrations from Outside a Worker

goflare.NewD1Migrator runs DDL migrations against a D1 database from CI — outside a Worker, where cloudflare/d1.NewEdge doesn't exist. See docs/D1.md.

⚠️ Critical: NO heavy stdlib in edge code

Files with //go:build wasm (everything under edge/, routes/, modules/, webtyp/cloudflare) NEVER import fmt, strings, errors, encoding/*, net/http, log, io/ioutil. Use webtyp/fmt, webtyp/json, webtyp/strings, webtyp/fetch instead: every stdlib package is more JavaScript in app.js. goflare imports lists the ones the edge reaches.

Verification: grep -rE '^\s*"(fmt|strings|errors|encoding|net/http|log|io/ioutil)"' edge/ routes/ modules/ $(go env GOPATH)/pkg/mod/webtyp.com/cloudflare* must return empty — edge runtime lives in webtyp/cloudflare.

Library usage

cfg := &goflare.Config{
    ProjectName: "myapp",
    AccountID:   "acc-id",
}
g := goflare.New(cfg)
g.Build()
g.Deploy()

Config reference

Field .env key Default Notes
ProjectName PROJECT_NAME — required
AccountID GitHub Secret CLOUDFLARE_ACCOUNT_ID — required
WorkerName WORKER_NAME <ProjectName>-worker optional
Entry — auto: edge Convention: web/edge/main.go
PublicDir — auto: web/public Convention: web/public
Domain DOMAIN — optional custom domain
D1Binding D1_BINDING — binding name the Worker code uses; required with D1Name
D1Name D1_NAME — D1 database name; its ID is resolved at deploy
R2Binding R2_BINDING — binding name the Worker code uses; required with R2Bucket
R2Bucket R2_BUCKET — R2 bucket name
Vars VARS — non-secret variables, one KEY=VALUE per line

Testing

Edge code talks to js.Global(), not to Cloudflare — so it is tested in a browser against a fake context.env, with no deploy and no wrangler. See docs/TESTING.md for the three tiers and the rule for choosing one.

gotest    # never `go test` — browser-driven for //go:build wasm packages

Requirements

  • Go 1.25.2+
  • goesm (compiles the edge to JavaScript)
  • TinyGo — only for Web Workers that declare //go:build wasm && tinygo, installed via webtyp/tinygo

About

GoFlare is a lightweight handler for building and deploying Go-based WebAssembly and JavaScript modules to Cloudflare Workers, Pages, or Functions. Designed to integrate seamlessly into existing build pipelines and tools.

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages