Skip to content

Commit

Permalink
fix(clients): fix clients names (#6)
Browse files Browse the repository at this point in the history
* fix(clients): fix clients and load test
  • Loading branch information
GStones committed Mar 19, 2024
1 parent 3dc5209 commit d29eeed
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 23 deletions.
27 changes: 19 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
# Demo
# Moke Project Template
[![Go Report Card](https://goreportcard.com/badge/github.com/gstones/moke-layout)](https://goreportcard.com/report/github.com/gstones/moke-layout)
[![Go Reference](https://pkg.go.dev/badge/github.com/GStones/moke-layout.svg)](https://pkg.go.dev/github.com/GStones/moke-layout)
[![Release](https://img.shields.io/github/v/release/gstones/moke-layout.svg?style=flat-square)](https://github.com/GStones/moke-layout)

## How to run
## Create a service
```shell
# install gonew
go install golang.org/x/tools/cmd/gonew@latest
# create a new project
gonew github.com/gstones/moke-layout your.domain/myprog
```

## How to run
* deploy infrastructure:
* redis:6379
* nats:4222
* mongo:27017

```shell
# first need fix .env file
docker compose -f ./deployment/docker-compose/infrastructure.yaml up -d
```

* service:
* run service:
```shell
go run cmd/demo/service/main.go
```
Expand All @@ -30,7 +36,12 @@
./client.exe tcp
```
tips: http client use Postman to connect `localhost:8081`.

* run load test:
* install [k6](https://grafana.com/docs/k6/latest/get-started/installation/)
* run
``` shell
k6 run ./tests/demo/demo.js
```
* proto generate
* install [buf](https://buf.build/docs/installation)
```shell
Expand Down
2 changes: 1 addition & 1 deletion internal/clients/demo/grpc_cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type DemoGrpc struct {
cmd *ishell.Cmd
}

func NewDemoGrpc(conn *grpc.ClientConn) *DemoGrpc {
func NewDemoGrpcCli(conn *grpc.ClientConn) *DemoGrpc {
cmd := &ishell.Cmd{
Name: "demo",
Help: "demo interactive",
Expand Down
6 changes: 3 additions & 3 deletions internal/clients/demo/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func RunGrpc(url string) {
if conn, err := tools.DialInsecure(url); err != nil {
slogger.Die(sh, err)
} else {
demoGrpc := NewDemoGrpc(conn)
demoGrpc := NewDemoGrpcCli(conn)
sh.AddCmd(demoGrpc.GetCmd())

sh.Interrupt(func(c *ishell.Context, count int, input string) {
Expand All @@ -38,8 +38,8 @@ func RunTcp(url string) {
if conn, err := net.Dial("tcp", url); err != nil {
slogger.Die(sh, err)
} else {
demoZinx := NewZinxDemo(conn)
sh.AddCmd(demoZinx.GetCmd())
demoTcp := NewTcpCli(conn)
sh.AddCmd(demoTcp.GetCmd())

sh.Interrupt(func(c *ishell.Context, count int, input string) {
if count >= 2 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,30 @@ import (
pb "github.com/gstones/moke-layout/api/gen/demo/api"
)

type DemoZinx struct {
type DemoTcp struct {
conn net.Conn
cmd *ishell.Cmd
}

func NewZinxDemo(conn net.Conn) *DemoZinx {
func NewTcpCli(conn net.Conn) *DemoTcp {
cmd := &ishell.Cmd{
Name: "demo",
Help: "demo interactive",
Aliases: []string{"D"},
}
p := &DemoZinx{
p := &DemoTcp{
conn: conn,
cmd: cmd,
}
p.initSubShells()
return p
}

func (dz *DemoZinx) GetCmd() *ishell.Cmd {
func (dz *DemoTcp) GetCmd() *ishell.Cmd {
return dz.cmd
}

func (dz *DemoZinx) initSubShells() {
func (dz *DemoTcp) initSubShells() {
dz.cmd.AddCmd(&ishell.Cmd{
Name: "hi",
Help: "say hi",
Expand All @@ -54,7 +54,7 @@ func (dz *DemoZinx) initSubShells() {

}

func (dz *DemoZinx) sayHi(c *ishell.Context) {
func (dz *DemoTcp) sayHi(c *ishell.Context) {
c.ShowPrompt(false)
defer c.ShowPrompt(true)

Expand All @@ -78,7 +78,7 @@ func (dz *DemoZinx) sayHi(c *ishell.Context) {
}
}

func (dz *DemoZinx) watch(c *ishell.Context) {
func (dz *DemoTcp) watch(c *ishell.Context) {
c.ShowPrompt(false)
defer c.ShowPrompt(true)

Expand Down
40 changes: 40 additions & 0 deletions pkg/dfx/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package dfx

import (
"errors"
"strings"

"go.uber.org/fx"
"go.uber.org/zap"

"github.com/gstones/moke-kit/server/pkg/sfx"
)

// Author is a demo auth service
type Author struct {
}

// Auth is a demo auth method
func (d *Author) Auth(token string) (string, error) {
if !strings.Contains(token, "test") {
return "", errors.New("token error")
}
return token, nil
}

// AuthModule is a demo auth module
// you can implement your own auth service or auth function
// Auth will check every rpc/http request,
// if you want to disable it for a service, add `utility.WithoutAuth` in struct of your service
//
// type service struct {
// utility.WithoutAuth
// }
var AuthModule = fx.Provide(
func(
l *zap.Logger,
) (out sfx.AuthServiceResult, err error) {
out.AuthService = &Author{}
return
},
)
3 changes: 3 additions & 0 deletions pkg/modules/demo_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
var GrpcModule = fx.Module("grpcService",
dfx.SqliteDriverModule,
dfx.SettingsModule,
dfx.AuthModule,
demo.GrpcModule,
)

Expand All @@ -20,6 +21,7 @@ var GrpcModule = fx.Module("grpcService",
var HttpModule = fx.Module("httpService",
dfx.SettingsModule,
dfx.SqliteDriverModule,
dfx.AuthModule,
demo.GrpcModule,
demo.GatewayModule,
)
Expand All @@ -35,6 +37,7 @@ var TcpModule = fx.Module("tcpService",
// AllModule all service module
// can inject it to any fxmain.Main(), if you want to start demo all type services
var AllModule = fx.Module("allService",
dfx.AuthModule,
dfx.SettingsModule,
dfx.SqliteDriverModule,
demo.GrpcModule,
Expand Down
10 changes: 10 additions & 0 deletions tests/common/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const makeParams = (token) => {
return {
metadata: {
'x-my-header': 'k6test',
'x-my-header-bin': new Uint8Array([1, 2, 3]),
'authorization': "bearer " + token,
},
tags: {k6test: 'yes'},
};
}
27 changes: 27 additions & 0 deletions tests/demo/demo-k6.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
syntax = "proto3";

package demo.pb;
option go_package = "demo/api;pb";

service DemoService {
rpc Hi (HiRequest) returns (HiResponse) {};
rpc Watch(WatchRequest) returns (stream WatchResponse) {};
}

message HiRequest {
string uid = 1;
string message = 2;
}

message HiResponse {
string message = 1;
}

message WatchRequest {
string uid = 1;
string topic = 2;
}

message WatchResponse {
string message = 1;
}
9 changes: 5 additions & 4 deletions tests/demo/demo.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {Client, StatusOK} from 'k6/net/grpc';
import {check, sleep} from 'k6';

import {makeParams} from "../common/common.js";

const client = new Client();
client.load(['../../api/demo'], 'demo.proto');
client.load(['./'], 'demo-k6.proto');

const GRPC_ADDR = __ENV.SERVER_HOST || '127.0.0.1:8081';

Expand All @@ -15,8 +15,9 @@ export default function () {
uid: 'test',
message: 'hello',
};

let response = client.invoke('demo.pb.DemoService/Hi', data);
const params = makeParams("test");
let response = client.invoke('demo.pb.DemoService/Hi', data,params);
console.log(response);
check(response, {
'status is OK': (r) => r && r.status === StatusOK,
});
Expand Down

0 comments on commit d29eeed

Please sign in to comment.