Skip to content

Commit

Permalink
cellery hello world api sample 🎉
Browse files Browse the repository at this point in the history
  • Loading branch information
isurulucky committed Apr 9, 2019
1 parent e3305a5 commit 98059c9
Show file tree
Hide file tree
Showing 5 changed files with 245 additions and 0 deletions.
63 changes: 63 additions & 0 deletions hello-world-api/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Copyright (c) 2019 WSO2 Inc. (http:www.wso2.org) All Rights Reserved.
#
# WSO2 Inc. licenses this file to you under the Apache License,
# Version 2.0 (the "License"); you may not use this file except
# in compliance with the License.
# You may obtain a copy of the License at
#
# http:www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

PROJECT_ROOT := $(realpath $(dir $(abspath $(lastword $(MAKEFILE_LIST)))))
DOCKER_REPO ?= wso2cellery
DOCKER_IMAGE_TAG ?= latest

SAMPLE := hello-world-api
COMPONENT := hello-service

all: clean build docker

.PHONY: clean.$(SAMPLE)
clean.$(SAMPLE): clean

.PHONY: check-style.$(SAMPLE)
check-style.$(SAMPLE): check-style

.PHONY: build.$(SAMPLE)
build.$(SAMPLE): build

.PHONY: docker.$(SAMPLE)
docker.$(SAMPLE): docker

.PHONY: docker-push.$(SAMPLE)
docker-push.$(SAMPLE): docker-push

.PHONY: clean
clean:
cd components/$(COMPONENT); \
rm -f hello-service

.PHONY: check-style
check-style:
@:

PHONY: build
build:
cd components/$(COMPONENT); \
CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o hello-service hello-service.go

.PHONY: docker
docker:
cd components/$(COMPONENT); \
docker build -t ${DOCKER_REPO}/samples-$(SAMPLE):${DOCKER_IMAGE_TAG} .

.PHONY: docker-push
docker-push:
cd components/$(COMPONENT); \
docker push ${DOCKER_REPO}/samples-$(SAMPLE):${DOCKER_IMAGE_TAG}
92 changes: 92 additions & 0 deletions hello-world-api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
Hello World API
===============

Hello World API sample is a simple rest API that returns the text 'Hello World!' as a response to a GET request.

## Getting Started

### Checkout the Sample

1. Clone the [wso2-cellery/samples](https://github.com/wso2-cellery/samples) repository.

2. Navigate to the hello-world-api Sample.
```
cd <SAMPLES_ROOT>/hello-world-api
```

### Running the Hello World API Cell

1. Build the hello-world-api Cell.
```
cellery build hello-world-api.bal wso2cellery/hello-world-api:0.1.0
```
2. Deploy the hello-world Cell
```
cellery run wso2cellery/hello-world-api:0.1.0 -n my-hello-api
```
3. Check the running Cell.
```
cellery status my-hello-api
```
Once the status becomes 'Ready' you can go ahead and invoke it.

4. Invoking the Hello World API

By default, the Cellery installation will be using the following domain names for the relevant components in Cellery:
* cellery-dashboard
* wso2-apim-gateway
* cellery-k8s-metrics
* wso2-apim
* idp.cellery-system
* wso2sp-observability-api

These domains should be mapped to the IP of Kubernetes Ingress Controller in the Cellery installation.

To invoke the API, an oauth2 token should be obtained from the API store, as described in the following steps:

1. Login to the [API Store](https://wso2-apim/store/) using admin:admin credentials.

2. Click on ‘my_hello_api_global_1_0_0_hello’ to create a subscription and generate a token.
(See [Subscribing to an API](https://docs.wso2.com/display/AM260/Subscribe+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/my-hello-api/hello/ -kv
```
## Building the Components from Source
You do not need to build the Components if you just wish to deploy the Cells. This should only be done if you wish to change the Hello World API sample and play around with it.
### Prerequisites
* Docker
* GNU Make 4.1+
### Building the Components
If you wish to change the Hello World API Sample and play around with Cellery, you can follow this section to rebuild the Components.
1. [Checkout](#checkout-the-sample) the Sample
2. Set the following environment variables for customizing the build.
| Environment Variable | |
|-----------------------|-----------------------------------------------------------------------|
| DOCKER_REPO | The name of the repository of the Docker images (Your Docker Hub ID) |
| DOCKER_IMAGE_TAG | The tag of the Docker images |
3. Run the make target for building docker images.
```
make docker
```
This would build the components from source and build the docker images using the environment variables you have provided.
4. Login to Docker Hub
```
docker login
```
5. Run the target for pushing the docker images.
```
make docker-push
```
6. Update the `<SAMPLES_ROOT>/hello-world-api/hello-world-api.bal` file and set the newly created image names for the component source.
7. [Build and run](#getting-started) the Cells.
3 changes: 3 additions & 0 deletions hello-world-api/components/hello-service/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM scratch
COPY hello-service /
ENTRYPOINT ["/hello-service","-logtostderr=true"]
33 changes: 33 additions & 0 deletions hello-world-api/components/hello-service/hello-service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2019 WSO2 Inc. (http:www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http:www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package main

import (
"fmt"
"net/http"
)

func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello World! \n")
})

fmt.Println("hello service is listening on port 9090")
http.ListenAndServe(":9090", nil)
}
54 changes: 54 additions & 0 deletions hello-world-api/hello-world-api.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// Copyright (c) 2019 WSO2 Inc. (http:www.wso2.org) All Rights Reserved.
//
// WSO2 Inc. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
// in compliance with the License.
// You may obtain a copy of the License at
//
// http:www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//

import ballerina/io;
import celleryio/cellery;

//Hello World Component
cellery:Component helloComponent = {
name: "hello-api",
source: {
image: "docker.io/wso2cellery/samples-hello-world-api"
},
ingresses: {
helloApi: <cellery:HttpApiIngress>{ port: 9090,
context: "hello",
definition: {
resources: [
{
path: "/",
method: "GET"
}
]
},
expose: "global"
}
}
};

cellery:CellImage helloCell = {
components: {
helloComp: helloComponent
}
};

public function build(cellery:ImageName iName) returns error? {
//Build Hello Cell
io:println("Building Hello World Cell ...");
return cellery:createImage(helloCell, iName);
}

0 comments on commit 98059c9

Please sign in to comment.