Skip to content

Commit

Permalink
docs: add go-sdk reference and supporting code cases. (#906)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyb0225 committed Jun 24, 2023
1 parent 87af334 commit 7738192
Show file tree
Hide file tree
Showing 8 changed files with 869 additions and 1,617 deletions.
6 changes: 6 additions & 0 deletions configs/config_standalone.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@
"metadata": {}
}
},
"file": {
"file_demo": {
"type": "local",
"metadata": {}
}
},
"secret_store": {
"secret_demo": {
"type": "local.file",
Expand Down
104 changes: 104 additions & 0 deletions demo/file/local/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Copyright 2021 Layotto Authors
// Licensed 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 (
"context"
"flag"
"fmt"
"os"
"strconv"

"google.golang.org/grpc"

runtimev1pb "mosn.io/layotto/spec/proto/runtime/v1"
)

var (
storeName string
FileName = "file"
FileValue = "value"
)

func init() {
flag.StringVar(&storeName, "s", "", "set `storeName`")
}

func main() {
flag.Parse()
if storeName == "" {
panic("storeName is empty.")
}

// conn to layotto grpc server
conn, err := grpc.Dial("127.0.0.1:34904", grpc.WithInsecure())
if err != nil {
fmt.Printf("conn build failed,err:%+v", err)
return
}

c := runtimev1pb.NewRuntimeClient(conn)

// Set metadata, the specific configuration needs to see what configuration is required
// to put the relevant source code in the local file.

// Create metadata and a new client
meta := make(map[string]string)
meta["FileMode"] = "0777"
meta["FileFlag"] = strconv.Itoa(os.O_CREATE | os.O_RDWR)

// Make a request to put a file
// storeName is the name of file instance.
req := &runtimev1pb.PutFileRequest{StoreName: storeName, Name: FileName, Metadata: meta}
stream, err := c.PutFile(context.Background())
if err != nil {
fmt.Printf("put file failed:%+v", err)
panic(err)
}
req.Data = []byte(FileValue)
_ = stream.Send(req)
_, err = stream.CloseAndRecv()
if err != nil {
fmt.Printf("cannot receive response: %+v", err)
panic(err)
}

// Get File
file, err := c.GetFile(context.Background(), &runtimev1pb.GetFileRequest{StoreName: storeName, Name: FileName, Metadata: nil})
if err != nil {
fmt.Printf("get file failed: %+v", err)
panic(err)
}

// Receive data from the server and store in pic
pic := make([]byte, 0)
for {
resp, err := file.Recv()
if err != nil {
if err.Error() != "EOF" {
fmt.Println("recv file failed")
panic(err)
}
break
}
pic = append(pic, resp.Data...)
}

if string(pic) != FileValue {
fmt.Printf("the file is not the same as the value we put before. %s", string(pic))
return
}

fmt.Println("test file operate success")
}
2 changes: 1 addition & 1 deletion demo/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ require (
google.golang.org/protobuf v1.27.1
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
mosn.io/layotto v0.5.0 // indirect
mosn.io/layotto/sdk/go-sdk v0.0.0-20211020084508-6f5ee3cfeba0
mosn.io/layotto/spec v0.0.0-20220413092851-55c58dbb1a23
)

replace (
mosn.io/layotto/sdk/go-sdk => ../sdk/go-sdk
mosn.io/layotto/spec => ../spec

)
1,614 changes: 0 additions & 1,614 deletions demo/go.sum

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions demo/hello/common/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
You can run this demo with different component names.
For example:

```go
go build -o client
./client -s "helloworld"
```
52 changes: 52 additions & 0 deletions demo/hello/common/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2021 Layotto Authors
// Licensed 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 (
"context"
"flag"
"fmt"
"log"

"mosn.io/layotto/sdk/go-sdk/client"
)

var storeName string

func init() {
flag.StringVar(&storeName, "s", "", "set `storeName`")
}

func main() {
flag.Parse()
if storeName == "" {
panic("storeName is empty.")
}

cli, err := client.NewClient()
if err != nil {
log.Fatal(err)
}

defer cli.Close()

res, err := cli.SayHello(context.Background(), &client.SayHelloRequest{
ServiceName: storeName,
})
if err != nil {
log.Fatal(err)
}

fmt.Println(res.Hello)
}
Loading

0 comments on commit 7738192

Please sign in to comment.