How to invoke ballerina service in windows cmd - cmd

I'm running my services in ballerina composer, then I'm using Windows cmd to invoke the service using the curl but cmd is complaining that curl is not a recognized command.
How could I do it in cmd?
Please help.

Create your service file, here it is hello_service.bal.
import ballerina/http;
endpoint http:Listener listener {
port:9090
};
service<http:Service> hello bind listener {
sayHello (endpoint caller, http:Request request) {
http:Response response = new;
response.setTextPayload("Hello World!\n");
_ = caller -> respond(response);
}
}
2. Run it on cmd. ballerina run hello_service.bal
3. Make a new main.bal file.
import ballerina/http;
import ballerina/log;
import ballerina/io;
endpoint http:Client clientEndpoint {
url: "http://localhost:9090"
};
function main(string... args) {
// Send a GET request to the Hello World service endpoint.
var response = clientEndpoint->get("/hello/sayHello");
match response {
http:Response resp => {
io:println(resp.getTextPayload());
}
error err => {
log:printError(err.message, err = err);
}
}
}
4. ballerina run main.bal.
5. You can see the result as Hello World! on cmd now.

You don't need to use cURL to invoke Ballerina HTTP services. You can use the HTTP client you normally use (e.g., Postman). If you really want to, you can install cURL in Windows as well: https://curl.haxx.se/download.html.

Related

kitex "could not import echoTest/kitex_gen/echo"

when I use kitex to start an examples, I Got an error like below
for my step:
mkdir -p Protobuf-test
new file whose name is "echo.proto" and content is like this
syntax = "proto3";
option go_package = "echo";
package echo;
message Request {
string msg = 1;
}
message Response {
string msg = 1;
}
service EchoService {
rpc ClientSideStreaming(stream Request) returns (Response){} // 客户端streaming
rpc ServerSideStreaming(Request) returns (stream Response){} // 服务端streaming
rpc BidiSideStreaming(stream Request) returns (stream Response){} //双向流
}
open a terminal executed a command
kitex -type protobuf -module echoTest -service echoTest echo.proto
please give me some advise, thanks very mush
I take a comparison to using "thrift", it is ok, in the directory "xx/kitex_gen/echo", there has a file named echo.go, but not when using protobuf as model

Ballerina integrator and Elasticsearch

i am working on my bachelor degree project and i have to use the Ballerina integrator middle-ware.
Now i need to make a service which will be able to index data coming to ballerina into Elasticsearch.Is there a way to communicate with Elasticsearch using only ballerina (without using Log stash or File beat..) just like we were communicating to a SQL database?
If there is someone looking for the same thing, i just found a way to communicate with Elastic and it's working very well
---here is the code---
import ballerina/http;
import ballerina/log;
listener http:Listener httpListener = new(9090);
http:Client elasticDB = new("http://localhost:9200/");
#http:ServiceConfig{
basePath: "/elastic"
}
service GetElasticSource on httpListener{
#http:ResourceConfig {
methods: ["GET"],
path: "/{index}/_source/{id}"
}
resource function retrieveElasticIndexById(http:Caller httpCaller, http:Request httpRequest, string index, string id){
http:Response resp = new;
var data = elasticDB->get(<#untained> ("/"+index+"/_source/"+id));
if(data is http:Response){
resp = data;
}
var respRet = httpCaller->respond(resp);
if(respRet is error){
log:printError("error responding to the client", err = respRet);
}
}
}

GRPC-GO:Client stub not shown in the generated pb.go file

