การเขียนผู้ให้บริการ: บทแนะนำทีละขั้นตอน
หน้า
ผู้
เกี่ยว
สิ่งที่คุณต้องมี
- ชุด
เครื่อง มือ Go ซึ่ง คอมไพล์ เป็น WebAssembly โดย ไม่ ต้อง มี เครื่อง มือ เพิ่ม - สำเนา
ที่ คู่มือเช็กเอาต์ ของ ชุด พัฒนา ปลั๊กอิน ซึ่ง นัก แนะนำพัฒนา ปลั๊กอิน ไว้ และ ของ แพ็กเกจ ABI ที่ ชุด เครื่อง มือ นั้น สร้าง บน: go.modของเกสต์ ด้าน ล่าง ชี้ ไป ยัง ทั้ง สอง - ไบนารี
aggrega
รูปทรงของผลงานที่เสร็จแล้ว
ผู้.wasm และPlugin ที่
พื้นที่
dotenv-walkthrough/ guest/ the Go module that compiles to the .wasm go.mod go.sum main.go catalog/ the Component and Recipe you author component/example/compute/checkout/1.0.0/component.yaml recipe/dotenv/checkout/1.0.0/recipe.yaml dev-catalog/ the generated Plugin entity, with the .wasm beside it plugin/provider/dotenv/0.1.0/plugin.yaml plugin/provider/dotenv/0.1.0/provider--dotenv.wasm dotenv.wasm the compiled guest overlay.yaml the identity you supply to the entity generator aggregate.yaml manifest.yaml release.yaml .aggrega-config.yaml1. การรายงานตนเองของเกสต์
ทุกdescribe() โฮสต์
สร้าง guest/main.go:
// A provider guest that renders environment-file artifacts.package main
import ( "bytes" "fmt" "sort" "strings"
pdk "github.com/extism/go-pdk"
"github.com/aggrega-ai/aggrega-pdk/go/pkg/aggrega")
func descriptor() aggrega.PluginDescriptor { return aggrega.PluginDescriptor{ AbiVersion: aggrega.APIVersion, PluginType: "provider", Description: "Render artifact bodies into environment files, quoting and escaping each value as the format requires.", Capabilities: []string{"dotenv"}, Exports: []aggrega.PluginExport{ {Name: "render_node"}, }, Metadata: &aggrega.PluginDescriptorMeta{License: "Apache-2.0"}, }}
//go:wasmexport describefunc describe() int32 { out, err := aggrega.MarshalDescriptor(descriptor()) if err != nil { pdk.SetError(err) return 1 } pdk.Output(out) return 0}PluginType บอกCapabilities คือspec.capability เป็น dotenv จะExports ระบุrender_node คือ
ตัว
2. การเรนเดอร์โหนด
render_node ถูก
งานguest/main.go:
// needsQuoting reports whether a value cannot be written bare in an// environment file. A bare value ends at the first space, so anything// carrying whitespace has to be quoted; the quote characters, the comment// introducer and the expansion introducer would each be read as syntax.func needsQuoting(v string) bool { if v == "" { return true } return strings.ContainsAny(v, " \t\n\r\"'#$\\")}
// quote wraps a value in double quotes and escapes what the format reads as// syntax inside them. Order matters: the backslash is escaped first, or the// escapes introduced afterwards would be escaped a second time.func quote(v string) string { v = strings.ReplaceAll(v, `\`, `\\`) v = strings.ReplaceAll(v, `"`, `\"`) v = strings.ReplaceAll(v, "\n", `\n`) v = strings.ReplaceAll(v, "\r", `\r`) v = strings.ReplaceAll(v, "\t", `\t`) return `"` + v + `"`}
// normaliseKey maps an authored key into the environment-variable grammar:// upper case, with the separators an author reaches for folded to// underscores. It reports an error rather than correcting a key that still// does not fit, because a silently-renamed variable is one the application// never reads.func normaliseKey(k string) (string, error) { n := strings.ToUpper(k) n = strings.NewReplacer("-", "_", ".", "_", "/", "_").Replace(n) if n == "" { return "", fmt.Errorf("empty key") } for i, r := range n { switch { case r >= 'A' && r <= 'Z', r == '_': case r >= '0' && r <= '9' && i > 0: default: return "", fmt.Errorf("key %q maps to %q, which is not a valid environment-variable name", k, n) } } return n, nil}การ
ตอน
// provenanceComment renders the build-provenance key set the host computed as// comment lines. An environment file has no metadata block, so a comment// header is where provenance can go; the keys are emitted in sorted order so// the bytes are identical across runs of identical input.func provenanceComment(annotations map[string]string) []byte { if len(annotations) == 0 { return nil } keys := make([]string, 0, len(annotations)) for k := range annotations { keys = append(keys, k) } sort.Strings(keys) var out bytes.Buffer for _, k := range keys { fmt.Fprintf(&out, "# %s=%s\n", k, annotations[k]) } out.WriteByte('\n') return out.Bytes()}
// renderBody translates one artifact body into environment-file lines. A// blank line and a comment pass through untouched; every other line is// KEY=value, and a line carrying no separator is a loud failure rather than a// line the guest guesses at.func renderBody(name string, body []byte) ([]byte, error) { var out bytes.Buffer for i, raw := range strings.Split(string(body), "\n") { line := strings.TrimSpace(raw) if line == "" || strings.HasPrefix(line, "#") { continue } k, v, found := strings.Cut(line, "=") if !found { return nil, fmt.Errorf("%s line %d: %q declares no value; an entry is KEY=value", name, i+1, line) } key, err := normaliseKey(strings.TrimSpace(k)) if err != nil { return nil, fmt.Errorf("%s line %d: %w", name, i+1, err) } val := strings.TrimSpace(v) if needsQuoting(val) { val = quote(val) } fmt.Fprintf(&out, "%s=%s\n", key, val) } return out.Bytes(), nil}
//go:wasmexport render_nodefunc render_node() int32 { var in aggrega.RenderNodeInput if err := in.DecodeCBOR(pdk.Input()); err != nil { pdk.SetError(fmt.Errorf("dotenv: decode RenderNodeInput: %w", err)) return 1 }
if in.ArtifactsProjection == nil || len(in.ArtifactsProjection.Entries) == 0 { pdk.SetError(fmt.Errorf("dotenv: node %q supplied no artifacts to render", in.Node.ID)) return 1 }
var files []aggrega.RenderedFile for _, e := range in.ArtifactsProjection.Entries { body, err := renderBody(e.Name, e.Content) if err != nil { pdk.SetError(fmt.Errorf("dotenv: %w", err)) return 1 } files = append(files, aggrega.RenderedFile{ Path: e.Name, Content: append(provenanceComment(e.Annotations), body...), Format: "text", }) }
outputBytes, err := aggrega.RenderNodeOutput{Files: files}.EncodeCBOR() if err != nil { pdk.SetError(fmt.Errorf("dotenv: encode RenderNodeOutput: %w", err)) return 1 } pdk.Output(outputBytes) return 0}
func main() {}เพย์โหลดrender_node เป็น CBOR แบบDecodeCBOR ของEncodeCBOR ของContent ของ
โฮสต์Content ของ
ความ
guest/go.mod อ้างอิง
module example.com/dotenv-provider
go 1.26.0
require ( github.com/aggrega-ai/aggrega-pdk/go v0.0.0 github.com/extism/go-pdk v1.1.3)
require github.com/aggrega-ai/aggrega-abi/go v0.0.0 // indirect
replace github.com/aggrega-ai/aggrega-pdk/go => /path/to/aggrega-pdk/go
replace github.com/aggrega-ai/aggrega-abi/go => /path/to/aggrega-abi/go3. บิลด์เกสต์
เกสต์
cd guestgo mod tidyGOWORK=off GOOS=wasip1 GOARCH=wasm go build -trimpath -buildvcs=false \ -ldflags "-X github.com/aggrega-ai/aggrega-pdk/go/pkg/aggrega.Version=0.1.0 \ -X github.com/aggrega-ai/aggrega-pdk/go/pkg/aggrega.Commit=walkthru" \ -buildmode=c-shared -o ../dotenv.wasm ./go mod tidy เขียน go.sum ซึ่ง-trimpath และ -buildvcs=false กัน-ldflags สอง
ยืนยัน
aggrega inspect --plugin dotenv.wasmPlugin .wasm: dotenv.wasmExports: render_nodehostFunctions (derived from binary, not yet recorded by aggrega plugin sync): (none)เกสต์
4. สร้างเอนทิตี Plugin ขึ้น
ฟิลด์
overlay.yaml:
apiVersion: aggrega.ai/v1beta1kind: Pluginmetadata: name: dotenvaggrega plugin gen-catalog --plugin dotenv.wasm \ --overlay overlay.yaml --output-dir dev-catalog/plugingenerated dev-catalog/plugin/provider/dotenv/0.1.0/plugin.yamlเอนทิตี
# Generated from the plugin by `aggrega plugin gen-catalog`. You may narrow its capability lists; edit nothing else here.apiVersion: aggrega.ai/v1beta1kind: Pluginmetadata: name: dotenv version: 0.1.0 pluginType: provider description: Render artifact bodies into environment files, quoting and escaping each value as the format requires.spec: abiVersion: v1beta1 exports: - name: render_node provider: capabilities: - dotenvสังเกต.wasm ตำแหน่งpluginType/name บวก
5. วางเกสต์ในที่ที่บิลด์จะมองหา
ขณะ.wasm วาง ข้าง เอนทิตี
dev-catalog/plugin/provider/dotenv/0.1.0/plugin.yamldev-catalog/plugin/provider/dotenv/0.1.0/provider--dotenv.wasmcp dotenv.wasm dev-catalog/plugin/provider/dotenv/0.1.0/provider--dotenv.wasmชื่อdotenv ที่
คัด
เอนทิตี.wasm ของ.wasm ทั้ง.wasm is not on this machine (ไม่
ชี้.aggrega-config.yaml:
workspace: catalog_dirs: - catalog - dev-catalogauto_pull: trueauto_pull ทำให้ aggrega build หา
6. เอกสารที่มุ่งเป้าไปที่ผู้ให้บริการ
ไฟล์
catalog/component/example/compute/checkout/1.0.0/component.yaml:
apiVersion: aggrega.ai/v1beta1kind: Componentmetadata: name: example/compute/checkout version: 1.0.0 description: An application whose configuration is delivered as an environment file. labels: component.aggrega.ai/type: server component.aggrega.ai/category: computespec: schema: values: service_name: type: string default: checkout description: The name the application reports itself under. log_level: type: string default: info description: The application's log verbosity. greeting: type: string default: hello description: A value carrying characters the environment-file format must quote. infra: endpoint: type: string default: http://localhost:8080 description: The endpoint the operator points the application at.catalog/recipe/dotenv/checkout/1.0.0/recipe.yaml:
apiVersion: aggrega.ai/v1beta1kind: Recipemetadata: name: checkout version: 1.0.0 description: Renders the application's configuration as an environment file.spec: capability: dotenv implements: example/compute/checkout:1.0.0
artifacts: app.env: | service-name = {{ .values.service_name }} log.level = {{ .values.log_level }} greeting = {{ .values.greeting }} endpoint = {{ .infra.endpoint }}spec.capability: dotenv คือ
คีย์service-name และ log.level เป็นการ
aggregate.yaml:
apiVersion: aggrega.ai/v1beta1kind: Aggregatemetadata: name: aggregate version: "1.0.0" description: One application whose configuration is an environment file.spec: components: app: type: example/compute/checkout values: service_name: checkout log_level: debug greeting: hello worldmanifest.yaml:
apiVersion: aggrega.ai/v1beta1kind: Manifestmetadata: name: manifest version: "1.0.0" description: Binds the component type to the environment-file recipe.spec: mappings: components: type: example/compute/checkout:1.0.0: checkout:1.0.0release.yaml:
apiVersion: aggrega.ai/v1beta1kind: Releasemetadata: name: walkthrough environment: production version: "1.0.0"spec: aggregate: aggregate manifest: manifest boundaries: infra: directory: infra components: app: infra: endpoint: "https://checkout.example.net"7. บิลด์ครั้งแรก และสิ่งที่บิลด์ปฏิเสธ
aggrega build --release release.yaml --auto-pull warning: plugin "provider/dotenv" resolved from local with no recorded digest to check it against, so it runs unverified; it is read from beside its Plugin entity in a catalog directory on every build, and a pull records its digest only for a plugin attached at a transform role; otherwise publish the plugin and reference the published entity to have its digest recorded[… omitted: the notice saying the Release could not be rendered to discover its external inputs, carrying the error below beneath its first line]Pull complete: release.lock (entities=5, source_artifacts=0, external_inputs=0)[… omitted: a line saying again that external inputs were not fully discovered][CATALOG_ERROR E0265] instance app provider does not support the node's input kind (node "app"). provider "dotenv" does not support artifacts-only recipes (this provider supports: none); declare one of the supported input kinds, or target a provider that supports artifacts-onlyอ่าน
คำ
ข้อnone — this provider supports: none ตัว
นี่describe() ไม่ใช่
8. ประกาศสัญญาพฤติกรรมการเรนเดอร์
เพิ่มguest/main.go:
Capabilities: []string{"dotenv"}, Contract: &aggrega.ProviderContract{ InputKinds: aggrega.InputKinds{ Sourced: false, Synthesized: false, ArtifactsStandalone: true, }, BoundaryAssembly: "none", }, Exports: []aggrega.PluginExport{ArtifactsStandalone: true คือfalse สองrender_node แล้ว
BoundaryAssembly: "none" หมายความmerge หรือ nest แทน และassemble_boundary เพื่อ
บิลด์.wasm ใหม่
cd guestGOWORK=off GOOS=wasip1 GOARCH=wasm go build -trimpath -buildvcs=false \ -ldflags "-X github.com/aggrega-ai/aggrega-pdk/go/pkg/aggrega.Version=0.1.0 \ -X github.com/aggrega-ai/aggrega-pdk/go/pkg/aggrega.Commit=walkthru" \ -buildmode=c-shared -o ../dotenv.wasm ./cd ..aggrega plugin gen-catalog --plugin dotenv.wasm \ --overlay overlay.yaml --output-dir dev-catalog/plugincp dotenv.wasm dev-catalog/plugin/provider/dotenv/0.1.0/provider--dotenv.wasmเอนทิตี
[… omitted: the header comments and the metadata block, unchanged from the first generation]spec: abiVersion: v1beta1 exports: - name: render_node provider: capabilities: - dotenv contract: inputKinds: artifactsStandalone: true boundaryAssembly: noneการ
9. บิลด์ที่เรนเดอร์
aggrega build --release release.yaml --auto-pull warning: plugin "provider/dotenv" resolved from local with no recorded digest to check it against, so it runs unverified; it is read from beside its Plugin entity in a catalog directory on every build, and a pull records its digest only for a plugin attached at a transform role; otherwise publish the plugin and reference the published entity to have its digest recordedPull complete: release.lock (entities=5, source_artifacts=0, external_inputs=0)[… omitted: a line reporting total build time, which differs on every run]Build completed: 1 file written to <workspace>/distบรรทัด<workspace> แทน
dist/infra/dotenv/app/app.envและ
# aggrega.ai/boundary-path=infraSERVICE_NAME=checkoutLOG_LEVEL=debugGREETING="hello world"ENDPOINT=https://checkout.example.netทุก
service-nameและlog.levelเข้ามา ตาม ที่ เขียน และ ออก ไป เป็น SERVICE_NAMEและLOG_LEVELผู้เขียน ไม่ ต้อง เขียน ไวยากรณ์ ของ เป้า หมาย เลย GREETINGถูกใส่ เครื่องหมาย อัญประกาศ และ อีก สาม ตัว ไม่ ถูก ใส่ hello worldมีช่อง ว่าง หาก ไม่ ใส่ เครื่องหมาย อัญประกาศ จะ จบ ที่ helloส่วนปลาย ทาง ไม่มี อักขระ ใด ที่ รูป แบบ อ่าน เป็น ไวยากรณ์ การ ใส่ เครื่องหมาย อัญประกาศ จึง เพิ่ม แต่ สิ่ง รบกวน - คีย์
ที่มา สอง คีย์ เป็น ความ คิด เห็น เพราะ ไฟล์ สภาพ แวดล้อม ไม่มี ที่ อื่น ให้ ใส่ ผู้ ให้ บริการ ที่ เอาต์พุต มี บล็อก เมทาดาทา จะ ใส่ คีย์ เดียวกัน ไว้ ที่ นั่น แทน โฮสต์ คำนวณ คีย์ เกสต์ เลือก ตำแหน่ง - ค่า
มา จาก ผู้ เขียน สอง คน ที่ ต่าง กัน service_name,log_levelและgreetingเป็นเจตนา ของ นัก พัฒนา จาก Aggregate ส่วน endpointเป็นของ ผู้ ดำเนิน การ จาก Release เกสต์ ไม่ เห็น ความ ต่าง ทั้ง สอง เลย — ได้ รับ เนื้อหา ที่ ประเมิน แล้ว หนึ่ง ชุด
10. สองสิ่งที่ควรตรวจก่อนไว้ใจ
ว่า
cp -r dist ../capture && rm -rf distaggrega build --release release.yamldiff -r ../capture distdiff ที่
ว่าการ
artifacts: app.env: | service-name = {{ .values.service_name }} this line declares no value[… omitted: the unverified-plugin warning, and the pull's lines saying the Release could not be rendered to discover its external inputs][CATALOG_ERROR] instance app rendering node "app" caused by: wasm: plugin "provider/dotenv:0.1.0" call to render_node: dotenv: app.env line 2: "this line declares no value" declares no value; an entry is KEY=valueข้อความ
11. สิ่งที่ควรเพิ่มต่อไป
ผู้
-
assemble_boundaryเพื่อรวม เอาต์พุต ของ หลาย ส่วน ประกอบ เป็น เอกสาร เดียว ประกาศ ผ่าน BoundaryAssembly -
validate_node_inputเพื่อตรวจ สอบ อินพุต ของ โหนด เชิง โครงสร้าง ก่อน การ เรนเดอร์ ใด รัน อินพุต ที่ ผิด รูป จึง ถูก รายงาน ที่ เส้น แบ่ง ของ การ เขียน แทนที่ จะ กลาง การ เรนเดอร์ -
ข้อ
เท็จ เพื่อจริง ของ การ เรนเดอร์ รายงาน บาง อย่าง เกี่ยว กับ สิ่ง ที่ เกสต์ เขียน ซึ่ง มี เพียง ความ รู้ ของ ผู้ ให้ บริการ นี้ เรื่อง รูป แบบ ไฟล์ สภาพ แวดล้อม ที่ มอง เห็น ได้ — ใน ที่ นี้ คือ ชื่อ ตัวแปร ที่ แต่ละ ไฟล์ กำหนด ข้อ เท็จ จริง คือ ชื่อ กับ รายการ ซึ่ง ทั้ง สอง เป็น คำ ศัพท์ ของ ผู้ ให้ บริการ นี้ เอง ส่ง กลับ ไว้ ข้าง ไฟล์ — และ ค่า เป็น อาร์เรย์ JSON เสมอ แม้ มี รายการ เดียว มิ ฉะนั้น บิลด์ หยุด: // names lists the variables the files define, in the order they were written.value, err := json.Marshal(names)if err != nil {pdk.SetError(fmt.Errorf("dotenv: marshal render fact: %w", err))return 1}out := aggrega.RenderNodeOutput{Files: files,RenderFacts: []aggrega.RenderFact{{Name: "variable_names", Value: value}},}คอมไพเลอร์
เก็บ ข้อ เท็จ จริง ตาม ลำดับ ที่ เกสต์ ระบุ ตรวจ เพียง ว่า แต่ละ ค่า เป็น รายการ ส่ง รายการ ให้ เทมเพลต ของ Feature ที่ อ่าน รายการ เหล่า นั้น และ เขียน ไฟล์ เดียวกัน ไม่ ว่า จะ รายงาน ข้อ เท็จ จริง หรือ ไม่ บันทึก ชื่อ ของ แต่ละ ข้อ เท็จ จริง และ สิ่ง ที่ รายการ ของ ข้อ เท็จ จริง นั้น เก็บ เป็น ส่วน หนึ่ง ของ สัญญา ของ ผู้ ให้ บริการ -
รายการ
ที่ เมื่อละ ออก ผู้ ให้ บริการ เรนเดอร์ ซอร์ส อินพุต ของ การ เรนเดอร์ ซอร์ส อาจ มี Omitsซึ่งเป็น รายการ ที่ ระบุ หน่วย ที่ เรนเดอร์ ซึ่ง ต้อง ละไว้ ข้าง SourceListingผู้ให้ บริการ ที่ ปฏิบัติ ตาม จะ ละ ทุก หน่วย ที่ ตรง กัน ออก จาก สิ่ง ที่ ส่ง กลับ รายงาน จำนวน หนึ่ง ค่า ต่อ รายการ ใน Omittedและประกาศ Omits: trueในสัญญา การของ ตน รายการ เหล่า นี้ ไม่ เคย มา พร้อม การ เรนเดอร์ แบบ อาร์ติแฟกต์ อย่าง เดียว เช่น ของ ผู้ ให้ บริการ นี้ ผู้ ให้ บริการ นี้ จึง พบ รายการ เหล่า นั้น หลัง เรนเดอร์ อินพุต แบบ อิง ซอร์ส เท่านั้น ละ ในหน่วย ที่ เรนเดอร์ ออก สัญญา ของ ปลั๊กอิน มี รูป ทรง ทั้งหมด -
การ
เผย ซึ่งแพร่ เปลี่ยน การ จัด วาง dev-catalogภายในเครื่อง คู่มือเป็น อาร์ติแฟกต์ ที่ ปัก หมุด ด้วย ไดเจสต์ ซึ่ง Release หา ค่า และ ยืนยัน ได้ นัก ครอบคลุมพัฒนา ปลั๊กอิน วงจร ชีวิต นั้น ภาย ใต้ ขั้น ตอน การ เผย แพร่ ขั้น ตอน pull และ ขั้น ตอน บิลด์
เพย์โหลด
ข้อมูลอ้างอิงที่เกี่ยวข้อง
- คู่มือ
นัก — เอนทิตีพัฒนา ปลั๊กอิน Wasm Pluginพื้นผิว ความ สามารถ และ วงจร ชีวิต การ เผย แพร่ - สัญญา
ของ Plugin — รูปทรง อินพุต และ เอาต์พุต ของ ทุก ซ็อกเก็ต - สถาปัตยกรรม
ของ Provider — วิธีที่ ผู้ ให้ บริการ เข้า กับ ไปป์ไลน์ การ คอมไพล์ - ดัชนี
อ้างอิง Provider — สิ่งที่ ผู้ ให้ บริการ ใน ไลบรารี มาตรฐาน แต่ละ ราย สร้าง ขึ้น - การ
อ่าน — ธรรมเนียมเอาต์พุต ใน หน้า เหล่า นี้ ที่ บล็อก เอาต์พุต ข้าง ต้น ปฏิบัติ ตาม