Hello-world API sample is a simple rest API that returns the text 'Hello World!' as a response to a GET request.
Note: To run this sample, a cellery deployment which includes API Manager component is required (complete cellery deployment or basic celley deployment with APIM is required).
- Clone the wso2-cellery/samples repository
- Navigate to the hello-world-api Sample.
cd <SAMPLES_ROOT>/cells/hello-world-api
In this section let's focus on build, run and push a hello world api cell.
The cell helloCell
consists of one component defined as hello
component and it has one http API ingress that is exposed globally.
import ballerina/io;
import celleryio/cellery;
public function build(cellery:ImageName iName) returns error? {
//Hello World Component
cellery:Component helloComponent = {
name: "hello-api",
source: {
image: "docker.io/wso2cellery/samples-hello-world-api-hello-service:latest"
},
ingresses: {
helloApi: <cellery:HttpApiIngress>{ port: 9090,
context: "hello",
definition: {
resources: [
{
path: "/",
method: "GET"
}
]
},
expose: "global"
}
}
};
cellery:CellImage helloCell = {
components: {
helloComp: helloComponent
}
};
//Build Hello Cell
io:println("Building Hello World Cell ...");
return cellery:createImage(helloCell, untaint iName);
}
-
Build the cellery image for hello world project by executing the cellery build command as shown below. Note
DOCKER_HUB_ORG
is your organization name in docker hub.$ cellery build hello-world-api.bal <DOCKER_HUB_ORG>/hello-world-api-cell:latest ✔ Building image <DOCKER_HUB_ORG>/hello-world-api-cell:latest ✔ Saving new Image to the Local Repository ✔ Successfully built cell image: <DOCKER_HUB_ORG>/hello-world-api-cell:latest What's next? -------------------------------------------------------- Execute the following command to run the image: $ cellery run <DOCKER_HUB_ORG>/helloworld:latest --------------------------------------------------------
-
Run the cell image by executing
cellery run
command as shown below.$ cellery run <DOCKER_HUB_ORG>/hello-world-api-cell:latest -n hello-world-api-cell ✔ Extracting Cell Image <DOCKER_HUB_ORG/hello-world-api-cell:latest Main Instance: my-hello-world ✔ Reading Cell Image <DOCKER_HUB_ORG/hello-world-api-cell:latest ✔ Validating environment variables ✔ Validating dependencies Instances to be Used: INSTANCE NAME CELL IMAGE USED INSTANCE SHARED ---------------------- --------------------------------------------- --------------- -------- hello-world-api-cell <DOCKER_HUB_ORG>/hello-world-api-cell:latest To be Created - Dependency Tree to be Used: No Dependencies ? Do you wish to continue with starting above Cell instances (Y/n)? ✔ Starting main instance my-hello-world ✔ Successfully deployed cell image: <DOCKER_HUB_ORG>/hello-world-api-cell:latest What's next? -------------------------------------------------------- Execute the following command to list running cells: $ cellery list instances --------------------------------------------------------
-
Now your hello world cell is deployed, you can run the cellery list instances command to see the status of the deployed cell.
$ cellery list instances INSTANCE CELL IMAGE STATUS GATEWAY COMPONENTS AGE ------------------------------------------ -------------------------------------------- -------- ----------------------------------------------------------------- ------------ ---------------------- hello-world-api-cell-1-0-0-676b2131 sinthuja/hello-world-api-cell:latest Ready sinthuja-hello-world-api-cell-latest-676b2131--gateway-service 1 10 minutes 1 seconds
-
Execute
cellery view
to see the components of your cell. This will open a HTML page in a browser and you can visualize the components and dependent cells of the cell image.$ cellery view <DOCKER_HUB_ORG>/hello-world-api-cell:latest
Since the hello-world-api is exposed via the global gateway, the request can go through the global API gateway to the hello world service. And, by default all the APIs are secured, therefore we need to obtain a token to invoke the API. The below provided steps explains the process to obtain the token and invoke the API.
-
Login to the API Store as admin user (username: admin, password: admin).
-
Click on ‘hello_world_api_cell_global_1_0_0_hello’ to create a subscription and generate a token. (See Subscribing to an API)
-
Once you have subscribed to the API and generated a token, invoke the API passing the same as a Bearer token:
$ curl -H "Authorization: Bearer <token>" https://wso2-apim-gateway/hello-world-api-cell/hello/ -k Hello World!
Congratulations! You have successfully tried out the hello world api sample!
Please feel free to checkout this repository and play around with the sample as explained here