跳到主要内容

API plugin: register your own API

This is a demo to show you how to register your own API.

Layotto has the api-plugin feature to let you add your own API based on your need.

step 1. start Layotto with a new helloworld API

Change directory:

# change directory 
cd ${project_path}/cmd/layotto_multiple_api

Build Layotto :

# build it
go build -o layotto

Run Layotto:

./layotto start -c ../../configs/config_standalone.json

Q: What happened?

Check the code in main.go and you will find a new API was registered during startup:

		// register your grpc API here
runtime.WithGrpcAPI(
// default grpc API
default_api.NewGrpcAPI,
// a demo to show how to register your own API
helloworld_api.NewHelloWorldAPI,
),

step 2. invoke the helloworld API

# change directory 
cd ${project_path}/cmd/layotto_multiple_api
# run demo client
go run client/main.go

The result will be:

Greeting: Hello world

This message is the response of the helloworld API you just registered in step 1.

Next

You can refer to the demo code to implement your own API. Have a try !

For more details,you can refer to the design doc