Skip to content

Commit

Permalink
Merge pull request #26 from golemcloud/template-to-component
Browse files Browse the repository at this point in the history
Revert naming from template to component
  • Loading branch information
vigoo authored Apr 24, 2024
2 parents 967a787 + 95d1d4c commit 854e76c
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 90 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
- name: Tests
run: cargo test --all-features
- name: List bundled examples
run: cargo run --features=build-binary list-templates
run: cargo run --features=build-binary list-examples
publish:
needs: [ build ]
if: "startsWith(github.ref, 'refs/tags/v')"
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ Golem examples are currently simple and not using any known template language, i

When calling `golem-new` the user specifies a **template name**. The provided component name must use either `PascalCase`, `snake_case` or `kebab-case`.

There is an optional parameter for defining a **package name**, which defaults to `golem:template`. It has to be in the `pack:name` format.
There is an optional parameter for defining a **package name**, which defaults to `golem:component`. It has to be in the `pack:name` format.

The following occurrences get replaced to the provided component name, applying the casing used in the template:
- `template-name`
- `TemplateName`
- `template_name`
- `component-name`
- `ComponentName`
- `component_name`
- `pack::name`
- `pack:name`
- `pack_name`
Expand Down
62 changes: 31 additions & 31 deletions examples/rust/rust-auction/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ By sharding the state this way the application can be highly scalable because us

Our auction will not require any database because we can just store the state of the auction registry and each auction with in memory data structures and rely on Golem's durability guarantees. Of course if we had large files like images to go along with our auctions we might still want to use a database.

To run an auction we first need to upload the templates for our auction worker and auction registry. Let's upload the template for the auction worker first since we will need to provide the template identifier for the auction worker to the auction registry so it can create new auction workers as needed.
To run an auction we first need to upload the components for our auction worker and auction registry. Let's upload the component for the auction worker first since we will need to provide the component identifier for the auction worker to the auction registry so it can create new auction workers as needed.

The code for both of our workers are written in Rust, though we code have written them in any language that compiles to WebAssembly ("WASM"). To compile them to WASM we just do:

Expand All @@ -16,44 +16,44 @@ cargo component build --release

This will create two files, `auction.wasm` and `auction-registry.wasm`, in the `target/wasm32-wasi-release` folder that represent the code for our workers compiled to WASM. Copy them to the current directory so they are easily accessible.

We can upload the template for the auction worker to Golem like this:
We can upload the component for the auction worker to Golem like this:

```bash
golem-cli template add --template-name auction-1 auction.wasm
golem-cli component add --component-name auction-1 auction.wasm
```

This will return some metadata about the template we have just uploaded:
This will return some metadata about the component we have just uploaded:

```
template_id: 69dd184e-1fef-4925-800c-8a0d91ef2ef3
template_version: 0
template_name: auction-1
template_size: 2143417
component_id: 69dd184e-1fef-4925-800c-8a0d91ef2ef3
component_version: 0
component_name: auction-1
component_size: 2143417
exports:
- 'golem:template/api/initialize(auction: {auction-id: {auction-id: str}, name: str, description: str, limit-price: f32, expiration: u64}) => '
- 'golem:template/api/bid(bidder-id: {bidder-id: str}, price: f32) => variant(auction-expired: (), price-too-low: (), success: ())'
- 'golem:template/api/close-auction() => {bidder-id: str}?'
- 'golem:component/api/initialize(auction: {auction-id: {auction-id: str}, name: str, description: str, limit-price: f32, expiration: u64}) => '
- 'golem:component/api/bid(bidder-id: {bidder-id: str}, price: f32) => variant(auction-expired: (), price-too-low: (), success: ())'
- 'golem:component/api/close-auction() => {bidder-id: str}?'
```

Of particular relevance to us is the `template_id`, which uniquely identifies each worker template. We will need that shortly when we create the auction registry worker so it knows how to create new auction workers as needed.
Of particular relevance to us is the `component_id`, which uniquely identifies each worker component. We will need that shortly when we create the auction registry worker so it knows how to create new auction workers as needed.

You will also notice that the metadata includes the signatures of all the functions that the worker exports. These exported functions represent the public API of our worker and we will use that later to bid on our auction.

We can upload the template for the auction registry for Golem in the same way:
We can upload the component for the auction registry for Golem in the same way:

```bash
golem-cli template add --template-name auction_registry-1 auction_registry.wasm
golem-cli component add --component-name auction_registry-1 auction_registry.wasm
```

```
template_id: d4da0a79-a31c-43ca-be6c-2a7fddd9e33e
template_version: 0
template_name: auction-registry-1
template_size: 2710063
component_id: d4da0a79-a31c-43ca-be6c-2a7fddd9e33e
component_version: 0
component_name: auction-registry-1
component_size: 2710063
exports:
- 'golem:template/api/create-bidder(name: str, address: str) => {bidder-id: str}'
- 'golem:template/api/create-auction(name: str, description: str, limit-price: f32, expiration: u64) => {auction-id: str}'
- 'golem:template/api/get-auctions() => [{auction-id: {auction-id: str}, name: str, description: str, limit-price: f32, expiration: u64}]'
- 'golem:component/api/create-bidder(name: str, address: str) => {bidder-id: str}'
- 'golem:component/api/create-auction(name: str, description: str, limit-price: f32, expiration: u64) => {auction-id: str}'
- 'golem:component/api/get-auctions() => [{auction-id: {auction-id: str}, name: str, description: str, limit-price: f32, expiration: u64}]'
```

We're almost ready to create our first worker. We just need one more thing which is our authorization token, which the auction registry will need to have access to in order to create new auction workers on our behalf.
Expand All @@ -79,24 +79,24 @@ The token secret gives the ability to interact with Golem services on your behal
With this we have everything we need to deploy our auction service. We can create our auction registry work like this:

```bash
golem-cli worker add --template-name auction-registry-1 --worker-name auction-registry -1 --env "GOLEM_TOKEN_SECRET"="07db308e-3721-432d-9aa2-0a6390e0781c" --env "AUCTION_TEMPLATE_ID"="69dd184e-1fef-4925-800c-8a0d91ef2ef3"
golem-cli worker add --component-name auction-registry-1 --worker-name auction-registry -1 --env "GOLEM_TOKEN_SECRET"="07db308e-3721-432d-9aa2-0a6390e0781c" --env "AUCTION_COMPONENT_ID"="69dd184e-1fef-4925-800c-8a0d91ef2ef3"
```

```
workerId:
rawTemplateId: d4da0a79-a31c-43ca-be6c-2a7fddd9e33e
componentId: d4da0a79-a31c-43ca-be6c-2a7fddd9e33e
workerName: auction-registry-1
templateVersionUsed: 0
componentVersionUsed: 0
```

We are using environment variables to provide our auction registry worker with both our authorization token and the identifier for the template of our auction worker. Internally our auction registry worker will be able to use this information to create new auction workers for each new auction we create.
We are using environment variables to provide our auction registry worker with both our authorization token and the identifier for the component of our auction worker. Internally our auction registry worker will be able to use this information to create new auction workers for each new auction we create.

Since the auction registry will take care of creating new auctions as needed there is nothing else we need to do to deply our application.
Since the auction registry will take care of creating new auctions as needed there is nothing else we need to do to deploy our application.

Let's get started by registering as a bidder. We can see from the metadata for the auction registry that `create-bidder` expects two strings representing the bidder's name and address and returns a `bidder-id`:

```bash
golem-cli worker invoke-and-await --template-name=auction-registry-1 --worker-name=auction-registry-1 --function=golem:template/api/create-bidder --parameters='["Adam", "123 green street"]'
golem-cli worker invoke-and-await --component-name=auction-registry-1 --worker-name=auction-registry-1 --function=golem:component/api/create-bidder --parameters='["Adam", "123 green street"]'
```

