Skip to main content

Quickstart

The cosmo CLI is your one-stop-shop for all things Cosmonic. It puts at your fingertips the ability to interact with and connect to your constellations. As we progress through the developer preview phase, more and more functionality will be added to cosmo, such as the ability to interact with your account, preferences, and much more.

Installing the CLI

Installing the CLI requires just one command:

bash -c "$(curl -fsSL https://cosmonic.sh/install.sh)"

Hello, World!

After installing cosmo, you're ready to generate and run your first application. Let's start with the classic "Hello world":

cosmo tutorial hello

The above command will take you through a few interactive steps to generate a Cosmonic project, authenticate your local machine, and launch your "Hello world" application. This project will consist of a single actor, which is a stateless, reactive component that represents the smallest unit of compute that you can create.

After running through the tutorial, check the output for a curl command that you can run, like:

curl https://floral-flower-1234.cosmonic.app

This is a publicly accessible HTTPS endpoint that makes an HTTP request to your locally running actor. Go ahead, make a request! You should see Hello, World! whether you curl it or open the URL in your browser tab.

Using your editor or terminal of choice, we can take a look at the code that made this possible under hello/src/lib.rs:

use wasmbus_rpc::actor::prelude::*;
use wasmcloud_interface_httpserver::{HttpRequest, HttpResponse, HttpServer, HttpServerReceiver};

#[derive(Debug, Default, Actor, HealthResponder)]
#[services(Actor, HttpServer)]
struct HelloActor {}

/// Implementation of the HTTP server capability
#[async_trait]
impl HttpServer for HelloActor {
async fn handle_request(&self, _ctx: &Context, _req: &HttpRequest) -> RpcResult<HttpResponse> {
HttpResponse::ok("Hello, World!")
}
}

This "Hello, World" application makes use of a single capability: HTTP Server. This allowed our app to receive HTTP requests and return the HTTP response "Hello, World!" Notice that there are no implementation details in this code: no ports, no TLS certificates, and yet you were able to make a request and receive your response. We'll talk more about the fundamentals of capabilities in the next section.

Make it Your Own

The only thing left is to make this application your own. Using an editor of choice, edit the string inside of HttpResponse::ok() to say whatever you'd like!

// For example:
async fn handle_request(&self, _ctx: &Context, _req: &HttpRequest) -> RpcResult<HttpResponse> {
HttpResponse::ok("Hello, Cosmonaut!")
}
info

Since we're editing Rust code, you'll want to make sure you have Rust installed, and ensure you have a WebAssembly toolchain with the following command:

rustup target add wasm32-wasi

Once you're done editing your file, you can deploy the new version using cosmo launch.

~ cosmo launch
>>> ⢋⠁ Building actor...
Compiling hello v0.1.0 (/Users/cosmonaut/hello)
Finished release [optimized] target(s) in 1.11s
🚀 Actor launched!

Now, make another request to the same URL as before. You'll get back the updated HTTP response!

Add Capabilities

We can add any amount of additional logic to change the HTTP response, but to make our application more dynamic, we'll want to add more capabilities. Let's move onto the next section where we dive into capability-driven development.