I am trying to learn GRPC from the official doc, Here is the tutorial I have followed grpc-go
Generating the proto using this command
protoc --go_out=$PWD helloworld/helloworld.proto
This above command will generate the file helloworld.pb.go without any problem but the problem is the code for the client stub is missing from the generated file
syntax = "proto3";
package helloworld;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
The actual error i am getting from the client connection which is
undefined: helloworld.NewGreeterClient
And this has occurred from the line c := pb.NewGreeterClient(conn) in the greeter_client/main.go file
The reason behind because the client stub not generated in the generated file
Issue resolved i have some problems with the command
Here is the actual command
protoc --go_out=plugins=grpc:$PWD helloworld.proto
Add --I to your command. e.g.
protoc -I helloworld --go_out=${PWD} helloworld/*.proto

Examples of integrating moleculer-io with moleculer-web using moleculer-runner instead of ServiceBroker?

I am having fun with using moleculer-runner instead of creating a ServiceBroker instance in a moleculer-web project I am working on. The Runner simplifies setting up services for moleculer-web, and all the services - including the api.service.js file - look and behave the same, using a module.exports = { blah } format.
I can cleanly define the REST endpoints in the api.service.js file, and create the connected functions in the appropriate service files. For example aliases: { 'GET sensors': 'sensors.list' } points to the list() action/function in sensors.service.js . It all works great using some dummy data in an array.
The next step is to get the service(s) to open up a socket and talk to a local program listening on an internal set address/port. The idea is to accept a REST call from the web, talk to a local program over a socket to get some data, then format and return the data back via REST to the client.
BUT When I want to use sockets with moleculer, I'm having trouble finding useful info and examples on integrating moleculer-io with a moleculer-runner-based setup. All the examples I find use the ServiceBroker model. I thought my Google-Fu was pretty good, but I'm at a loss as to where to look to next. Or, can i modify the ServiceBroker examples to work with moleculer-runner? Any insight or input is welcome.
If you want the following chain:
localhost:3000/sensor/list -> sensor.list() -> send message to local program:8071 -> get response -> send response as return message to the REST caller.
Then you need to add a socket io client to your sensor service (which has the list() action). Adding a client will allow it to communicate with "outside world" via sockets.
Check the image below. I think it has everything that you need.
As a skeleton I've used moleculer-demo project.
What I have:
API service api.service.js. That handles the HTTP requests and passes them to the sensor.service.js
The sensor.service.js will be responsible for communicating with remote socket.io server so it needs to have a socket.io client. Now, when the sensor.service.js service has started() I'm establishing a connection with a remote server located at port 8071. After this I can use this connection in my service actions to communicate with socket.io server. This is exactly what I'm doing in sensor.list action.
I've also created remote-server.service.js to mock your socket.io server. Despite being a moleculer service, the sensor.service.js communicates with it via socket.io protocol.
It doesn't matter if your services use (or not) socket.io. All the services are declared in the same way, i.e., module.exports = {}
Below is a working example with socket.io.
const { ServiceBroker } = require("moleculer");
const ApiGateway = require("moleculer-web");
const SocketIOService = require("moleculer-io");
const io = require("socket.io-client");
const IOService = {
name: "api",
// SocketIOService should be after moleculer-web
// Load the HTTP API Gateway to be able to reach "greeter" action via:
// http://localhost:3000/hello/greeter
mixins: [ApiGateway, SocketIOService]
};
const HelloService = {
name: "hello",
actions: {
greeter() {
return "Hello Via Socket";
}
}
};
const broker = new ServiceBroker();
broker.createService(IOService);
broker.createService(HelloService);
broker.start().then(async () => {
const socket = io("http://localhost:3000", {
reconnectionDelay: 300,
reconnectionDelayMax: 300
});
socket.on("connect", () => {
console.log("Connection with the Gateway established");
});
socket.emit("call", "hello.greeter", (error, res) => {
console.log(res);
});
});
To make it work with moleculer-runner just copy the service declarations into my-service.service.js. So for example, your api.service.js could look like:
// api.service.js
module.exports = {
name: "api",
// SocketIOService should be after moleculer-web
// Load the HTTP API Gateway to be able to reach "greeter" action via:
// http://localhost:3000/hello/greeter
mixins: [ApiGateway, SocketIOService]
}
and your greeter service:
// greeter.service.js
module.exports = {
name: "hello",
actions: {
greeter() {
return "Hello Via Socket";
}
}
}
And run npm run dev or moleculer-runner --repl --hot services

How do you define a file response as a Message

I'm using proto to define a REST service
In my service, I'm trying to document that a service responds with a file.
I've looked through here https://github.com/protocolbuffers/protobuf/tree/master/src/google/protobuf but couldn't find anything that looked like a file.
service SomeService {
rpc GetStaticAsset(GetMessageRequest) returns (FileAsset) {
option (google.api.http) = {
get: "/static/{assetName}"
};
}
}
message FileAsset {
¯\_(ツ)_/¯
}
Found the answer here:
message Chunk {
bytes Content = 1;
}

Resources