```
Expand All @@ -105,10 +105,10 @@ golem-cli worker invoke-and-await --template-name=auction-registry-1 --worker-na

Let's also create an auction. An auction requires an item, a description, a limit price, and an expiration date in seconds since the epoch.

We'll use an expiration date that correponds to October 12, 2023, about one week in the future as of when this was written. This should be updated to a date that is in the future as of when we create our auction so that we can bid on it.
We'll use an expiration date that corresponds to October 12, 2023, about one week in the future as of when this was written. This should be updated to a date that is in the future as of when we create our auction so that we can bid on it.

```bash
golem-cli worker invoke-and-await --template-name=auction-registry-1 --worker-name=auction-registry-1 --function=golem:template/api/create-auction --parameters='["My first auction", "A simple auction", 100, 1697083549]'
golem-cli worker invoke-and-await --component-name=auction-registry-1 --worker-name=auction-registry-1 --function=golem:component/api/create-auction --parameters='["My first auction", "A simple auction", 100, 1697083549]'
```

```
Expand All @@ -120,7 +120,7 @@ This `auction-id` is the key for us to interact with our auction directly.
Let's try bidding on our auction. We'll first try to enter a bid that is below the limit price:

```bash
golem-cli worker invoke-and-await --template-name=auction-1 --worker-name=auction-6fff4e1c-e7d6-49dc-b60c-2484ab6d7a4c --function=golem:template/api/bid --parameters='[{ "bidder-id": "a11ff221-d861-42e2-bc49-23b48b722ee3" }, 50]'
golem-cli worker invoke-and-await --component-name=auction-1 --worker-name=auction-6fff4e1c-e7d6-49dc-b60c-2484ab6d7a4c --function=golem:component/api/bid --parameters='[{ "bidder-id": "a11ff221-d861-42e2-bc49-23b48b722ee3" }, 50]'
```

