ing
This commit is contained in:
parent
b0445e9ede
commit
7daa9dad65
11
external/external.go
vendored
11
external/external.go
vendored
|
@ -1,11 +0,0 @@
|
|||
package external
|
||||
|
||||
import "git.loafle.net/overflow/member_gateway_rest/external/grpc"
|
||||
|
||||
func InitPackage() {
|
||||
grpc.InitPackage()
|
||||
}
|
||||
|
||||
func DestroyPackage() {
|
||||
grpc.DestroyPackage()
|
||||
}
|
53
external/grpc/grpc.go
vendored
53
external/grpc/grpc.go
vendored
|
@ -1,53 +0,0 @@
|
|||
package grpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"git.loafle.net/commons/logging-go"
|
||||
oci "git.loafle.net/overflow/central_api/golang"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
var grpcClient oci.OverflowApiServerClient
|
||||
|
||||
func InitPackage() {
|
||||
conn, err := grpc.Dial("192.168.1.50:50006", grpc.WithInsecure())
|
||||
if nil != err {
|
||||
logging.Logger().Panic(err)
|
||||
}
|
||||
grpcClient = oci.NewOverflowApiServerClient(conn)
|
||||
|
||||
}
|
||||
|
||||
func DestroyPackage() {
|
||||
|
||||
}
|
||||
|
||||
var execMtx sync.RWMutex
|
||||
|
||||
func Exec(ctx context.Context, method string, params ...string) (string, error) {
|
||||
if nil == grpcClient {
|
||||
return "", fmt.Errorf("GRPC Client is not initialized")
|
||||
}
|
||||
|
||||
var err error
|
||||
|
||||
sm := strings.Split(method, ".")
|
||||
si := &oci.ServerInput{
|
||||
Target: sm[0],
|
||||
Method: sm[1],
|
||||
Params: params,
|
||||
}
|
||||
|
||||
execMtx.Lock()
|
||||
defer execMtx.Unlock()
|
||||
so, err := grpcClient.(oci.OverflowApiServerClient).Exec(ctx, si)
|
||||
if nil != err {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return so.Result, nil
|
||||
}
|
|
@ -5,3 +5,5 @@ import:
|
|||
- package: git.loafle.net/overflow/central_api
|
||||
- package: google.golang.org/grpc
|
||||
version: ^1.11.2
|
||||
- package: git.loafle.net/overflow/gateway_rest
|
||||
- package: git.loafle.net/overflow/gateway
|
||||
|
|
6
main.go
6
main.go
|
@ -9,7 +9,6 @@ import (
|
|||
"time"
|
||||
|
||||
"git.loafle.net/commons/logging-go"
|
||||
"git.loafle.net/overflow/member_gateway_rest/external"
|
||||
"git.loafle.net/overflow/member_gateway_rest/server"
|
||||
)
|
||||
|
||||
|
@ -20,11 +19,6 @@ func init() {
|
|||
func main() {
|
||||
s := server.NewServer()
|
||||
|
||||
external.InitPackage()
|
||||
defer func() {
|
||||
external.DestroyPackage()
|
||||
}()
|
||||
|
||||
go func() {
|
||||
err := s.ListenAndServe()
|
||||
if nil != err {
|
||||
|
|
|
@ -1,21 +1,13 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"git.loafle.net/commons/server-go"
|
||||
cswf "git.loafle.net/commons/server-go/web/fasthttp"
|
||||
cgrs "git.loafle.net/overflow/gateway_rest/server"
|
||||
)
|
||||
|
||||
type ServerHandler interface {
|
||||
cgrs.ServerHandler
|
||||
}
|
||||
|
||||
type ServerHandlers struct {
|
||||
cswf.ServerHandlers
|
||||
}
|
||||
|
||||
func (sh *ServerHandlers) Listener(serverCtx server.ServerCtx) (net.Listener, error) {
|
||||
l, err := net.Listen("tcp", "192.168.1.101:44449")
|
||||
if nil != err {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return l, nil
|
||||
cgrs.ServerHandlers
|
||||
}
|
||||
|
|
|
@ -18,7 +18,7 @@ func NewServer() *cswf.Server {
|
|||
logging.Logger().Error(err)
|
||||
}
|
||||
|
||||
webappS := &servlet.WebappServlet{}
|
||||
webappS := &servlet.WebappServlets{}
|
||||
webappS.ServerCodec = serverCodec
|
||||
webappS.RPCInvoker = rpcRegistry
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ import (
|
|||
"context"
|
||||
|
||||
"git.loafle.net/commons/server-go"
|
||||
"git.loafle.net/overflow/member_gateway_rest/external/grpc"
|
||||
"git.loafle.net/overflow/gateway/external/grpc"
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
|
|
|
@ -1,106 +0,0 @@
|
|||
package servlet
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
crp "git.loafle.net/commons/rpc-go/protocol"
|
||||
crr "git.loafle.net/commons/rpc-go/registry"
|
||||
"git.loafle.net/commons/server-go"
|
||||
csw "git.loafle.net/commons/server-go/web"
|
||||
cswf "git.loafle.net/commons/server-go/web/fasthttp"
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
type MethodMapping struct {
|
||||
Method string
|
||||
ParamKeys []string
|
||||
}
|
||||
|
||||
type RESTServlet struct {
|
||||
cswf.Servlets
|
||||
|
||||
ServerCodec crp.ServerCodec
|
||||
RPCInvoker crr.RPCInvoker
|
||||
|
||||
MethodMapping map[string]MethodMapping
|
||||
}
|
||||
|
||||
func (s *RESTServlet) Handle(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx) *csw.Error {
|
||||
method := string(ctx.Method())
|
||||
|
||||
switch method {
|
||||
case "GET":
|
||||
return s.HandleGet(servletCtx, ctx)
|
||||
case "POST":
|
||||
return s.HandlePost(servletCtx, ctx)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *RESTServlet) HandleGet(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx) *csw.Error {
|
||||
path := string(ctx.Path())
|
||||
if nil == s.MethodMapping {
|
||||
return csw.NewError(fasthttp.StatusNotFound, fmt.Errorf("Not Found for %s", path))
|
||||
}
|
||||
mapping, ok := s.MethodMapping[path]
|
||||
if !ok {
|
||||
return csw.NewError(fasthttp.StatusNotFound, fmt.Errorf("Not Found for %s", path))
|
||||
}
|
||||
|
||||
method := mapping.Method
|
||||
params := make([]string, 0)
|
||||
if nil != mapping.ParamKeys && 0 < len(mapping.ParamKeys) {
|
||||
qargs := ctx.QueryArgs()
|
||||
if nil == qargs {
|
||||
return csw.NewError(fasthttp.StatusBadRequest, fmt.Errorf("Parameter is not valied"))
|
||||
}
|
||||
|
||||
for _, k := range mapping.ParamKeys {
|
||||
buf := qargs.Peek(k)
|
||||
if nil == buf {
|
||||
return csw.NewError(fasthttp.StatusBadRequest, fmt.Errorf("Parameter for %s is not valied", k))
|
||||
}
|
||||
params = append(params, string(buf))
|
||||
}
|
||||
}
|
||||
|
||||
reqCodec, err := s.ServerCodec.NewRequestWithString(method, params, nil)
|
||||
if nil != err {
|
||||
return csw.NewError(fasthttp.StatusBadRequest, err)
|
||||
}
|
||||
|
||||
reply, err := s.RPCInvoker.Invoke(reqCodec, servletCtx, ctx)
|
||||
|
||||
buf, err := reqCodec.NewResponseWithString(reply.(string), err)
|
||||
if nil != err {
|
||||
return csw.NewError(fasthttp.StatusInternalServerError, err)
|
||||
}
|
||||
|
||||
ctx.SetBody(buf)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *RESTServlet) HandlePost(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx) *csw.Error {
|
||||
buf := ctx.PostBody()
|
||||
if nil == buf {
|
||||
return csw.NewError(fasthttp.StatusBadRequest, fmt.Errorf("Parameter is not valied"))
|
||||
}
|
||||
|
||||
reqCodec, err := s.ServerCodec.NewRequest(buf)
|
||||
if nil != err {
|
||||
return csw.NewError(fasthttp.StatusBadRequest, err)
|
||||
}
|
||||
|
||||
reply, err := s.RPCInvoker.Invoke(reqCodec, servletCtx, ctx)
|
||||
|
||||
buf, err = reqCodec.NewResponseWithString(reply.(string), err)
|
||||
if nil != err {
|
||||
return csw.NewError(fasthttp.StatusInternalServerError, err)
|
||||
}
|
||||
|
||||
ctx.SetBody(buf)
|
||||
|
||||
return nil
|
||||
}
|
|
@ -1,5 +1,13 @@
|
|||
package servlet
|
||||
|
||||
type WebappServlet struct {
|
||||
RESTServlet
|
||||
import (
|
||||
cgrs "git.loafle.net/overflow/gateway_rest/servlet"
|
||||
)
|
||||
|
||||
type WebappServlet interface {
|
||||
cgrs.RESTServlet
|
||||
}
|
||||
|
||||
type WebappServlets struct {
|
||||
cgrs.RESTServlets
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user