add goa examples
This commit is contained in:
109
go/goa_example/gen/grpc/service1/server/encode_decode.go
Normal file
109
go/goa_example/gen/grpc/service1/server/encode_decode.go
Normal file
@@ -0,0 +1,109 @@
|
||||
// Code generated by goa v3.6.0, DO NOT EDIT.
|
||||
//
|
||||
// Service1 gRPC server encoders and decoders
|
||||
//
|
||||
// Command:
|
||||
// $ goa gen goa_example/design
|
||||
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
service1pb "goa_example/gen/grpc/service1/pb"
|
||||
service1 "goa_example/gen/service1"
|
||||
"strings"
|
||||
|
||||
goagrpc "goa.design/goa/v3/grpc"
|
||||
goa "goa.design/goa/v3/pkg"
|
||||
"google.golang.org/grpc/metadata"
|
||||
)
|
||||
|
||||
// EncodeSigninResponse encodes responses from the "Service1" service "signin"
|
||||
// endpoint.
|
||||
func EncodeSigninResponse(ctx context.Context, v interface{}, hdr, trlr *metadata.MD) (interface{}, error) {
|
||||
result, ok := v.(*service1.Creds)
|
||||
if !ok {
|
||||
return nil, goagrpc.ErrInvalidType("Service1", "signin", "*service1.Creds", v)
|
||||
}
|
||||
resp := NewSigninResponse(result)
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// DecodeSigninRequest decodes requests sent to "Service1" service "signin"
|
||||
// endpoint.
|
||||
func DecodeSigninRequest(ctx context.Context, v interface{}, md metadata.MD) (interface{}, error) {
|
||||
var (
|
||||
username string
|
||||
password string
|
||||
err error
|
||||
)
|
||||
{
|
||||
if vals := md.Get("username"); len(vals) == 0 {
|
||||
err = goa.MergeErrors(err, goa.MissingFieldError("username", "metadata"))
|
||||
} else {
|
||||
username = vals[0]
|
||||
}
|
||||
if vals := md.Get("password"); len(vals) == 0 {
|
||||
err = goa.MergeErrors(err, goa.MissingFieldError("password", "metadata"))
|
||||
} else {
|
||||
password = vals[0]
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var payload *service1.SigninPayload
|
||||
{
|
||||
payload = NewSigninPayload(username, password)
|
||||
}
|
||||
return payload, nil
|
||||
}
|
||||
|
||||
// EncodeSecureResponse encodes responses from the "Service1" service "secure"
|
||||
// endpoint.
|
||||
func EncodeSecureResponse(ctx context.Context, v interface{}, hdr, trlr *metadata.MD) (interface{}, error) {
|
||||
result, ok := v.(string)
|
||||
if !ok {
|
||||
return nil, goagrpc.ErrInvalidType("Service1", "secure", "string", v)
|
||||
}
|
||||
resp := NewSecureResponse(result)
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// DecodeSecureRequest decodes requests sent to "Service1" service "secure"
|
||||
// endpoint.
|
||||
func DecodeSecureRequest(ctx context.Context, v interface{}, md metadata.MD) (interface{}, error) {
|
||||
var (
|
||||
token string
|
||||
err error
|
||||
)
|
||||
{
|
||||
if vals := md.Get("authorization"); len(vals) == 0 {
|
||||
err = goa.MergeErrors(err, goa.MissingFieldError("authorization", "metadata"))
|
||||
} else {
|
||||
token = vals[0]
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var (
|
||||
message *service1pb.SecureRequest
|
||||
ok bool
|
||||
)
|
||||
{
|
||||
if message, ok = v.(*service1pb.SecureRequest); !ok {
|
||||
return nil, goagrpc.ErrInvalidType("Service1", "secure", "*service1pb.SecureRequest", v)
|
||||
}
|
||||
}
|
||||
var payload *service1.SecurePayload
|
||||
{
|
||||
payload = NewSecurePayload(message, token)
|
||||
if strings.Contains(payload.Token, " ") {
|
||||
// Remove authorization scheme prefix (e.g. "Bearer")
|
||||
cred := strings.SplitN(payload.Token, " ", 2)[1]
|
||||
payload.Token = cred
|
||||
}
|
||||
}
|
||||
return payload, nil
|
||||
}
|
||||
94
go/goa_example/gen/grpc/service1/server/server.go
Normal file
94
go/goa_example/gen/grpc/service1/server/server.go
Normal file
@@ -0,0 +1,94 @@
|
||||
// Code generated by goa v3.6.0, DO NOT EDIT.
|
||||
//
|
||||
// Service1 gRPC server
|
||||
//
|
||||
// Command:
|
||||
// $ goa gen goa_example/design
|
||||
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
service1pb "goa_example/gen/grpc/service1/pb"
|
||||
service1 "goa_example/gen/service1"
|
||||
|
||||
goagrpc "goa.design/goa/v3/grpc"
|
||||
goa "goa.design/goa/v3/pkg"
|
||||
"google.golang.org/grpc/codes"
|
||||
)
|
||||
|
||||
// Server implements the service1pb.Service1Server interface.
|
||||
type Server struct {
|
||||
SigninH goagrpc.UnaryHandler
|
||||
SecureH goagrpc.UnaryHandler
|
||||
service1pb.UnimplementedService1Server
|
||||
}
|
||||
|
||||
// ErrorNamer is an interface implemented by generated error structs that
|
||||
// exposes the name of the error as defined in the expr.
|
||||
type ErrorNamer interface {
|
||||
ErrorName() string
|
||||
}
|
||||
|
||||
// New instantiates the server struct with the Service1 service endpoints.
|
||||
func New(e *service1.Endpoints, uh goagrpc.UnaryHandler) *Server {
|
||||
return &Server{
|
||||
SigninH: NewSigninHandler(e.Signin, uh),
|
||||
SecureH: NewSecureHandler(e.Secure, uh),
|
||||
}
|
||||
}
|
||||
|
||||
// NewSigninHandler creates a gRPC handler which serves the "Service1" service
|
||||
// "signin" endpoint.
|
||||
func NewSigninHandler(endpoint goa.Endpoint, h goagrpc.UnaryHandler) goagrpc.UnaryHandler {
|
||||
if h == nil {
|
||||
h = goagrpc.NewUnaryHandler(endpoint, DecodeSigninRequest, EncodeSigninResponse)
|
||||
}
|
||||
return h
|
||||
}
|
||||
|
||||
// Signin implements the "Signin" method in service1pb.Service1Server interface.
|
||||
func (s *Server) Signin(ctx context.Context, message *service1pb.SigninRequest) (*service1pb.SigninResponse, error) {
|
||||
ctx = context.WithValue(ctx, goa.MethodKey, "signin")
|
||||
ctx = context.WithValue(ctx, goa.ServiceKey, "Service1")
|
||||
resp, err := s.SigninH.Handle(ctx, message)
|
||||
if err != nil {
|
||||
var en ErrorNamer
|
||||
if errors.As(err, &en) {
|
||||
switch en.ErrorName() {
|
||||
case "unauthorized":
|
||||
return nil, goagrpc.NewStatusError(codes.Unauthenticated, err, goagrpc.NewErrorResponse(err))
|
||||
}
|
||||
}
|
||||
return nil, goagrpc.EncodeError(err)
|
||||
}
|
||||
return resp.(*service1pb.SigninResponse), nil
|
||||
}
|
||||
|
||||
// NewSecureHandler creates a gRPC handler which serves the "Service1" service
|
||||
// "secure" endpoint.
|
||||
func NewSecureHandler(endpoint goa.Endpoint, h goagrpc.UnaryHandler) goagrpc.UnaryHandler {
|
||||
if h == nil {
|
||||
h = goagrpc.NewUnaryHandler(endpoint, DecodeSecureRequest, EncodeSecureResponse)
|
||||
}
|
||||
return h
|
||||
}
|
||||
|
||||
// Secure implements the "Secure" method in service1pb.Service1Server interface.
|
||||
func (s *Server) Secure(ctx context.Context, message *service1pb.SecureRequest) (*service1pb.SecureResponse, error) {
|
||||
ctx = context.WithValue(ctx, goa.MethodKey, "secure")
|
||||
ctx = context.WithValue(ctx, goa.ServiceKey, "Service1")
|
||||
resp, err := s.SecureH.Handle(ctx, message)
|
||||
if err != nil {
|
||||
var en ErrorNamer
|
||||
if errors.As(err, &en) {
|
||||
switch en.ErrorName() {
|
||||
case "unauthorized":
|
||||
return nil, goagrpc.NewStatusError(codes.Unauthenticated, err, goagrpc.NewErrorResponse(err))
|
||||
}
|
||||
}
|
||||
return nil, goagrpc.EncodeError(err)
|
||||
}
|
||||
return resp.(*service1pb.SecureResponse), nil
|
||||
}
|
||||
51
go/goa_example/gen/grpc/service1/server/types.go
Normal file
51
go/goa_example/gen/grpc/service1/server/types.go
Normal file
@@ -0,0 +1,51 @@
|
||||
// Code generated by goa v3.6.0, DO NOT EDIT.
|
||||
//
|
||||
// Service1 gRPC server types
|
||||
//
|
||||
// Command:
|
||||
// $ goa gen goa_example/design
|
||||
|
||||
package server
|
||||
|
||||
import (
|
||||
service1pb "goa_example/gen/grpc/service1/pb"
|
||||
service1 "goa_example/gen/service1"
|
||||
)
|
||||
|
||||
// NewSigninPayload builds the payload of the "signin" endpoint of the
|
||||
// "Service1" service from the gRPC request type.
|
||||
func NewSigninPayload(username string, password string) *service1.SigninPayload {
|
||||
v := &service1.SigninPayload{}
|
||||
v.Username = username
|
||||
v.Password = password
|
||||
return v
|
||||
}
|
||||
|
||||
// NewSigninResponse builds the gRPC response type from the result of the
|
||||
// "signin" endpoint of the "Service1" service.
|
||||
func NewSigninResponse(result *service1.Creds) *service1pb.SigninResponse {
|
||||
message := &service1pb.SigninResponse{
|
||||
Jwt: result.JWT,
|
||||
ApiKey: result.APIKey,
|
||||
OauthToken: result.OauthToken,
|
||||
}
|
||||
return message
|
||||
}
|
||||
|
||||
// NewSecurePayload builds the payload of the "secure" endpoint of the
|
||||
// "Service1" service from the gRPC request type.
|
||||
func NewSecurePayload(message *service1pb.SecureRequest, token string) *service1.SecurePayload {
|
||||
v := &service1.SecurePayload{
|
||||
Fail: &message.Fail,
|
||||
}
|
||||
v.Token = token
|
||||
return v
|
||||
}
|
||||
|
||||
// NewSecureResponse builds the gRPC response type from the result of the
|
||||
// "secure" endpoint of the "Service1" service.
|
||||
func NewSecureResponse(result string) *service1pb.SecureResponse {
|
||||
message := &service1pb.SecureResponse{}
|
||||
message.Field = result
|
||||
return message
|
||||
}
|
||||
Reference in New Issue
Block a user