```
Expand All @@ -130,7 +130,7 @@ golem-cli worker invoke-and-await --template-name=auction-1 --worker-name=auctio
Our price was too low! Let's try again with a higher price!

```bash
golem-cli worker invoke-and-await --template-name=auction-1 --worker-name=auction-6fff4e1c-e7d6-49dc-b60c-2484ab6d7a4c --function=golem:template/api/bid --parameters='[{ "bidder-id": "a11ff221-d861-42e2-bc49-23b48b722ee3" }, 200]'
golem-cli worker invoke-and-await --component-name=auction-1 --worker-name=auction-6fff4e1c-e7d6-49dc-b60c-2484ab6d7a4c --function=golem:component/api/bid --parameters='[{ "bidder-id": "a11ff221-d861-42e2-bc49-23b48b722ee3" }, 200]'
```

```
Expand Down
12 changes: 6 additions & 6 deletions examples/rust/rust-auction/auction-registry/src/auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ pub fn create(auction: Auction) {

fn create_worker(auction_id: AuctionId) {
let client = Client::new();
let template_id = env::var("AUCTION_TEMPLATE_ID").unwrap();
let component = env::var("AUCTION_COMPONENT_ID").unwrap();
let url = format!(
"https://release.api.golem.cloud/v1/templates/{}/workers",
template_id
component
);
let body = CreateWorkerBody::new(format!("auction-{}", auction_id.auction_id));
let token = env::var("GOLEM_TOKEN_SECRET").unwrap();
Expand All @@ -30,11 +30,11 @@ fn create_worker(auction_id: AuctionId) {

fn get_invocation_key(auction_id: AuctionId) -> InvocationKey {
let client = Client::new();
let template_id = env::var("AUCTION_TEMPLATE_ID").unwrap();
let component = env::var("AUCTION_COMPONENT_ID").unwrap();
let worker_id = format!("auction-{}", auction_id.auction_id);
let url = format!(
"https://release.api.golem.cloud/v1/templates/{}/workers/{}/key",
template_id, worker_id
component, worker_id
);
let token = env::var("GOLEM_TOKEN_SECRET").unwrap();
let response = client
Expand All @@ -48,9 +48,9 @@ fn get_invocation_key(auction_id: AuctionId) -> InvocationKey {

fn initialize_worker(auction: Auction, invocation_key: InvocationKey) {
let client = Client::new();
let template_id = env::var("AUCTION_TEMPLATE_ID").unwrap();
let component = env::var("AUCTION_COMPONENT_ID").unwrap();
let worker_id = format!("auction-{}", auction.auction_id.auction_id);
let url = format!("https://release.api.golem.cloud/v1/templates/{}/workers/{}/invoke-and-await", template_id, worker_id);
let url = format!("https://release.api.golem.cloud/v1/templates/{}/workers/{}/invoke-and-await", component, worker_id);
let body = InitializeWorkerBody::new(auction);
let token = env::var("GOLEM_TOKEN_SECRET").unwrap();
let function = "golem:template/api/initialize";
Expand Down
18 changes: 9 additions & 9 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clap::*;
use golem_examples::model::{
ExampleName, ExampleParameters, GuestLanguage, GuestLanguageTier, PackageName, TemplateName,
ComponentName, ExampleName, ExampleParameters, GuestLanguage, GuestLanguageTier, PackageName,
};
use golem_examples::*;
use std::env;
Expand All @@ -14,13 +14,13 @@ enum Command {
example: ExampleName,

#[arg(short, long)]
template_name: TemplateName,
component_name: ComponentName,

#[arg(short, long)]
package_name: Option<PackageName>,
},
#[command()]
ListTemplates {
ListExamples {
#[arg(short, long)]
min_tier: Option<GuestLanguageTier>,

Expand All @@ -41,7 +41,7 @@ pub fn main() {
match &command.command {
Command::New {
example: example_name,
template_name,
component_name,
package_name,
} => {
let examples = GolemExamples::list_all_examples();
Expand All @@ -54,23 +54,23 @@ pub fn main() {
match GolemExamples::instantiate(
example,
ExampleParameters {
template_name: template_name.clone(),
component_name: component_name.clone(),
package_name: package_name
.clone()
.unwrap_or(PackageName::from_string("golem:template").unwrap()),
.unwrap_or(PackageName::from_string("golem:component").unwrap()),
target_path: cwd,
},
) {
Ok(instructions) => println!("{instructions}"),
Err(err) => eprintln!("Failed to instantiate template: {err}"),
Err(err) => eprintln!("Failed to instantiate example: {err}"),
}
}
None => {
eprintln!("Unknown template {example_name}. Use the list-templates command to see the available commands.");
eprintln!("Unknown example {example_name}. Use the list-examples command to see the available commands.");
}
}
}
Command::ListTemplates { min_tier, language } => {
Command::ListExamples { min_tier, language } => {
GolemExamples::list_all_examples()
.iter()
.filter(|example| match language {
Expand Down
12 changes: 6 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl Examples for GolemExamples {
&example.example_path,
&parameters
.target_path
.join(parameters.template_name.as_string()),
.join(parameters.component_name.as_string()),
&parameters,
&example.exclude,
true,
Expand All @@ -72,7 +72,7 @@ impl Examples for GolemExamples {
adapter_path,
&parameters
.target_path
.join(parameters.template_name.as_string())
.join(parameters.component_name.as_string())
.join("adapters")
.join(example.language.tier().name())
.join(adapter_path.file_name().unwrap().to_str().unwrap()),
Expand All @@ -84,7 +84,7 @@ impl Examples for GolemExamples {
wit_dep,
&parameters
.target_path
.join(parameters.template_name.as_string())
.join(parameters.component_name.as_string())
.join("wit")
.join("deps")
.join(wit_dep.file_name().unwrap().to_str().unwrap()),
Expand Down Expand Up @@ -186,9 +186,9 @@ fn copy_all(catalog: &Dir<'_>, source_path: &Path, target_path: &Path) -> io::Re

fn transform(str: impl AsRef<str>, parameters: &ExampleParameters) -> String {
str.as_ref()
.replace("component-name", &parameters.template_name.to_kebab_case())
.replace("ComponentName", &parameters.template_name.to_pascal_case())
.replace("component_name", &parameters.template_name.to_snake_case())
.replace("component-name", &parameters.component_name.to_kebab_case())
.replace("ComponentName", &parameters.component_name.to_pascal_case())
.replace("component_name", &parameters.component_name.to_snake_case())
.replace(
"pack::name",
&parameters.package_name.to_string_with_double_colon(),
Expand Down
Loading

0 comments on commit 854e76c

Please sign in to comment.