mirror of
https://github.com/imjasonh/terraform-playground
synced 2026-07-06 23:12:22 +00:00
WIP: cf-worker-go; tf-cloudfuare doesn't work with wasm :(
Signed-off-by: Jason Hall <jason@chainguard.dev>
This commit is contained in:
parent
3517cb0699
commit
91e9eee26b
5 changed files with 83 additions and 0 deletions
1
cf-worker-go/.gitignore
vendored
Normal file
1
cf-worker-go/.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
build
|
||||||
7
cf-worker-go/go.mod
Normal file
7
cf-worker-go/go.mod
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
module github.com/imjasonh/terraform-playground/cf-worker-go
|
||||||
|
|
||||||
|
go 1.21.3
|
||||||
|
|
||||||
|
toolchain go1.21.5
|
||||||
|
|
||||||
|
require github.com/syumai/workers v0.18.0
|
||||||
2
cf-worker-go/go.sum
Normal file
2
cf-worker-go/go.sum
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
github.com/syumai/workers v0.18.0 h1:RBnOFqghALR0woeCCYumG7Zt7UDFAt51rSQJhd/hJjw=
|
||||||
|
github.com/syumai/workers v0.18.0/go.mod h1:ZnqmdiHNBrbxOLrZ/HJ5jzHy6af9cmiNZk10R9NrIEA=
|
||||||
15
cf-worker-go/main.go
Normal file
15
cf-worker-go/main.go
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/syumai/workers"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
http.HandleFunc("/hello", func(w http.ResponseWriter, req *http.Request) {
|
||||||
|
fmt.Fprintln(w, "Hello!")
|
||||||
|
})
|
||||||
|
workers.Serve(nil)
|
||||||
|
}
|
||||||
58
cf-worker-go/main.tf
Normal file
58
cf-worker-go/main.tf
Normal file
|
|
@ -0,0 +1,58 @@
|
||||||
|
terraform {
|
||||||
|
required_providers {
|
||||||
|
cloudflare = { source = "cloudflare/cloudflare" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "name" { type = string }
|
||||||
|
variable "cloudflare_zone" { type = string }
|
||||||
|
variable "cloudflare_api_token" { type = string }
|
||||||
|
variable "cloudflare_account_id" { type = string }
|
||||||
|
|
||||||
|
provider "cloudflare" {
|
||||||
|
api_token = var.cloudflare_api_token
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: need to manually enable the workers.dev route in the dashboard
|
||||||
|
// use cloudflare_worker_route instead to set up routing
|
||||||
|
resource "cloudflare_worker_script" "script" {
|
||||||
|
depends_on = [terraform_data.build]
|
||||||
|
account_id = var.cloudflare_account_id
|
||||||
|
name = var.name
|
||||||
|
content = file("${path.module}/build/worker.mjs")
|
||||||
|
|
||||||
|
// │ Error: error creating worker script: Wasm and blob bindings are not supported with modules; use imports instead (10021)
|
||||||
|
module = false
|
||||||
|
|
||||||
|
webassembly_binding {
|
||||||
|
name = "WASM"
|
||||||
|
module = filebase64("./build/app.wasm")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
resource "cloudflare_worker_route" "route" {
|
||||||
|
zone_id = data.cloudflare_zone.zone.id
|
||||||
|
pattern = "${var.name}.${var.cloudflare_zone}/*"
|
||||||
|
script_name = cloudflare_worker_script.script.name
|
||||||
|
}
|
||||||
|
|
||||||
|
data "cloudflare_zone" "zone" {
|
||||||
|
name = var.cloudflare_zone
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
resource "terraform_data" "build" {
|
||||||
|
# Changes to *.go files requires re-building it.
|
||||||
|
input = {
|
||||||
|
# https://stackoverflow.com/questions/51138667/can-terraform-watch-a-directory-for-changes
|
||||||
|
dir_sha1 = sha1(join("", [for f in fileset("${path.module}", "*.go") : filesha1("${path.module}/${f}")]))
|
||||||
|
}
|
||||||
|
|
||||||
|
provisioner "local-exec" {
|
||||||
|
command = <<EOC
|
||||||
|
go run github.com/syumai/workers/cmd/workers-assets-gen@v0.18.0 -mode=go
|
||||||
|
GOOS=js GOARCH=wasm go build -o ./build/app.wasm .
|
||||||
|
EOC
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue