Skip to content

Commit

Permalink
feat grpc server (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
kooksee authored Jul 12, 2023
1 parent b9eec91 commit 5c62cfe
Show file tree
Hide file tree
Showing 18 changed files with 46 additions and 93 deletions.
File renamed without changes.
3 changes: 2 additions & 1 deletion clients/grpcc/request.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package grpcc

import (
"github.com/pubgo/lava/lava"
"google.golang.org/grpc"

"github.com/pubgo/lava/lava"
)

var _ lava.Request = (*request)(nil)
Expand Down
3 changes: 2 additions & 1 deletion clients/grpcc/response.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package grpcc

import (
"github.com/pubgo/lava/lava"
"google.golang.org/grpc"

"github.com/pubgo/lava/lava"
)

var _ lava.Response = (*response)(nil)
Expand Down
4 changes: 2 additions & 2 deletions cmds/builder/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"github.com/pubgo/lava/core/scheduler"
"github.com/pubgo/lava/internal/middlewares/middleware_accesslog"
"github.com/pubgo/lava/internal/middlewares/middleware_metric"
"github.com/pubgo/lava/pkg/cmds"
"github.com/pubgo/lava/pkg/cmdutil"
"github.com/pubgo/lava/services/metadata"

// debug
Expand Down Expand Up @@ -97,7 +97,7 @@ func Run(di *dix.Dix, cmdL ...*cli.Command) {
Name: version.Project(),
Suggest: true,
UseShortOptionHandling: true,
Usage: cmds.UsageDesc("%s service", version.Project()),
Usage: cmdutil.UsageDesc("%s service", version.Project()),
Version: version.Version(),
Flags: flags.GetFlags(),
Commands: cmdL,
Expand Down
4 changes: 2 additions & 2 deletions cmds/depcmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import (
"github.com/pubgo/funk/version"
cli "github.com/urfave/cli/v3"

"github.com/pubgo/lava/pkg/cmds"
"github.com/pubgo/lava/pkg/cmdutil"
)

func New(di *dix.Dix) *cli.Command {
return &cli.Command{
Name: "dep",
Usage: "Print the dependency package information",
Description: cmds.ExampleFmt(
Description: cmdutil.ExampleFmt(
"lava dep",
"lava dep json",
"lava dep t"),
Expand Down
4 changes: 2 additions & 2 deletions cmds/grpcservercmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"github.com/pubgo/funk/version"
"github.com/urfave/cli/v3"

"github.com/pubgo/lava/pkg/cmds"
"github.com/pubgo/lava/pkg/cmdutil"
"github.com/pubgo/lava/servers/grpcs"
)

func New(di *dix.Dix) *cli.Command {
return &cli.Command{
Name: "grpc",
Usage: cmds.UsageDesc("%s grpc service", version.Project()),
Usage: cmdutil.UsageDesc("%s grpc service", version.Project()),
Action: func(ctx *cli.Context) error {
defer recovery.Exit()
srv := dix.Inject(di, grpcs.New())
Expand Down
6 changes: 3 additions & 3 deletions cmds/healthcmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import (
"github.com/pubgo/funk/version"
"github.com/urfave/cli/v3"

"github.com/pubgo/lava/pkg/cmds"
"github.com/pubgo/lava/pkg/cmdutil"
"github.com/pubgo/lava/pkg/netutil"
)

func New() *cli.Command {
return &cli.Command{
Name: "health",
Usage: cmds.UsageDesc("%s health check", version.Project()),
Description: cmds.ExampleFmt(
Usage: cmdutil.UsageDesc("%s health check", version.Project()),
Description: cmdutil.ExampleFmt(
"lava health",
"lava health localhost:8080",
),
Expand Down
4 changes: 2 additions & 2 deletions cmds/httpservercmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"github.com/pubgo/funk/version"
"github.com/urfave/cli/v3"

"github.com/pubgo/lava/pkg/cmds"
"github.com/pubgo/lava/pkg/cmdutil"
"github.com/pubgo/lava/servers/https"
)

func New(di *dix.Dix) *cli.Command {
return &cli.Command{
Name: "http",
Usage: cmds.UsageDesc("%s http service", version.Project()),
Usage: cmdutil.UsageDesc("%s http service", version.Project()),
Action: func(ctx *cli.Context) error {
defer recovery.Exit()
srv := dix.Inject(di, https.New())
Expand Down
4 changes: 2 additions & 2 deletions cmds/versioncmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (
"github.com/pubgo/funk/version"
"github.com/urfave/cli/v3"

"github.com/pubgo/lava/pkg/cmds"
"github.com/pubgo/lava/pkg/cmdutil"
)

func New() *cli.Command {
return &cli.Command{
Name: "version",
Aliases: []string{"v"},
Usage: cmds.UsageDesc("%s version info", version.Project()),
Usage: cmdutil.UsageDesc("%s version info", version.Project()),
Action: func(ctx *cli.Context) error {
defer recovery.Exit()
fmt.Println("project:", version.Project())
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmds/cmd.go → pkg/cmdutil/cmd.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmds
package cmdutil

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmds/flag.go → pkg/cmdutil/flag.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmds
package cmdutil

import (
"flag"
Expand Down
5 changes: 3 additions & 2 deletions servers/grpcs/codec.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package grpcs

import (
encoding2 "github.com/pubgo/lava/core/encoding"
"google.golang.org/grpc/encoding"

codec "github.com/pubgo/lava/core/encoding"
)

func init() {
// 编码注册
encoding2.Each(func(_ string, cdc encoding2.Codec) {
codec.Each(func(_ string, cdc codec.Codec) {
encoding.RegisterCodec(cdc)
})
}
1 change: 1 addition & 0 deletions servers/grpcs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package grpcs

import (
"github.com/pubgo/funk/version"

"github.com/pubgo/lava/pkg/grpc_builder"
)

Expand Down
23 changes: 1 addition & 22 deletions servers/grpcs/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package grpcs
import (
"context"
"fmt"
"strings"
"time"

grpcMiddle "github.com/grpc-ecosystem/go-grpc-middleware"
Expand All @@ -24,23 +23,13 @@ import (
pbv1 "github.com/pubgo/lava/pkg/proto/lava"
)

func parsePath(path string) (string, string, string) {
parts := strings.Split(path, "/")
if len(parts) < 2 {
return "", "", ""
}
method := parts[len(parts)-1]
pkgService := parts[len(parts)-2]
prefix := strings.Join(parts[0:len(parts)-2], "/")
return prefix, pkgService, method
}

func handlerUnaryMiddle(middlewares map[string][]lava.Middleware) grpc.UnaryServerInterceptor {
unaryWrapper := func(ctx context.Context, req lava.Request) (rsp lava.Response, gErr error) {
dt, err := req.(*rpcRequest).handler(ctx, req.Payload())
if err != nil {
return nil, err
}

return &rpcResponse{header: new(fasthttp.ResponseHeader), dt: dt}, nil
}

Expand Down Expand Up @@ -279,13 +268,3 @@ func handlerStreamMiddle(middlewares map[string][]lava.Middleware) grpc.StreamSe
return grpc.SetTrailer(ctx, md)
}
}

// serviceFromMethod returns the service
// /service.Foo/Bar => service.Foo
func serviceFromMethod(m string) string {
if len(m) == 0 {
return m
}

return strings.Split(strings.Trim(m, "/"), "/")[0]
}
31 changes: 0 additions & 31 deletions servers/grpcs/request.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package grpcs

import (
"path/filepath"

"github.com/gofiber/fiber/v2"
"google.golang.org/grpc"

"github.com/pubgo/lava/lava"
Expand Down Expand Up @@ -33,31 +30,3 @@ func (r *rpcRequest) Service() string { return r.service }
func (r *rpcRequest) Operation() string { return r.method }
func (r *rpcRequest) Endpoint() string { return r.url }
func (r *rpcRequest) Stream() bool { return r.stream != nil }

var _ lava.Request = (*httpRequest)(nil)

type httpRequest struct {
ctx *fiber.Ctx
}

func (r *httpRequest) Kind() string { return "http" }
func (r *httpRequest) Operation() string {
_, srv, method := parsePath(r.ctx.OriginalURL())
return filepath.Join(srv, method)
}

func (r *httpRequest) Client() bool { return false }
func (r *httpRequest) Header() *lava.RequestHeader { return &r.ctx.Request().Header }
func (r *httpRequest) Payload() interface{} { return r.ctx.Body() }

func (r *httpRequest) ContentType() string {
return string(r.ctx.Request().Header.ContentType())
}

func (r *httpRequest) Service() string {
_, srv, _ := parsePath(r.ctx.OriginalURL())
return srv
}

func (r *httpRequest) Endpoint() string { return r.ctx.OriginalURL() }
func (r *httpRequest) Stream() bool { return false }
11 changes: 0 additions & 11 deletions servers/grpcs/response.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package grpcs

import (
"github.com/gofiber/fiber/v2"
"google.golang.org/grpc"

"github.com/pubgo/lava/lava"
Expand All @@ -18,13 +17,3 @@ type rpcResponse struct {
func (h *rpcResponse) Header() *lava.ResponseHeader { return h.header }
func (h *rpcResponse) Payload() interface{} { return h.dt }
func (h *rpcResponse) Stream() bool { return h.stream != nil }

var _ lava.Response = (*httpResponse)(nil)

type httpResponse struct {
ctx *fiber.Ctx
}

func (h *httpResponse) Header() *lava.ResponseHeader { return &h.ctx.Response().Header }
func (h *httpResponse) Payload() interface{} { return h.ctx.Response().Body() }
func (h *httpResponse) Stream() bool { return false }
19 changes: 9 additions & 10 deletions servers/grpcs/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import (
"context"
"errors"
"fmt"
"net"
"net/http"
"net/url"
"strings"

"github.com/fullstorydev/grpchan"
"github.com/fullstorydev/grpchan/inprocgrpc"
"github.com/gofiber/adaptor/v2"
Expand All @@ -15,12 +20,15 @@ import (
"github.com/pubgo/funk/async"
"github.com/pubgo/funk/config"
"github.com/pubgo/funk/log"
"github.com/pubgo/funk/pretty"
"github.com/pubgo/funk/recovery"
"github.com/pubgo/funk/running"
"github.com/pubgo/funk/stack"
"github.com/pubgo/funk/vars"
"github.com/pubgo/funk/version"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
"google.golang.org/grpc"

"github.com/pubgo/lava/core/debug"
"github.com/pubgo/lava/core/lifecycle"
"github.com/pubgo/lava/core/metrics"
Expand All @@ -33,13 +41,6 @@ import (
"github.com/pubgo/lava/internal/middlewares/middleware_service_info"
"github.com/pubgo/lava/lava"
"github.com/pubgo/lava/pkg/grpcutil"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
"google.golang.org/grpc"
"net"
"net/http"
"net/url"
"strings"
)

func New() lava.Service { return newService() }
Expand Down Expand Up @@ -141,8 +142,6 @@ func (s *serviceImpl) DixInject(
}
}

pretty.Println(mux)

s.cc.WithServerUnaryInterceptor(handlerUnaryMiddle(srvMidMap))
s.cc.WithServerStreamInterceptor(handlerStreamMiddle(srvMidMap))

Expand Down
13 changes: 13 additions & 0 deletions servers/grpcs/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package grpcs

import "strings"

// serviceFromMethod returns the service
// /service.Foo/Bar => service.Foo
func serviceFromMethod(m string) string {
if len(m) == 0 {
return m
}

return strings.Split(strings.Trim(m, "/"), "/")[0]
}

0 comments on commit 5c62cfe

Please sign in to comment.