add goa examples
This commit is contained in:
172
go/goa_example/gen/grpc/cli/host/cli.go
Normal file
172
go/goa_example/gen/grpc/cli/host/cli.go
Normal file
@@ -0,0 +1,172 @@
|
||||
// Code generated by goa v3.6.0, DO NOT EDIT.
|
||||
//
|
||||
// host gRPC client CLI support package
|
||||
//
|
||||
// Command:
|
||||
// $ goa gen goa_example/design
|
||||
|
||||
package cli
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
service1c "goa_example/gen/grpc/service1/client"
|
||||
"os"
|
||||
|
||||
goa "goa.design/goa/v3/pkg"
|
||||
grpc "google.golang.org/grpc"
|
||||
)
|
||||
|
||||
// UsageCommands returns the set of commands and sub-commands using the format
|
||||
//
|
||||
// command (subcommand1|subcommand2|...)
|
||||
//
|
||||
func UsageCommands() string {
|
||||
return `service1 (signin|secure)
|
||||
`
|
||||
}
|
||||
|
||||
// UsageExamples produces an example of a valid invocation of the CLI tool.
|
||||
func UsageExamples() string {
|
||||
return os.Args[0] + ` service1 signin --username "user" --password "password"` + "\n" +
|
||||
""
|
||||
}
|
||||
|
||||
// ParseEndpoint returns the endpoint and payload as specified on the command
|
||||
// line.
|
||||
func ParseEndpoint(cc *grpc.ClientConn, opts ...grpc.CallOption) (goa.Endpoint, interface{}, error) {
|
||||
var (
|
||||
service1Flags = flag.NewFlagSet("service1", flag.ContinueOnError)
|
||||
|
||||
service1SigninFlags = flag.NewFlagSet("signin", flag.ExitOnError)
|
||||
service1SigninUsernameFlag = service1SigninFlags.String("username", "REQUIRED", "")
|
||||
service1SigninPasswordFlag = service1SigninFlags.String("password", "REQUIRED", "")
|
||||
|
||||
service1SecureFlags = flag.NewFlagSet("secure", flag.ExitOnError)
|
||||
service1SecureMessageFlag = service1SecureFlags.String("message", "", "")
|
||||
service1SecureTokenFlag = service1SecureFlags.String("token", "REQUIRED", "")
|
||||
)
|
||||
service1Flags.Usage = service1Usage
|
||||
service1SigninFlags.Usage = service1SigninUsage
|
||||
service1SecureFlags.Usage = service1SecureUsage
|
||||
|
||||
if err := flag.CommandLine.Parse(os.Args[1:]); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
if flag.NArg() < 2 { // two non flag args are required: SERVICE and ENDPOINT (aka COMMAND)
|
||||
return nil, nil, fmt.Errorf("not enough arguments")
|
||||
}
|
||||
|
||||
var (
|
||||
svcn string
|
||||
svcf *flag.FlagSet
|
||||
)
|
||||
{
|
||||
svcn = flag.Arg(0)
|
||||
switch svcn {
|
||||
case "service1":
|
||||
svcf = service1Flags
|
||||
default:
|
||||
return nil, nil, fmt.Errorf("unknown service %q", svcn)
|
||||
}
|
||||
}
|
||||
if err := svcf.Parse(flag.Args()[1:]); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
var (
|
||||
epn string
|
||||
epf *flag.FlagSet
|
||||
)
|
||||
{
|
||||
epn = svcf.Arg(0)
|
||||
switch svcn {
|
||||
case "service1":
|
||||
switch epn {
|
||||
case "signin":
|
||||
epf = service1SigninFlags
|
||||
|
||||
case "secure":
|
||||
epf = service1SecureFlags
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
if epf == nil {
|
||||
return nil, nil, fmt.Errorf("unknown %q endpoint %q", svcn, epn)
|
||||
}
|
||||
|
||||
// Parse endpoint flags if any
|
||||
if svcf.NArg() > 1 {
|
||||
if err := epf.Parse(svcf.Args()[1:]); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
data interface{}
|
||||
endpoint goa.Endpoint
|
||||
err error
|
||||
)
|
||||
{
|
||||
switch svcn {
|
||||
case "service1":
|
||||
c := service1c.NewClient(cc, opts...)
|
||||
switch epn {
|
||||
case "signin":
|
||||
endpoint = c.Signin()
|
||||
data, err = service1c.BuildSigninPayload(*service1SigninUsernameFlag, *service1SigninPasswordFlag)
|
||||
case "secure":
|
||||
endpoint = c.Secure()
|
||||
data, err = service1c.BuildSecurePayload(*service1SecureMessageFlag, *service1SecureTokenFlag)
|
||||
}
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return endpoint, data, nil
|
||||
}
|
||||
|
||||
// service1Usage displays the usage of the service1 command and its subcommands.
|
||||
func service1Usage() {
|
||||
fmt.Fprintf(os.Stderr, `The secured service exposes endpoints that require valid authorization credentials.
|
||||
Usage:
|
||||
%[1]s [globalflags] service1 COMMAND [flags]
|
||||
|
||||
COMMAND:
|
||||
signin: Signin implements signin.
|
||||
secure: 这是一个需要JWT认证的接口
|
||||
|
||||
Additional help:
|
||||
%[1]s service1 COMMAND --help
|
||||
`, os.Args[0])
|
||||
}
|
||||
func service1SigninUsage() {
|
||||
fmt.Fprintf(os.Stderr, `%[1]s [flags] service1 signin -username STRING -password STRING
|
||||
|
||||
Signin implements signin.
|
||||
-username STRING:
|
||||
-password STRING:
|
||||
|
||||
Example:
|
||||
%[1]s service1 signin --username "user" --password "password"
|
||||
`, os.Args[0])
|
||||
}
|
||||
|
||||
func service1SecureUsage() {
|
||||
fmt.Fprintf(os.Stderr, `%[1]s [flags] service1 secure -message JSON -token STRING
|
||||
|
||||
这是一个需要JWT认证的接口
|
||||
-message JSON:
|
||||
-token STRING:
|
||||
|
||||
Example:
|
||||
%[1]s service1 secure --message '{
|
||||
"fail": true
|
||||
}' --token "Quia omnis amet et rerum quis."
|
||||
`, os.Args[0])
|
||||
}
|
||||
58
go/goa_example/gen/grpc/service1/client/cli.go
Normal file
58
go/goa_example/gen/grpc/service1/client/cli.go
Normal file
@@ -0,0 +1,58 @@
|
||||
// Code generated by goa v3.6.0, DO NOT EDIT.
|
||||
//
|
||||
// Service1 gRPC client CLI support package
|
||||
//
|
||||
// Command:
|
||||
// $ goa gen goa_example/design
|
||||
|
||||
package client
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
service1pb "goa_example/gen/grpc/service1/pb"
|
||||
service1 "goa_example/gen/service1"
|
||||
)
|
||||
|
||||
// BuildSigninPayload builds the payload for the Service1 signin endpoint from
|
||||
// CLI flags.
|
||||
func BuildSigninPayload(service1SigninUsername string, service1SigninPassword string) (*service1.SigninPayload, error) {
|
||||
var username string
|
||||
{
|
||||
username = service1SigninUsername
|
||||
}
|
||||
var password string
|
||||
{
|
||||
password = service1SigninPassword
|
||||
}
|
||||
v := &service1.SigninPayload{}
|
||||
v.Username = username
|
||||
v.Password = password
|
||||
|
||||
return v, nil
|
||||
}
|
||||
|
||||
// BuildSecurePayload builds the payload for the Service1 secure endpoint from
|
||||
// CLI flags.
|
||||
func BuildSecurePayload(service1SecureMessage string, service1SecureToken string) (*service1.SecurePayload, error) {
|
||||
var err error
|
||||
var message service1pb.SecureRequest
|
||||
{
|
||||
if service1SecureMessage != "" {
|
||||
err = json.Unmarshal([]byte(service1SecureMessage), &message)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid JSON for message, \nerror: %s, \nexample of valid JSON:\n%s", err, "'{\n \"fail\": true\n }'")
|
||||
}
|
||||
}
|
||||
}
|
||||
var token string
|
||||
{
|
||||
token = service1SecureToken
|
||||
}
|
||||
v := &service1.SecurePayload{
|
||||
Fail: &message.Fail,
|
||||
}
|
||||
v.Token = token
|
||||
|
||||
return v, nil
|
||||
}
|
||||
74
go/goa_example/gen/grpc/service1/client/client.go
Normal file
74
go/goa_example/gen/grpc/service1/client/client.go
Normal file
@@ -0,0 +1,74 @@
|
||||
// Code generated by goa v3.6.0, DO NOT EDIT.
|
||||
//
|
||||
// Service1 gRPC client
|
||||
//
|
||||
// Command:
|
||||
// $ goa gen goa_example/design
|
||||
|
||||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
service1pb "goa_example/gen/grpc/service1/pb"
|
||||
|
||||
goagrpc "goa.design/goa/v3/grpc"
|
||||
goapb "goa.design/goa/v3/grpc/pb"
|
||||
goa "goa.design/goa/v3/pkg"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
// Client lists the service endpoint gRPC clients.
|
||||
type Client struct {
|
||||
grpccli service1pb.Service1Client
|
||||
opts []grpc.CallOption
|
||||
}
|
||||
|
||||
// NewClient instantiates gRPC client for all the Service1 service servers.
|
||||
func NewClient(cc *grpc.ClientConn, opts ...grpc.CallOption) *Client {
|
||||
return &Client{
|
||||
grpccli: service1pb.NewService1Client(cc),
|
||||
opts: opts,
|
||||
}
|
||||
}
|
||||
|
||||
// Signin calls the "Signin" function in service1pb.Service1Client interface.
|
||||
func (c *Client) Signin() goa.Endpoint {
|
||||
return func(ctx context.Context, v interface{}) (interface{}, error) {
|
||||
inv := goagrpc.NewInvoker(
|
||||
BuildSigninFunc(c.grpccli, c.opts...),
|
||||
EncodeSigninRequest,
|
||||
DecodeSigninResponse)
|
||||
res, err := inv.Invoke(ctx, v)
|
||||
if err != nil {
|
||||
resp := goagrpc.DecodeError(err)
|
||||
switch message := resp.(type) {
|
||||
case *goapb.ErrorResponse:
|
||||
return nil, goagrpc.NewServiceError(message)
|
||||
default:
|
||||
return nil, goa.Fault(err.Error())
|
||||
}
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
}
|
||||
|
||||
// Secure calls the "Secure" function in service1pb.Service1Client interface.
|
||||
func (c *Client) Secure() goa.Endpoint {
|
||||
return func(ctx context.Context, v interface{}) (interface{}, error) {
|
||||
inv := goagrpc.NewInvoker(
|
||||
BuildSecureFunc(c.grpccli, c.opts...),
|
||||
EncodeSecureRequest,
|
||||
DecodeSecureResponse)
|
||||
res, err := inv.Invoke(ctx, v)
|
||||
if err != nil {
|
||||
resp := goagrpc.DecodeError(err)
|
||||
switch message := resp.(type) {
|
||||
case *goapb.ErrorResponse:
|
||||
return nil, goagrpc.NewServiceError(message)
|
||||
default:
|
||||
return nil, goa.Fault(err.Error())
|
||||
}
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
}
|
||||
87
go/goa_example/gen/grpc/service1/client/encode_decode.go
Normal file
87
go/goa_example/gen/grpc/service1/client/encode_decode.go
Normal file
@@ -0,0 +1,87 @@
|
||||
// Code generated by goa v3.6.0, DO NOT EDIT.
|
||||
//
|
||||
// Service1 gRPC client encoders and decoders
|
||||
//
|
||||
// Command:
|
||||
// $ goa gen goa_example/design
|
||||
|
||||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
service1pb "goa_example/gen/grpc/service1/pb"
|
||||
service1 "goa_example/gen/service1"
|
||||
|
||||
goagrpc "goa.design/goa/v3/grpc"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/metadata"
|
||||
)
|
||||
|
||||
// BuildSigninFunc builds the remote method to invoke for "Service1" service
|
||||
// "signin" endpoint.
|
||||
func BuildSigninFunc(grpccli service1pb.Service1Client, cliopts ...grpc.CallOption) goagrpc.RemoteFunc {
|
||||
return func(ctx context.Context, reqpb interface{}, opts ...grpc.CallOption) (interface{}, error) {
|
||||
for _, opt := range cliopts {
|
||||
opts = append(opts, opt)
|
||||
}
|
||||
if reqpb != nil {
|
||||
return grpccli.Signin(ctx, reqpb.(*service1pb.SigninRequest), opts...)
|
||||
}
|
||||
return grpccli.Signin(ctx, &service1pb.SigninRequest{}, opts...)
|
||||
}
|
||||
}
|
||||
|
||||
// EncodeSigninRequest encodes requests sent to Service1 signin endpoint.
|
||||
func EncodeSigninRequest(ctx context.Context, v interface{}, md *metadata.MD) (interface{}, error) {
|
||||
payload, ok := v.(*service1.SigninPayload)
|
||||
if !ok {
|
||||
return nil, goagrpc.ErrInvalidType("Service1", "signin", "*service1.SigninPayload", v)
|
||||
}
|
||||
(*md).Append("username", payload.Username)
|
||||
(*md).Append("password", payload.Password)
|
||||
return NewSigninRequest(), nil
|
||||
}
|
||||
|
||||
// DecodeSigninResponse decodes responses from the Service1 signin endpoint.
|
||||
func DecodeSigninResponse(ctx context.Context, v interface{}, hdr, trlr metadata.MD) (interface{}, error) {
|
||||
message, ok := v.(*service1pb.SigninResponse)
|
||||
if !ok {
|
||||
return nil, goagrpc.ErrInvalidType("Service1", "signin", "*service1pb.SigninResponse", v)
|
||||
}
|
||||
res := NewSigninResult(message)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// BuildSecureFunc builds the remote method to invoke for "Service1" service
|
||||
// "secure" endpoint.
|
||||
func BuildSecureFunc(grpccli service1pb.Service1Client, cliopts ...grpc.CallOption) goagrpc.RemoteFunc {
|
||||
return func(ctx context.Context, reqpb interface{}, opts ...grpc.CallOption) (interface{}, error) {
|
||||
for _, opt := range cliopts {
|
||||
opts = append(opts, opt)
|
||||
}
|
||||
if reqpb != nil {
|
||||
return grpccli.Secure(ctx, reqpb.(*service1pb.SecureRequest), opts...)
|
||||
}
|
||||
return grpccli.Secure(ctx, &service1pb.SecureRequest{}, opts...)
|
||||
}
|
||||
}
|
||||
|
||||
// EncodeSecureRequest encodes requests sent to Service1 secure endpoint.
|
||||
func EncodeSecureRequest(ctx context.Context, v interface{}, md *metadata.MD) (interface{}, error) {
|
||||
payload, ok := v.(*service1.SecurePayload)
|
||||
if !ok {
|
||||
return nil, goagrpc.ErrInvalidType("Service1", "secure", "*service1.SecurePayload", v)
|
||||
}
|
||||
(*md).Append("authorization", payload.Token)
|
||||
return NewSecureRequest(payload), nil
|
||||
}
|
||||
|
||||
// DecodeSecureResponse decodes responses from the Service1 secure endpoint.
|
||||
func DecodeSecureResponse(ctx context.Context, v interface{}, hdr, trlr metadata.MD) (interface{}, error) {
|
||||
message, ok := v.(*service1pb.SecureResponse)
|
||||
if !ok {
|
||||
return nil, goagrpc.ErrInvalidType("Service1", "secure", "*service1pb.SecureResponse", v)
|
||||
}
|
||||
res := NewSecureResult(message)
|
||||
return res, nil
|
||||
}
|
||||
48
go/goa_example/gen/grpc/service1/client/types.go
Normal file
48
go/goa_example/gen/grpc/service1/client/types.go
Normal file
@@ -0,0 +1,48 @@
|
||||
// Code generated by goa v3.6.0, DO NOT EDIT.
|
||||
//
|
||||
// Service1 gRPC client types
|
||||
//
|
||||
// Command:
|
||||
// $ goa gen goa_example/design
|
||||
|
||||
package client
|
||||
|
||||
import (
|
||||
service1pb "goa_example/gen/grpc/service1/pb"
|
||||
service1 "goa_example/gen/service1"
|
||||
)
|
||||
|
||||
// NewSigninRequest builds the gRPC request type from the payload of the
|
||||
// "signin" endpoint of the "Service1" service.
|
||||
func NewSigninRequest() *service1pb.SigninRequest {
|
||||
message := &service1pb.SigninRequest{}
|
||||
return message
|
||||
}
|
||||
|
||||
// NewSigninResult builds the result type of the "signin" endpoint of the
|
||||
// "Service1" service from the gRPC response type.
|
||||
func NewSigninResult(message *service1pb.SigninResponse) *service1.Creds {
|
||||
result := &service1.Creds{
|
||||
JWT: message.Jwt,
|
||||
APIKey: message.ApiKey,
|
||||
OauthToken: message.OauthToken,
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// NewSecureRequest builds the gRPC request type from the payload of the
|
||||
// "secure" endpoint of the "Service1" service.
|
||||
func NewSecureRequest(payload *service1.SecurePayload) *service1pb.SecureRequest {
|
||||
message := &service1pb.SecureRequest{}
|
||||
if payload.Fail != nil {
|
||||
message.Fail = *payload.Fail
|
||||
}
|
||||
return message
|
||||
}
|
||||
|
||||
// NewSecureResult builds the result type of the "secure" endpoint of the
|
||||
// "Service1" service from the gRPC response type.
|
||||
func NewSecureResult(message *service1pb.SecureResponse) string {
|
||||
result := message.Field
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,363 @@
|
||||
// Code generated with goa v3.6.0, DO NOT EDIT.
|
||||
//
|
||||
// Service1 protocol buffer definition
|
||||
//
|
||||
// Command:
|
||||
// $ goa gen goa_example/design
|
||||
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.27.1
|
||||
// protoc v3.19.4
|
||||
// source: goadesign_goagen_service1.proto
|
||||
|
||||
package service1pb
|
||||
|
||||
import (
|
||||
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
|
||||
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
|
||||
reflect "reflect"
|
||||
sync "sync"
|
||||
)
|
||||
|
||||
const (
|
||||
// Verify that this generated code is sufficiently up-to-date.
|
||||
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
|
||||
// Verify that runtime/protoimpl is sufficiently up-to-date.
|
||||
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
|
||||
)
|
||||
|
||||
type SigninRequest struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
}
|
||||
|
||||
func (x *SigninRequest) Reset() {
|
||||
*x = SigninRequest{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_goadesign_goagen_service1_proto_msgTypes[0]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *SigninRequest) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*SigninRequest) ProtoMessage() {}
|
||||
|
||||
func (x *SigninRequest) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_goadesign_goagen_service1_proto_msgTypes[0]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use SigninRequest.ProtoReflect.Descriptor instead.
|
||||
func (*SigninRequest) Descriptor() ([]byte, []int) {
|
||||
return file_goadesign_goagen_service1_proto_rawDescGZIP(), []int{0}
|
||||
}
|
||||
|
||||
type SigninResponse struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
// JWT token
|
||||
Jwt string `protobuf:"bytes,1,opt,name=jwt,proto3" json:"jwt,omitempty"`
|
||||
// API Key
|
||||
ApiKey string `protobuf:"bytes,2,opt,name=api_key,json=apiKey,proto3" json:"api_key,omitempty"`
|
||||
// OAuth2 token
|
||||
OauthToken string `protobuf:"bytes,3,opt,name=oauth_token,json=oauthToken,proto3" json:"oauth_token,omitempty"`
|
||||
}
|
||||
|
||||
func (x *SigninResponse) Reset() {
|
||||
*x = SigninResponse{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_goadesign_goagen_service1_proto_msgTypes[1]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *SigninResponse) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*SigninResponse) ProtoMessage() {}
|
||||
|
||||
func (x *SigninResponse) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_goadesign_goagen_service1_proto_msgTypes[1]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use SigninResponse.ProtoReflect.Descriptor instead.
|
||||
func (*SigninResponse) Descriptor() ([]byte, []int) {
|
||||
return file_goadesign_goagen_service1_proto_rawDescGZIP(), []int{1}
|
||||
}
|
||||
|
||||
func (x *SigninResponse) GetJwt() string {
|
||||
if x != nil {
|
||||
return x.Jwt
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *SigninResponse) GetApiKey() string {
|
||||
if x != nil {
|
||||
return x.ApiKey
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *SigninResponse) GetOauthToken() string {
|
||||
if x != nil {
|
||||
return x.OauthToken
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type SecureRequest struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
// Whether to force auth failure even with a valid JWT
|
||||
Fail bool `protobuf:"varint,1,opt,name=fail,proto3" json:"fail,omitempty"`
|
||||
}
|
||||
|
||||
func (x *SecureRequest) Reset() {
|
||||
*x = SecureRequest{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_goadesign_goagen_service1_proto_msgTypes[2]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *SecureRequest) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*SecureRequest) ProtoMessage() {}
|
||||
|
||||
func (x *SecureRequest) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_goadesign_goagen_service1_proto_msgTypes[2]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use SecureRequest.ProtoReflect.Descriptor instead.
|
||||
func (*SecureRequest) Descriptor() ([]byte, []int) {
|
||||
return file_goadesign_goagen_service1_proto_rawDescGZIP(), []int{2}
|
||||
}
|
||||
|
||||
func (x *SecureRequest) GetFail() bool {
|
||||
if x != nil {
|
||||
return x.Fail
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
type SecureResponse struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
Field string `protobuf:"bytes,1,opt,name=field,proto3" json:"field,omitempty"`
|
||||
}
|
||||
|
||||
func (x *SecureResponse) Reset() {
|
||||
*x = SecureResponse{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_goadesign_goagen_service1_proto_msgTypes[3]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *SecureResponse) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*SecureResponse) ProtoMessage() {}
|
||||
|
||||
func (x *SecureResponse) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_goadesign_goagen_service1_proto_msgTypes[3]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use SecureResponse.ProtoReflect.Descriptor instead.
|
||||
func (*SecureResponse) Descriptor() ([]byte, []int) {
|
||||
return file_goadesign_goagen_service1_proto_rawDescGZIP(), []int{3}
|
||||
}
|
||||
|
||||
func (x *SecureResponse) GetField() string {
|
||||
if x != nil {
|
||||
return x.Field
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
var File_goadesign_goagen_service1_proto protoreflect.FileDescriptor
|
||||
|
||||
var file_goadesign_goagen_service1_proto_rawDesc = []byte{
|
||||
0x0a, 0x1f, 0x67, 0x6f, 0x61, 0x64, 0x65, 0x73, 0x69, 0x67, 0x6e, 0x5f, 0x67, 0x6f, 0x61, 0x67,
|
||||
0x65, 0x6e, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x31, 0x2e, 0x70, 0x72, 0x6f, 0x74,
|
||||
0x6f, 0x12, 0x08, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x31, 0x22, 0x0f, 0x0a, 0x0d, 0x53,
|
||||
0x69, 0x67, 0x6e, 0x69, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x22, 0x5c, 0x0a, 0x0e,
|
||||
0x53, 0x69, 0x67, 0x6e, 0x69, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x10,
|
||||
0x0a, 0x03, 0x6a, 0x77, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6a, 0x77, 0x74,
|
||||
0x12, 0x17, 0x0a, 0x07, 0x61, 0x70, 0x69, 0x5f, 0x6b, 0x65, 0x79, 0x18, 0x02, 0x20, 0x01, 0x28,
|
||||
0x09, 0x52, 0x06, 0x61, 0x70, 0x69, 0x4b, 0x65, 0x79, 0x12, 0x1f, 0x0a, 0x0b, 0x6f, 0x61, 0x75,
|
||||
0x74, 0x68, 0x5f, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0a,
|
||||
0x6f, 0x61, 0x75, 0x74, 0x68, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x22, 0x23, 0x0a, 0x0d, 0x53, 0x65,
|
||||
0x63, 0x75, 0x72, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x66,
|
||||
0x61, 0x69, 0x6c, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x04, 0x66, 0x61, 0x69, 0x6c, 0x22,
|
||||
0x26, 0x0a, 0x0e, 0x53, 0x65, 0x63, 0x75, 0x72, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73,
|
||||
0x65, 0x12, 0x14, 0x0a, 0x05, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09,
|
||||
0x52, 0x05, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x32, 0x84, 0x01, 0x0a, 0x08, 0x53, 0x65, 0x72, 0x76,
|
||||
0x69, 0x63, 0x65, 0x31, 0x12, 0x3b, 0x0a, 0x06, 0x53, 0x69, 0x67, 0x6e, 0x69, 0x6e, 0x12, 0x17,
|
||||
0x2e, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x31, 0x2e, 0x53, 0x69, 0x67, 0x6e, 0x69, 0x6e,
|
||||
0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x18, 0x2e, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63,
|
||||
0x65, 0x31, 0x2e, 0x53, 0x69, 0x67, 0x6e, 0x69, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73,
|
||||
0x65, 0x12, 0x3b, 0x0a, 0x06, 0x53, 0x65, 0x63, 0x75, 0x72, 0x65, 0x12, 0x17, 0x2e, 0x73, 0x65,
|
||||
0x72, 0x76, 0x69, 0x63, 0x65, 0x31, 0x2e, 0x53, 0x65, 0x63, 0x75, 0x72, 0x65, 0x52, 0x65, 0x71,
|
||||
0x75, 0x65, 0x73, 0x74, 0x1a, 0x18, 0x2e, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x31, 0x2e,
|
||||
0x53, 0x65, 0x63, 0x75, 0x72, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x0d,
|
||||
0x5a, 0x0b, 0x2f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x31, 0x70, 0x62, 0x62, 0x06, 0x70,
|
||||
0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||
}
|
||||
|
||||
var (
|
||||
file_goadesign_goagen_service1_proto_rawDescOnce sync.Once
|
||||
file_goadesign_goagen_service1_proto_rawDescData = file_goadesign_goagen_service1_proto_rawDesc
|
||||
)
|
||||
|
||||
func file_goadesign_goagen_service1_proto_rawDescGZIP() []byte {
|
||||
file_goadesign_goagen_service1_proto_rawDescOnce.Do(func() {
|
||||
file_goadesign_goagen_service1_proto_rawDescData = protoimpl.X.CompressGZIP(file_goadesign_goagen_service1_proto_rawDescData)
|
||||
})
|
||||
return file_goadesign_goagen_service1_proto_rawDescData
|
||||
}
|
||||
|
||||
var file_goadesign_goagen_service1_proto_msgTypes = make([]protoimpl.MessageInfo, 4)
|
||||
var file_goadesign_goagen_service1_proto_goTypes = []interface{}{
|
||||
(*SigninRequest)(nil), // 0: service1.SigninRequest
|
||||
(*SigninResponse)(nil), // 1: service1.SigninResponse
|
||||
(*SecureRequest)(nil), // 2: service1.SecureRequest
|
||||
(*SecureResponse)(nil), // 3: service1.SecureResponse
|
||||
}
|
||||
var file_goadesign_goagen_service1_proto_depIdxs = []int32{
|
||||
0, // 0: service1.Service1.Signin:input_type -> service1.SigninRequest
|
||||
2, // 1: service1.Service1.Secure:input_type -> service1.SecureRequest
|
||||
1, // 2: service1.Service1.Signin:output_type -> service1.SigninResponse
|
||||
3, // 3: service1.Service1.Secure:output_type -> service1.SecureResponse
|
||||
2, // [2:4] is the sub-list for method output_type
|
||||
0, // [0:2] is the sub-list for method input_type
|
||||
0, // [0:0] is the sub-list for extension type_name
|
||||
0, // [0:0] is the sub-list for extension extendee
|
||||
0, // [0:0] is the sub-list for field type_name
|
||||
}
|
||||
|
||||
func init() { file_goadesign_goagen_service1_proto_init() }
|
||||
func file_goadesign_goagen_service1_proto_init() {
|
||||
if File_goadesign_goagen_service1_proto != nil {
|
||||
return
|
||||
}
|
||||
if !protoimpl.UnsafeEnabled {
|
||||
file_goadesign_goagen_service1_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*SigninRequest); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_goadesign_goagen_service1_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*SigninResponse); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_goadesign_goagen_service1_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*SecureRequest); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_goadesign_goagen_service1_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*SecureResponse); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
type x struct{}
|
||||
out := protoimpl.TypeBuilder{
|
||||
File: protoimpl.DescBuilder{
|
||||
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
|
||||
RawDescriptor: file_goadesign_goagen_service1_proto_rawDesc,
|
||||
NumEnums: 0,
|
||||
NumMessages: 4,
|
||||
NumExtensions: 0,
|
||||
NumServices: 1,
|
||||
},
|
||||
GoTypes: file_goadesign_goagen_service1_proto_goTypes,
|
||||
DependencyIndexes: file_goadesign_goagen_service1_proto_depIdxs,
|
||||
MessageInfos: file_goadesign_goagen_service1_proto_msgTypes,
|
||||
}.Build()
|
||||
File_goadesign_goagen_service1_proto = out.File
|
||||
file_goadesign_goagen_service1_proto_rawDesc = nil
|
||||
file_goadesign_goagen_service1_proto_goTypes = nil
|
||||
file_goadesign_goagen_service1_proto_depIdxs = nil
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
// Code generated with goa v3.6.0, DO NOT EDIT.
|
||||
//
|
||||
// Service1 protocol buffer definition
|
||||
//
|
||||
// Command:
|
||||
// $ goa gen goa_example/design
|
||||
|
||||
syntax = "proto3";
|
||||
|
||||
package service1;
|
||||
|
||||
option go_package = "/service1pb";
|
||||
|
||||
// The secured service exposes endpoints that require valid authorization
|
||||
// credentials.
|
||||
service Service1 {
|
||||
// Signin implements signin.
|
||||
rpc Signin (SigninRequest) returns (SigninResponse);
|
||||
// 这是一个需要JWT认证的接口
|
||||
rpc Secure (SecureRequest) returns (SecureResponse);
|
||||
}
|
||||
|
||||
message SigninRequest {
|
||||
}
|
||||
|
||||
message SigninResponse {
|
||||
// JWT token
|
||||
string jwt = 1;
|
||||
// API Key
|
||||
string api_key = 2;
|
||||
// OAuth2 token
|
||||
string oauth_token = 3;
|
||||
}
|
||||
|
||||
message SecureRequest {
|
||||
// Whether to force auth failure even with a valid JWT
|
||||
bool fail = 1;
|
||||
}
|
||||
|
||||
message SecureResponse {
|
||||
string field = 1;
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
|
||||
// versions:
|
||||
// - protoc-gen-go-grpc v1.2.0
|
||||
// - protoc v3.19.4
|
||||
// source: goadesign_goagen_service1.proto
|
||||
|
||||
package service1pb
|
||||
|
||||
import (
|
||||
context "context"
|
||||
grpc "google.golang.org/grpc"
|
||||
codes "google.golang.org/grpc/codes"
|
||||
status "google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
// This is a compile-time assertion to ensure that this generated file
|
||||
// is compatible with the grpc package it is being compiled against.
|
||||
// Requires gRPC-Go v1.32.0 or later.
|
||||
const _ = grpc.SupportPackageIsVersion7
|
||||
|
||||
// Service1Client is the client API for Service1 service.
|
||||
//
|
||||
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
|
||||
type Service1Client interface {
|
||||
// Signin implements signin.
|
||||
Signin(ctx context.Context, in *SigninRequest, opts ...grpc.CallOption) (*SigninResponse, error)
|
||||
// 这是一个需要JWT认证的接口
|
||||
Secure(ctx context.Context, in *SecureRequest, opts ...grpc.CallOption) (*SecureResponse, error)
|
||||
}
|
||||
|
||||
type service1Client struct {
|
||||
cc grpc.ClientConnInterface
|
||||
}
|
||||
|
||||
func NewService1Client(cc grpc.ClientConnInterface) Service1Client {
|
||||
return &service1Client{cc}
|
||||
}
|
||||
|
||||
func (c *service1Client) Signin(ctx context.Context, in *SigninRequest, opts ...grpc.CallOption) (*SigninResponse, error) {
|
||||
out := new(SigninResponse)
|
||||
err := c.cc.Invoke(ctx, "/service1.Service1/Signin", in, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *service1Client) Secure(ctx context.Context, in *SecureRequest, opts ...grpc.CallOption) (*SecureResponse, error) {
|
||||
out := new(SecureResponse)
|
||||
err := c.cc.Invoke(ctx, "/service1.Service1/Secure", in, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Service1Server is the server API for Service1 service.
|
||||
// All implementations must embed UnimplementedService1Server
|
||||
// for forward compatibility
|
||||
type Service1Server interface {
|
||||
// Signin implements signin.
|
||||
Signin(context.Context, *SigninRequest) (*SigninResponse, error)
|
||||
// 这是一个需要JWT认证的接口
|
||||
Secure(context.Context, *SecureRequest) (*SecureResponse, error)
|
||||
mustEmbedUnimplementedService1Server()
|
||||
}
|
||||
|
||||
// UnimplementedService1Server must be embedded to have forward compatible implementations.
|
||||
type UnimplementedService1Server struct {
|
||||
}
|
||||
|
||||
func (UnimplementedService1Server) Signin(context.Context, *SigninRequest) (*SigninResponse, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method Signin not implemented")
|
||||
}
|
||||
func (UnimplementedService1Server) Secure(context.Context, *SecureRequest) (*SecureResponse, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method Secure not implemented")
|
||||
}
|
||||
func (UnimplementedService1Server) mustEmbedUnimplementedService1Server() {}
|
||||
|
||||
// UnsafeService1Server may be embedded to opt out of forward compatibility for this service.
|
||||
// Use of this interface is not recommended, as added methods to Service1Server will
|
||||
// result in compilation errors.
|
||||
type UnsafeService1Server interface {
|
||||
mustEmbedUnimplementedService1Server()
|
||||
}
|
||||
|
||||
func RegisterService1Server(s grpc.ServiceRegistrar, srv Service1Server) {
|
||||
s.RegisterService(&Service1_ServiceDesc, srv)
|
||||
}
|
||||
|
||||
func _Service1_Signin_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(SigninRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(Service1Server).Signin(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: "/service1.Service1/Signin",
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(Service1Server).Signin(ctx, req.(*SigninRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _Service1_Secure_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(SecureRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(Service1Server).Secure(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: "/service1.Service1/Secure",
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(Service1Server).Secure(ctx, req.(*SecureRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
// Service1_ServiceDesc is the grpc.ServiceDesc for Service1 service.
|
||||
// It's only intended for direct use with grpc.RegisterService,
|
||||
// and not to be introspected or modified (even as a copy)
|
||||
var Service1_ServiceDesc = grpc.ServiceDesc{
|
||||
ServiceName: "service1.Service1",
|
||||
HandlerType: (*Service1Server)(nil),
|
||||
Methods: []grpc.MethodDesc{
|
||||
{
|
||||
MethodName: "Signin",
|
||||
Handler: _Service1_Signin_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "Secure",
|
||||
Handler: _Service1_Secure_Handler,
|
||||
},
|
||||
},
|
||||
Streams: []grpc.StreamDesc{},
|
||||
Metadata: "goadesign_goagen_service1.proto",
|
||||
}
|
||||
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
|
||||
}
|
||||
179
go/goa_example/gen/http/cli/host/cli.go
Normal file
179
go/goa_example/gen/http/cli/host/cli.go
Normal file
@@ -0,0 +1,179 @@
|
||||
// Code generated by goa v3.6.0, DO NOT EDIT.
|
||||
//
|
||||
// host HTTP client CLI support package
|
||||
//
|
||||
// Command:
|
||||
// $ goa gen goa_example/design
|
||||
|
||||
package cli
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
service1c "goa_example/gen/http/service1/client"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
goahttp "goa.design/goa/v3/http"
|
||||
goa "goa.design/goa/v3/pkg"
|
||||
)
|
||||
|
||||
// UsageCommands returns the set of commands and sub-commands using the format
|
||||
//
|
||||
// command (subcommand1|subcommand2|...)
|
||||
//
|
||||
func UsageCommands() string {
|
||||
return `service1 (signin|secure)
|
||||
`
|
||||
}
|
||||
|
||||
// UsageExamples produces an example of a valid invocation of the CLI tool.
|
||||
func UsageExamples() string {
|
||||
return os.Args[0] + ` service1 signin --username "user" --password "password"` + "\n" +
|
||||
""
|
||||
}
|
||||
|
||||
// ParseEndpoint returns the endpoint and payload as specified on the command
|
||||
// line.
|
||||
func ParseEndpoint(
|
||||
scheme, host string,
|
||||
doer goahttp.Doer,
|
||||
enc func(*http.Request) goahttp.Encoder,
|
||||
dec func(*http.Response) goahttp.Decoder,
|
||||
restore bool,
|
||||
) (goa.Endpoint, interface{}, error) {
|
||||
var (
|
||||
service1Flags = flag.NewFlagSet("service1", flag.ContinueOnError)
|
||||
|
||||
service1SigninFlags = flag.NewFlagSet("signin", flag.ExitOnError)
|
||||
service1SigninUsernameFlag = service1SigninFlags.String("username", "REQUIRED", "Username used to perform signin")
|
||||
service1SigninPasswordFlag = service1SigninFlags.String("password", "REQUIRED", "Password used to perform signin")
|
||||
|
||||
service1SecureFlags = flag.NewFlagSet("secure", flag.ExitOnError)
|
||||
service1SecureBodyFlag = service1SecureFlags.String("body", "REQUIRED", "")
|
||||
service1SecureTokenFlag = service1SecureFlags.String("token", "REQUIRED", "")
|
||||
)
|
||||
service1Flags.Usage = service1Usage
|
||||
service1SigninFlags.Usage = service1SigninUsage
|
||||
service1SecureFlags.Usage = service1SecureUsage
|
||||
|
||||
if err := flag.CommandLine.Parse(os.Args[1:]); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
if flag.NArg() < 2 { // two non flag args are required: SERVICE and ENDPOINT (aka COMMAND)
|
||||
return nil, nil, fmt.Errorf("not enough arguments")
|
||||
}
|
||||
|
||||
var (
|
||||
svcn string
|
||||
svcf *flag.FlagSet
|
||||
)
|
||||
{
|
||||
svcn = flag.Arg(0)
|
||||
switch svcn {
|
||||
case "service1":
|
||||
svcf = service1Flags
|
||||
default:
|
||||
return nil, nil, fmt.Errorf("unknown service %q", svcn)
|
||||
}
|
||||
}
|
||||
if err := svcf.Parse(flag.Args()[1:]); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
var (
|
||||
epn string
|
||||
epf *flag.FlagSet
|
||||
)
|
||||
{
|
||||
epn = svcf.Arg(0)
|
||||
switch svcn {
|
||||
case "service1":
|
||||
switch epn {
|
||||
case "signin":
|
||||
epf = service1SigninFlags
|
||||
|
||||
case "secure":
|
||||
epf = service1SecureFlags
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
if epf == nil {
|
||||
return nil, nil, fmt.Errorf("unknown %q endpoint %q", svcn, epn)
|
||||
}
|
||||
|
||||
// Parse endpoint flags if any
|
||||
if svcf.NArg() > 1 {
|
||||
if err := epf.Parse(svcf.Args()[1:]); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
data interface{}
|
||||
endpoint goa.Endpoint
|
||||
err error
|
||||
)
|
||||
{
|
||||
switch svcn {
|
||||
case "service1":
|
||||
c := service1c.NewClient(scheme, host, doer, enc, dec, restore)
|
||||
switch epn {
|
||||
case "signin":
|
||||
endpoint = c.Signin()
|
||||
data, err = service1c.BuildSigninPayload(*service1SigninUsernameFlag, *service1SigninPasswordFlag)
|
||||
case "secure":
|
||||
endpoint = c.Secure()
|
||||
data, err = service1c.BuildSecurePayload(*service1SecureBodyFlag, *service1SecureTokenFlag)
|
||||
}
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return endpoint, data, nil
|
||||
}
|
||||
|
||||
// service1Usage displays the usage of the service1 command and its subcommands.
|
||||
func service1Usage() {
|
||||
fmt.Fprintf(os.Stderr, `The secured service exposes endpoints that require valid authorization credentials.
|
||||
Usage:
|
||||
%[1]s [globalflags] service1 COMMAND [flags]
|
||||
|
||||
COMMAND:
|
||||
signin: Signin implements signin.
|
||||
secure: 这是一个需要JWT认证的接口
|
||||
|
||||
Additional help:
|
||||
%[1]s service1 COMMAND --help
|
||||
`, os.Args[0])
|
||||
}
|
||||
func service1SigninUsage() {
|
||||
fmt.Fprintf(os.Stderr, `%[1]s [flags] service1 signin -username STRING -password STRING
|
||||
|
||||
Signin implements signin.
|
||||
-username STRING: Username used to perform signin
|
||||
-password STRING: Password used to perform signin
|
||||
|
||||
Example:
|
||||
%[1]s service1 signin --username "user" --password "password"
|
||||
`, os.Args[0])
|
||||
}
|
||||
|
||||
func service1SecureUsage() {
|
||||
fmt.Fprintf(os.Stderr, `%[1]s [flags] service1 secure -body JSON -token STRING
|
||||
|
||||
这是一个需要JWT认证的接口
|
||||
-body JSON:
|
||||
-token STRING:
|
||||
|
||||
Example:
|
||||
%[1]s service1 secure --body '{
|
||||
"fail": true
|
||||
}' --token "Ducimus aut similique."
|
||||
`, os.Args[0])
|
||||
}
|
||||
1
go/goa_example/gen/http/openapi.json
Normal file
1
go/goa_example/gen/http/openapi.json
Normal file
@@ -0,0 +1 @@
|
||||
{"swagger":"2.0","info":{"title":"A Goa Example Service","description":"HTTP service for test","version":""},"host":"localhost:8088","consumes":["application/json","application/xml","application/gob"],"produces":["application/json","application/xml","application/gob"],"paths":{"/secure":{"get":{"tags":["Service1"],"summary":"secure Service1","description":"这是一个需要JWT认证的接口","operationId":"Service1#secure","parameters":[{"name":"Authorization","in":"header","description":"JWT used for authentication","required":true,"type":"string"},{"name":"SecureRequestBody","in":"body","required":true,"schema":{"$ref":"#/definitions/Service1SecureRequestBody"}}],"responses":{"200":{"description":"OK response.","schema":{"type":"string"}},"401":{"description":"Unauthorized response.","schema":{"type":"string"}}},"schemes":["http"],"security":[{"jwt_header_Authorization":[]}]}},"/signin":{"post":{"tags":["Service1"],"summary":"signin Service1","operationId":"Service1#signin","parameters":[{"name":"Authorization","in":"header","description":"Basic Auth security using Basic scheme (https://tools.ietf.org/html/rfc7617)","required":true,"type":"string"}],"responses":{"200":{"description":"OK response.","schema":{"$ref":"#/definitions/Service1SigninOKResponseBody","required":["jwt","api_key","oauth_token"]}},"400":{"description":"Bad Request response.","schema":{"$ref":"#/definitions/Service1SigninBadRequestResponseBody","required":["jwt","api_key","oauth_token"]}},"401":{"description":"Unauthorized response.","schema":{"type":"string"}}},"schemes":["http"],"security":[{"basic_header_Authorization":[]}]}}},"definitions":{"Service1SecureRequestBody":{"title":"Service1SecureRequestBody","type":"object","properties":{"fail":{"type":"boolean","description":"Whether to force auth failure even with a valid JWT","example":false}},"example":{"fail":false}},"Service1SigninBadRequestResponseBody":{"title":"Service1SigninBadRequestResponseBody","type":"object","properties":{"api_key":{"type":"string","description":"API Key","example":"abcdef12345"},"jwt":{"type":"string","description":"JWT token","example":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ"},"oauth_token":{"type":"string","description":"OAuth2 token","example":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ"}},"example":{"api_key":"abcdef12345","jwt":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ","oauth_token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ"},"required":["jwt","api_key","oauth_token"]},"Service1SigninOKResponseBody":{"title":"Service1SigninOKResponseBody","type":"object","properties":{"api_key":{"type":"string","description":"API Key","example":"abcdef12345"},"jwt":{"type":"string","description":"JWT token","example":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ"},"oauth_token":{"type":"string","description":"OAuth2 token","example":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ"}},"example":{"api_key":"abcdef12345","jwt":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ","oauth_token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ"},"required":["jwt","api_key","oauth_token"]}},"securityDefinitions":{"basic_header_Authorization":{"type":"basic","description":"Basic authentication used to authenticate security principal during signin"},"jwt_header_Authorization":{"type":"apiKey","description":"Secures endpoint by requiring a valid JWT token retrieved via the signin endpoint. Supports scopes \"api:read\" and \"api:write\".","name":"Authorization","in":"header"}}}
|
||||
153
go/goa_example/gen/http/openapi.yaml
Normal file
153
go/goa_example/gen/http/openapi.yaml
Normal file
@@ -0,0 +1,153 @@
|
||||
swagger: "2.0"
|
||||
info:
|
||||
title: A Goa Example Service
|
||||
description: HTTP service for test
|
||||
version: ""
|
||||
host: localhost:8088
|
||||
consumes:
|
||||
- application/json
|
||||
- application/xml
|
||||
- application/gob
|
||||
produces:
|
||||
- application/json
|
||||
- application/xml
|
||||
- application/gob
|
||||
paths:
|
||||
/secure:
|
||||
get:
|
||||
tags:
|
||||
- Service1
|
||||
summary: secure Service1
|
||||
description: 这是一个需要JWT认证的接口
|
||||
operationId: Service1#secure
|
||||
parameters:
|
||||
- name: Authorization
|
||||
in: header
|
||||
description: JWT used for authentication
|
||||
required: true
|
||||
type: string
|
||||
- name: SecureRequestBody
|
||||
in: body
|
||||
required: true
|
||||
schema:
|
||||
$ref: '#/definitions/Service1SecureRequestBody'
|
||||
responses:
|
||||
"200":
|
||||
description: OK response.
|
||||
schema:
|
||||
type: string
|
||||
"401":
|
||||
description: Unauthorized response.
|
||||
schema:
|
||||
type: string
|
||||
schemes:
|
||||
- http
|
||||
security:
|
||||
- jwt_header_Authorization: []
|
||||
/signin:
|
||||
post:
|
||||
tags:
|
||||
- Service1
|
||||
summary: signin Service1
|
||||
operationId: Service1#signin
|
||||
parameters:
|
||||
- name: Authorization
|
||||
in: header
|
||||
description: Basic Auth security using Basic scheme (https://tools.ietf.org/html/rfc7617)
|
||||
required: true
|
||||
type: string
|
||||
responses:
|
||||
"200":
|
||||
description: OK response.
|
||||
schema:
|
||||
$ref: '#/definitions/Service1SigninOKResponseBody'
|
||||
required:
|
||||
- jwt
|
||||
- api_key
|
||||
- oauth_token
|
||||
"400":
|
||||
description: Bad Request response.
|
||||
schema:
|
||||
$ref: '#/definitions/Service1SigninBadRequestResponseBody'
|
||||
required:
|
||||
- jwt
|
||||
- api_key
|
||||
- oauth_token
|
||||
"401":
|
||||
description: Unauthorized response.
|
||||
schema:
|
||||
type: string
|
||||
schemes:
|
||||
- http
|
||||
security:
|
||||
- basic_header_Authorization: []
|
||||
definitions:
|
||||
Service1SecureRequestBody:
|
||||
title: Service1SecureRequestBody
|
||||
type: object
|
||||
properties:
|
||||
fail:
|
||||
type: boolean
|
||||
description: Whether to force auth failure even with a valid JWT
|
||||
example: false
|
||||
example:
|
||||
fail: false
|
||||
Service1SigninBadRequestResponseBody:
|
||||
title: Service1SigninBadRequestResponseBody
|
||||
type: object
|
||||
properties:
|
||||
api_key:
|
||||
type: string
|
||||
description: API Key
|
||||
example: abcdef12345
|
||||
jwt:
|
||||
type: string
|
||||
description: JWT token
|
||||
example: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ
|
||||
oauth_token:
|
||||
type: string
|
||||
description: OAuth2 token
|
||||
example: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ
|
||||
example:
|
||||
api_key: abcdef12345
|
||||
jwt: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ
|
||||
oauth_token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ
|
||||
required:
|
||||
- jwt
|
||||
- api_key
|
||||
- oauth_token
|
||||
Service1SigninOKResponseBody:
|
||||
title: Service1SigninOKResponseBody
|
||||
type: object
|
||||
properties:
|
||||
api_key:
|
||||
type: string
|
||||
description: API Key
|
||||
example: abcdef12345
|
||||
jwt:
|
||||
type: string
|
||||
description: JWT token
|
||||
example: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ
|
||||
oauth_token:
|
||||
type: string
|
||||
description: OAuth2 token
|
||||
example: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ
|
||||
example:
|
||||
api_key: abcdef12345
|
||||
jwt: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ
|
||||
oauth_token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ
|
||||
required:
|
||||
- jwt
|
||||
- api_key
|
||||
- oauth_token
|
||||
securityDefinitions:
|
||||
basic_header_Authorization:
|
||||
type: basic
|
||||
description: Basic authentication used to authenticate security principal during
|
||||
signin
|
||||
jwt_header_Authorization:
|
||||
type: apiKey
|
||||
description: Secures endpoint by requiring a valid JWT token retrieved via the
|
||||
signin endpoint. Supports scopes "api:read" and "api:write".
|
||||
name: Authorization
|
||||
in: header
|
||||
1
go/goa_example/gen/http/openapi3.json
Normal file
1
go/goa_example/gen/http/openapi3.json
Normal file
@@ -0,0 +1 @@
|
||||
{"openapi":"3.0.3","info":{"title":"A Goa Example Service","description":"HTTP service for test","version":"1.0"},"servers":[{"url":"http://localhost:8088"},{"url":"http://localhost:8088"}],"paths":{"/secure":{"get":{"tags":["Service1"],"summary":"secure Service1","description":"这是一个需要JWT认证的接口","operationId":"Service1#secure","requestBody":{"required":true,"content":{"application/json":{"schema":{"$ref":"#/components/schemas/SecureRequestBody"},"example":{"fail":true}}}},"responses":{"200":{"description":"OK response.","content":{"application/json":{"schema":{"type":"string","example":"Commodi earum ut dolorem qui."},"example":"Aliquid corrupti facere voluptate."}}},"401":{"description":"Unauthorized response.","content":{"application/json":{"schema":{"type":"string","example":"Alias placeat est tenetur ad distinctio nesciunt."},"example":"Odit qui ut culpa est."}}}},"security":[{"jwt_header_Authorization":[]}]}},"/signin":{"post":{"tags":["Service1"],"summary":"signin Service1","operationId":"Service1#signin","responses":{"200":{"description":"OK response.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/Creds"},"example":{"api_key":"abcdef12345","jwt":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ","oauth_token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ"}}}},"400":{"description":"Bad Request response.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/Creds"},"example":{"api_key":"abcdef12345","jwt":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ","oauth_token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ"}}}},"401":{"description":"Unauthorized response.","content":{"application/json":{"schema":{"type":"string","example":"Voluptate non dolore autem ipsam omnis."},"example":"Et necessitatibus cupiditate repudiandae iste."}}}},"security":[{"basic_header_Authorization":[]}]}}},"components":{"schemas":{"Creds":{"type":"object","properties":{"api_key":{"type":"string","description":"API Key","example":"abcdef12345"},"jwt":{"type":"string","description":"JWT token","example":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ"},"oauth_token":{"type":"string","description":"OAuth2 token","example":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ"}},"example":{"api_key":"abcdef12345","jwt":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ","oauth_token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ"},"required":["jwt","api_key","oauth_token"]},"SecureRequestBody":{"type":"object","properties":{"fail":{"type":"boolean","description":"Whether to force auth failure even with a valid JWT","example":false}},"example":{"fail":false}}},"securitySchemes":{"basic_header_Authorization":{"type":"http","description":"Basic authentication used to authenticate security principal during signin","scheme":"basic"},"jwt_header_Authorization":{"type":"http","description":"Secures endpoint by requiring a valid JWT token retrieved via the signin endpoint. Supports scopes \"api:read\" and \"api:write\".","scheme":"bearer"}}},"tags":[{"name":"Service1","description":"The secured service exposes endpoints that require valid authorization credentials."}]}
|
||||
129
go/goa_example/gen/http/openapi3.yaml
Normal file
129
go/goa_example/gen/http/openapi3.yaml
Normal file
@@ -0,0 +1,129 @@
|
||||
openapi: 3.0.3
|
||||
info:
|
||||
title: A Goa Example Service
|
||||
description: HTTP service for test
|
||||
version: "1.0"
|
||||
servers:
|
||||
- url: http://localhost:8088
|
||||
- url: http://localhost:8088
|
||||
paths:
|
||||
/secure:
|
||||
get:
|
||||
tags:
|
||||
- Service1
|
||||
summary: secure Service1
|
||||
description: 这是一个需要JWT认证的接口
|
||||
operationId: Service1#secure
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/SecureRequestBody'
|
||||
example:
|
||||
fail: true
|
||||
responses:
|
||||
"200":
|
||||
description: OK response.
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: string
|
||||
example: Commodi earum ut dolorem qui.
|
||||
example: Aliquid corrupti facere voluptate.
|
||||
"401":
|
||||
description: Unauthorized response.
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: string
|
||||
example: Alias placeat est tenetur ad distinctio nesciunt.
|
||||
example: Odit qui ut culpa est.
|
||||
security:
|
||||
- jwt_header_Authorization: []
|
||||
/signin:
|
||||
post:
|
||||
tags:
|
||||
- Service1
|
||||
summary: signin Service1
|
||||
operationId: Service1#signin
|
||||
responses:
|
||||
"200":
|
||||
description: OK response.
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/Creds'
|
||||
example:
|
||||
api_key: abcdef12345
|
||||
jwt: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ
|
||||
oauth_token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ
|
||||
"400":
|
||||
description: Bad Request response.
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/Creds'
|
||||
example:
|
||||
api_key: abcdef12345
|
||||
jwt: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ
|
||||
oauth_token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ
|
||||
"401":
|
||||
description: Unauthorized response.
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: string
|
||||
example: Voluptate non dolore autem ipsam omnis.
|
||||
example: Et necessitatibus cupiditate repudiandae iste.
|
||||
security:
|
||||
- basic_header_Authorization: []
|
||||
components:
|
||||
schemas:
|
||||
Creds:
|
||||
type: object
|
||||
properties:
|
||||
api_key:
|
||||
type: string
|
||||
description: API Key
|
||||
example: abcdef12345
|
||||
jwt:
|
||||
type: string
|
||||
description: JWT token
|
||||
example: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ
|
||||
oauth_token:
|
||||
type: string
|
||||
description: OAuth2 token
|
||||
example: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ
|
||||
example:
|
||||
api_key: abcdef12345
|
||||
jwt: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ
|
||||
oauth_token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ
|
||||
required:
|
||||
- jwt
|
||||
- api_key
|
||||
- oauth_token
|
||||
SecureRequestBody:
|
||||
type: object
|
||||
properties:
|
||||
fail:
|
||||
type: boolean
|
||||
description: Whether to force auth failure even with a valid JWT
|
||||
example: false
|
||||
example:
|
||||
fail: false
|
||||
securitySchemes:
|
||||
basic_header_Authorization:
|
||||
type: http
|
||||
description: Basic authentication used to authenticate security principal during
|
||||
signin
|
||||
scheme: basic
|
||||
jwt_header_Authorization:
|
||||
type: http
|
||||
description: Secures endpoint by requiring a valid JWT token retrieved via the
|
||||
signin endpoint. Supports scopes "api:read" and "api:write".
|
||||
scheme: bearer
|
||||
tags:
|
||||
- name: Service1
|
||||
description: The secured service exposes endpoints that require valid authorization
|
||||
credentials.
|
||||
55
go/goa_example/gen/http/service1/client/cli.go
Normal file
55
go/goa_example/gen/http/service1/client/cli.go
Normal file
@@ -0,0 +1,55 @@
|
||||
// Code generated by goa v3.6.0, DO NOT EDIT.
|
||||
//
|
||||
// Service1 HTTP client CLI support package
|
||||
//
|
||||
// Command:
|
||||
// $ goa gen goa_example/design
|
||||
|
||||
package client
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
service1 "goa_example/gen/service1"
|
||||
)
|
||||
|
||||
// BuildSigninPayload builds the payload for the Service1 signin endpoint from
|
||||
// CLI flags.
|
||||
func BuildSigninPayload(service1SigninUsername string, service1SigninPassword string) (*service1.SigninPayload, error) {
|
||||
var username string
|
||||
{
|
||||
username = service1SigninUsername
|
||||
}
|
||||
var password string
|
||||
{
|
||||
password = service1SigninPassword
|
||||
}
|
||||
v := &service1.SigninPayload{}
|
||||
v.Username = username
|
||||
v.Password = password
|
||||
|
||||
return v, nil
|
||||
}
|
||||
|
||||
// BuildSecurePayload builds the payload for the Service1 secure endpoint from
|
||||
// CLI flags.
|
||||
func BuildSecurePayload(service1SecureBody string, service1SecureToken string) (*service1.SecurePayload, error) {
|
||||
var err error
|
||||
var body SecureRequestBody
|
||||
{
|
||||
err = json.Unmarshal([]byte(service1SecureBody), &body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid JSON for body, \nerror: %s, \nexample of valid JSON:\n%s", err, "'{\n \"fail\": true\n }'")
|
||||
}
|
||||
}
|
||||
var token string
|
||||
{
|
||||
token = service1SecureToken
|
||||
}
|
||||
v := &service1.SecurePayload{
|
||||
Fail: body.Fail,
|
||||
}
|
||||
v.Token = token
|
||||
|
||||
return v, nil
|
||||
}
|
||||
102
go/goa_example/gen/http/service1/client/client.go
Normal file
102
go/goa_example/gen/http/service1/client/client.go
Normal file
@@ -0,0 +1,102 @@
|
||||
// Code generated by goa v3.6.0, DO NOT EDIT.
|
||||
//
|
||||
// Service1 client HTTP transport
|
||||
//
|
||||
// Command:
|
||||
// $ goa gen goa_example/design
|
||||
|
||||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
goahttp "goa.design/goa/v3/http"
|
||||
goa "goa.design/goa/v3/pkg"
|
||||
)
|
||||
|
||||
// Client lists the Service1 service endpoint HTTP clients.
|
||||
type Client struct {
|
||||
// Signin Doer is the HTTP client used to make requests to the signin endpoint.
|
||||
SigninDoer goahttp.Doer
|
||||
|
||||
// Secure Doer is the HTTP client used to make requests to the secure endpoint.
|
||||
SecureDoer goahttp.Doer
|
||||
|
||||
// RestoreResponseBody controls whether the response bodies are reset after
|
||||
// decoding so they can be read again.
|
||||
RestoreResponseBody bool
|
||||
|
||||
scheme string
|
||||
host string
|
||||
encoder func(*http.Request) goahttp.Encoder
|
||||
decoder func(*http.Response) goahttp.Decoder
|
||||
}
|
||||
|
||||
// NewClient instantiates HTTP clients for all the Service1 service servers.
|
||||
func NewClient(
|
||||
scheme string,
|
||||
host string,
|
||||
doer goahttp.Doer,
|
||||
enc func(*http.Request) goahttp.Encoder,
|
||||
dec func(*http.Response) goahttp.Decoder,
|
||||
restoreBody bool,
|
||||
) *Client {
|
||||
return &Client{
|
||||
SigninDoer: doer,
|
||||
SecureDoer: doer,
|
||||
RestoreResponseBody: restoreBody,
|
||||
scheme: scheme,
|
||||
host: host,
|
||||
decoder: dec,
|
||||
encoder: enc,
|
||||
}
|
||||
}
|
||||
|
||||
// Signin returns an endpoint that makes HTTP requests to the Service1 service
|
||||
// signin server.
|
||||
func (c *Client) Signin() goa.Endpoint {
|
||||
var (
|
||||
encodeRequest = EncodeSigninRequest(c.encoder)
|
||||
decodeResponse = DecodeSigninResponse(c.decoder, c.RestoreResponseBody)
|
||||
)
|
||||
return func(ctx context.Context, v interface{}) (interface{}, error) {
|
||||
req, err := c.BuildSigninRequest(ctx, v)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = encodeRequest(req, v)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := c.SigninDoer.Do(req)
|
||||
if err != nil {
|
||||
return nil, goahttp.ErrRequestError("Service1", "signin", err)
|
||||
}
|
||||
return decodeResponse(resp)
|
||||
}
|
||||
}
|
||||
|
||||
// Secure returns an endpoint that makes HTTP requests to the Service1 service
|
||||
// secure server.
|
||||
func (c *Client) Secure() goa.Endpoint {
|
||||
var (
|
||||
encodeRequest = EncodeSecureRequest(c.encoder)
|
||||
decodeResponse = DecodeSecureResponse(c.decoder, c.RestoreResponseBody)
|
||||
)
|
||||
return func(ctx context.Context, v interface{}) (interface{}, error) {
|
||||
req, err := c.BuildSecureRequest(ctx, v)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = encodeRequest(req, v)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := c.SecureDoer.Do(req)
|
||||
if err != nil {
|
||||
return nil, goahttp.ErrRequestError("Service1", "secure", err)
|
||||
}
|
||||
return decodeResponse(resp)
|
||||
}
|
||||
}
|
||||
188
go/goa_example/gen/http/service1/client/encode_decode.go
Normal file
188
go/goa_example/gen/http/service1/client/encode_decode.go
Normal file
@@ -0,0 +1,188 @@
|
||||
// Code generated by goa v3.6.0, DO NOT EDIT.
|
||||
//
|
||||
// Service1 HTTP client encoders and decoders
|
||||
//
|
||||
// Command:
|
||||
// $ goa gen goa_example/design
|
||||
|
||||
package client
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
service1 "goa_example/gen/service1"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
goahttp "goa.design/goa/v3/http"
|
||||
)
|
||||
|
||||
// BuildSigninRequest instantiates a HTTP request object with method and path
|
||||
// set to call the "Service1" service "signin" endpoint
|
||||
func (c *Client) BuildSigninRequest(ctx context.Context, v interface{}) (*http.Request, error) {
|
||||
u := &url.URL{Scheme: c.scheme, Host: c.host, Path: SigninService1Path()}
|
||||
req, err := http.NewRequest("POST", u.String(), nil)
|
||||
if err != nil {
|
||||
return nil, goahttp.ErrInvalidURL("Service1", "signin", u.String(), err)
|
||||
}
|
||||
if ctx != nil {
|
||||
req = req.WithContext(ctx)
|
||||
}
|
||||
|
||||
return req, nil
|
||||
}
|
||||
|
||||
// EncodeSigninRequest returns an encoder for requests sent to the Service1
|
||||
// signin server.
|
||||
func EncodeSigninRequest(encoder func(*http.Request) goahttp.Encoder) func(*http.Request, interface{}) error {
|
||||
return func(req *http.Request, v interface{}) error {
|
||||
p, ok := v.(*service1.SigninPayload)
|
||||
if !ok {
|
||||
return goahttp.ErrInvalidType("Service1", "signin", "*service1.SigninPayload", v)
|
||||
}
|
||||
req.SetBasicAuth(p.Username, p.Password)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// DecodeSigninResponse returns a decoder for responses returned by the
|
||||
// Service1 signin endpoint. restoreBody controls whether the response body
|
||||
// should be restored after having been read.
|
||||
// DecodeSigninResponse may return the following errors:
|
||||
// - "unauthorized" (type service1.Unauthorized): http.StatusUnauthorized
|
||||
// - error: internal error
|
||||
func DecodeSigninResponse(decoder func(*http.Response) goahttp.Decoder, restoreBody bool) func(*http.Response) (interface{}, error) {
|
||||
return func(resp *http.Response) (interface{}, error) {
|
||||
if restoreBody {
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp.Body = ioutil.NopCloser(bytes.NewBuffer(b))
|
||||
defer func() {
|
||||
resp.Body = ioutil.NopCloser(bytes.NewBuffer(b))
|
||||
}()
|
||||
} else {
|
||||
defer resp.Body.Close()
|
||||
}
|
||||
switch resp.StatusCode {
|
||||
case http.StatusOK:
|
||||
var (
|
||||
body SigninOKResponseBody
|
||||
err error
|
||||
)
|
||||
err = decoder(resp).Decode(&body)
|
||||
if err != nil {
|
||||
return nil, goahttp.ErrDecodingError("Service1", "signin", err)
|
||||
}
|
||||
err = ValidateSigninOKResponseBody(&body)
|
||||
if err != nil {
|
||||
return nil, goahttp.ErrValidationError("Service1", "signin", err)
|
||||
}
|
||||
res := NewSigninCredsOK(&body)
|
||||
return res, nil
|
||||
case http.StatusUnauthorized:
|
||||
var (
|
||||
body string
|
||||
err error
|
||||
)
|
||||
err = decoder(resp).Decode(&body)
|
||||
if err != nil {
|
||||
return nil, goahttp.ErrDecodingError("Service1", "signin", err)
|
||||
}
|
||||
return nil, NewSigninUnauthorized(body)
|
||||
default:
|
||||
body, _ := ioutil.ReadAll(resp.Body)
|
||||
return nil, goahttp.ErrInvalidResponse("Service1", "signin", resp.StatusCode, string(body))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// BuildSecureRequest instantiates a HTTP request object with method and path
|
||||
// set to call the "Service1" service "secure" endpoint
|
||||
func (c *Client) BuildSecureRequest(ctx context.Context, v interface{}) (*http.Request, error) {
|
||||
u := &url.URL{Scheme: c.scheme, Host: c.host, Path: SecureService1Path()}
|
||||
req, err := http.NewRequest("GET", u.String(), nil)
|
||||
if err != nil {
|
||||
return nil, goahttp.ErrInvalidURL("Service1", "secure", u.String(), err)
|
||||
}
|
||||
if ctx != nil {
|
||||
req = req.WithContext(ctx)
|
||||
}
|
||||
|
||||
return req, nil
|
||||
}
|
||||
|
||||
// EncodeSecureRequest returns an encoder for requests sent to the Service1
|
||||
// secure server.
|
||||
func EncodeSecureRequest(encoder func(*http.Request) goahttp.Encoder) func(*http.Request, interface{}) error {
|
||||
return func(req *http.Request, v interface{}) error {
|
||||
p, ok := v.(*service1.SecurePayload)
|
||||
if !ok {
|
||||
return goahttp.ErrInvalidType("Service1", "secure", "*service1.SecurePayload", v)
|
||||
}
|
||||
{
|
||||
head := p.Token
|
||||
if !strings.Contains(head, " ") {
|
||||
req.Header.Set("Authorization", "Bearer "+head)
|
||||
} else {
|
||||
req.Header.Set("Authorization", head)
|
||||
}
|
||||
}
|
||||
body := NewSecureRequestBody(p)
|
||||
if err := encoder(req).Encode(&body); err != nil {
|
||||
return goahttp.ErrEncodingError("Service1", "secure", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// DecodeSecureResponse returns a decoder for responses returned by the
|
||||
// Service1 secure endpoint. restoreBody controls whether the response body
|
||||
// should be restored after having been read.
|
||||
// DecodeSecureResponse may return the following errors:
|
||||
// - "unauthorized" (type service1.Unauthorized): http.StatusUnauthorized
|
||||
// - error: internal error
|
||||
func DecodeSecureResponse(decoder func(*http.Response) goahttp.Decoder, restoreBody bool) func(*http.Response) (interface{}, error) {
|
||||
return func(resp *http.Response) (interface{}, error) {
|
||||
if restoreBody {
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp.Body = ioutil.NopCloser(bytes.NewBuffer(b))
|
||||
defer func() {
|
||||
resp.Body = ioutil.NopCloser(bytes.NewBuffer(b))
|
||||
}()
|
||||
} else {
|
||||
defer resp.Body.Close()
|
||||
}
|
||||
switch resp.StatusCode {
|
||||
case http.StatusOK:
|
||||
var (
|
||||
body string
|
||||
err error
|
||||
)
|
||||
err = decoder(resp).Decode(&body)
|
||||
if err != nil {
|
||||
return nil, goahttp.ErrDecodingError("Service1", "secure", err)
|
||||
}
|
||||
return body, nil
|
||||
case http.StatusUnauthorized:
|
||||
var (
|
||||
body string
|
||||
err error
|
||||
)
|
||||
err = decoder(resp).Decode(&body)
|
||||
if err != nil {
|
||||
return nil, goahttp.ErrDecodingError("Service1", "secure", err)
|
||||
}
|
||||
return nil, NewSecureUnauthorized(body)
|
||||
default:
|
||||
body, _ := ioutil.ReadAll(resp.Body)
|
||||
return nil, goahttp.ErrInvalidResponse("Service1", "secure", resp.StatusCode, string(body))
|
||||
}
|
||||
}
|
||||
}
|
||||
18
go/goa_example/gen/http/service1/client/paths.go
Normal file
18
go/goa_example/gen/http/service1/client/paths.go
Normal file
@@ -0,0 +1,18 @@
|
||||
// Code generated by goa v3.6.0, DO NOT EDIT.
|
||||
//
|
||||
// HTTP request path constructors for the Service1 service.
|
||||
//
|
||||
// Command:
|
||||
// $ goa gen goa_example/design
|
||||
|
||||
package client
|
||||
|
||||
// SigninService1Path returns the URL path to the Service1 service signin HTTP endpoint.
|
||||
func SigninService1Path() string {
|
||||
return "/signin"
|
||||
}
|
||||
|
||||
// SecureService1Path returns the URL path to the Service1 service secure HTTP endpoint.
|
||||
func SecureService1Path() string {
|
||||
return "/secure"
|
||||
}
|
||||
109
go/goa_example/gen/http/service1/client/types.go
Normal file
109
go/goa_example/gen/http/service1/client/types.go
Normal file
@@ -0,0 +1,109 @@
|
||||
// Code generated by goa v3.6.0, DO NOT EDIT.
|
||||
//
|
||||
// Service1 HTTP client types
|
||||
//
|
||||
// Command:
|
||||
// $ goa gen goa_example/design
|
||||
|
||||
package client
|
||||
|
||||
import (
|
||||
service1 "goa_example/gen/service1"
|
||||
|
||||
goa "goa.design/goa/v3/pkg"
|
||||
)
|
||||
|
||||
// SecureRequestBody is the type of the "Service1" service "secure" endpoint
|
||||
// HTTP request body.
|
||||
type SecureRequestBody struct {
|
||||
// Whether to force auth failure even with a valid JWT
|
||||
Fail *bool `form:"fail,omitempty" json:"fail,omitempty" xml:"fail,omitempty"`
|
||||
}
|
||||
|
||||
// SigninOKResponseBody is the type of the "Service1" service "signin" endpoint
|
||||
// HTTP response body.
|
||||
type SigninOKResponseBody struct {
|
||||
// JWT token
|
||||
JWT *string `form:"jwt,omitempty" json:"jwt,omitempty" xml:"jwt,omitempty"`
|
||||
// API Key
|
||||
APIKey *string `form:"api_key,omitempty" json:"api_key,omitempty" xml:"api_key,omitempty"`
|
||||
// OAuth2 token
|
||||
OauthToken *string `form:"oauth_token,omitempty" json:"oauth_token,omitempty" xml:"oauth_token,omitempty"`
|
||||
}
|
||||
|
||||
// SigninBadRequestResponseBody is used to define fields on response body types.
|
||||
type SigninBadRequestResponseBody struct {
|
||||
// JWT token
|
||||
JWT *string `form:"jwt,omitempty" json:"jwt,omitempty" xml:"jwt,omitempty"`
|
||||
// API Key
|
||||
APIKey *string `form:"api_key,omitempty" json:"api_key,omitempty" xml:"api_key,omitempty"`
|
||||
// OAuth2 token
|
||||
OauthToken *string `form:"oauth_token,omitempty" json:"oauth_token,omitempty" xml:"oauth_token,omitempty"`
|
||||
}
|
||||
|
||||
// NewSecureRequestBody builds the HTTP request body from the payload of the
|
||||
// "secure" endpoint of the "Service1" service.
|
||||
func NewSecureRequestBody(p *service1.SecurePayload) *SecureRequestBody {
|
||||
body := &SecureRequestBody{
|
||||
Fail: p.Fail,
|
||||
}
|
||||
return body
|
||||
}
|
||||
|
||||
// NewSigninCredsOK builds a "Service1" service "signin" endpoint result from a
|
||||
// HTTP "OK" response.
|
||||
func NewSigninCredsOK(body *SigninOKResponseBody) *service1.Creds {
|
||||
v := &service1.Creds{
|
||||
JWT: *body.JWT,
|
||||
APIKey: *body.APIKey,
|
||||
OauthToken: *body.OauthToken,
|
||||
}
|
||||
|
||||
return v
|
||||
}
|
||||
|
||||
// NewSigninUnauthorized builds a Service1 service signin endpoint unauthorized
|
||||
// error.
|
||||
func NewSigninUnauthorized(body string) service1.Unauthorized {
|
||||
v := service1.Unauthorized(body)
|
||||
|
||||
return v
|
||||
}
|
||||
|
||||
// NewSecureUnauthorized builds a Service1 service secure endpoint unauthorized
|
||||
// error.
|
||||
func NewSecureUnauthorized(body string) service1.Unauthorized {
|
||||
v := service1.Unauthorized(body)
|
||||
|
||||
return v
|
||||
}
|
||||
|
||||
// ValidateSigninOKResponseBody runs the validations defined on
|
||||
// SigninOKResponseBody
|
||||
func ValidateSigninOKResponseBody(body *SigninOKResponseBody) (err error) {
|
||||
if body.JWT == nil {
|
||||
err = goa.MergeErrors(err, goa.MissingFieldError("jwt", "body"))
|
||||
}
|
||||
if body.APIKey == nil {
|
||||
err = goa.MergeErrors(err, goa.MissingFieldError("api_key", "body"))
|
||||
}
|
||||
if body.OauthToken == nil {
|
||||
err = goa.MergeErrors(err, goa.MissingFieldError("oauth_token", "body"))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// ValidateSigninBadRequestResponseBody runs the validations defined on
|
||||
// SigninBad RequestResponseBody
|
||||
func ValidateSigninBadRequestResponseBody(body *SigninBadRequestResponseBody) (err error) {
|
||||
if body.JWT == nil {
|
||||
err = goa.MergeErrors(err, goa.MissingFieldError("jwt", "body"))
|
||||
}
|
||||
if body.APIKey == nil {
|
||||
err = goa.MergeErrors(err, goa.MissingFieldError("api_key", "body"))
|
||||
}
|
||||
if body.OauthToken == nil {
|
||||
err = goa.MergeErrors(err, goa.MissingFieldError("oauth_token", "body"))
|
||||
}
|
||||
return
|
||||
}
|
||||
143
go/goa_example/gen/http/service1/server/encode_decode.go
Normal file
143
go/goa_example/gen/http/service1/server/encode_decode.go
Normal file
@@ -0,0 +1,143 @@
|
||||
// Code generated by goa v3.6.0, DO NOT EDIT.
|
||||
//
|
||||
// Service1 HTTP server encoders and decoders
|
||||
//
|
||||
// Command:
|
||||
// $ goa gen goa_example/design
|
||||
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
service1 "goa_example/gen/service1"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
goahttp "goa.design/goa/v3/http"
|
||||
goa "goa.design/goa/v3/pkg"
|
||||
)
|
||||
|
||||
// EncodeSigninResponse returns an encoder for responses returned by the
|
||||
// Service1 signin endpoint.
|
||||
func EncodeSigninResponse(encoder func(context.Context, http.ResponseWriter) goahttp.Encoder) func(context.Context, http.ResponseWriter, interface{}) error {
|
||||
return func(ctx context.Context, w http.ResponseWriter, v interface{}) error {
|
||||
res, _ := v.(*service1.Creds)
|
||||
enc := encoder(ctx, w)
|
||||
body := NewSigninOKResponseBody(res)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
return enc.Encode(body)
|
||||
}
|
||||
}
|
||||
|
||||
// DecodeSigninRequest returns a decoder for requests sent to the Service1
|
||||
// signin endpoint.
|
||||
func DecodeSigninRequest(mux goahttp.Muxer, decoder func(*http.Request) goahttp.Decoder) func(*http.Request) (interface{}, error) {
|
||||
return func(r *http.Request) (interface{}, error) {
|
||||
payload := NewSigninPayload()
|
||||
user, pass, ok := r.BasicAuth()
|
||||
if !ok {
|
||||
return nil, goa.MissingFieldError("Authorization", "header")
|
||||
}
|
||||
payload.Username = user
|
||||
payload.Password = pass
|
||||
|
||||
return payload, nil
|
||||
}
|
||||
}
|
||||
|
||||
// EncodeSigninError returns an encoder for errors returned by the signin
|
||||
// Service1 endpoint.
|
||||
func EncodeSigninError(encoder func(context.Context, http.ResponseWriter) goahttp.Encoder, formatter func(err error) goahttp.Statuser) func(context.Context, http.ResponseWriter, error) error {
|
||||
encodeError := goahttp.ErrorEncoder(encoder, formatter)
|
||||
return func(ctx context.Context, w http.ResponseWriter, v error) error {
|
||||
var en ErrorNamer
|
||||
if !errors.As(v, &en) {
|
||||
return encodeError(ctx, w, v)
|
||||
}
|
||||
switch en.ErrorName() {
|
||||
case "unauthorized":
|
||||
res := v.(service1.Unauthorized)
|
||||
enc := encoder(ctx, w)
|
||||
body := res
|
||||
w.Header().Set("goa-error", res.ErrorName())
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return enc.Encode(body)
|
||||
default:
|
||||
return encodeError(ctx, w, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// EncodeSecureResponse returns an encoder for responses returned by the
|
||||
// Service1 secure endpoint.
|
||||
func EncodeSecureResponse(encoder func(context.Context, http.ResponseWriter) goahttp.Encoder) func(context.Context, http.ResponseWriter, interface{}) error {
|
||||
return func(ctx context.Context, w http.ResponseWriter, v interface{}) error {
|
||||
res, _ := v.(string)
|
||||
enc := encoder(ctx, w)
|
||||
body := res
|
||||
w.WriteHeader(http.StatusOK)
|
||||
return enc.Encode(body)
|
||||
}
|
||||
}
|
||||
|
||||
// DecodeSecureRequest returns a decoder for requests sent to the Service1
|
||||
// secure endpoint.
|
||||
func DecodeSecureRequest(mux goahttp.Muxer, decoder func(*http.Request) goahttp.Decoder) func(*http.Request) (interface{}, error) {
|
||||
return func(r *http.Request) (interface{}, error) {
|
||||
var (
|
||||
body SecureRequestBody
|
||||
err error
|
||||
)
|
||||
err = decoder(r).Decode(&body)
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
return nil, goa.MissingPayloadError()
|
||||
}
|
||||
return nil, goa.DecodePayloadError(err.Error())
|
||||
}
|
||||
|
||||
var (
|
||||
token string
|
||||
)
|
||||
token = r.Header.Get("Authorization")
|
||||
if token == "" {
|
||||
err = goa.MergeErrors(err, goa.MissingFieldError("Authorization", "header"))
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
payload := NewSecurePayload(&body, 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
|
||||
}
|
||||
}
|
||||
|
||||
// EncodeSecureError returns an encoder for errors returned by the secure
|
||||
// Service1 endpoint.
|
||||
func EncodeSecureError(encoder func(context.Context, http.ResponseWriter) goahttp.Encoder, formatter func(err error) goahttp.Statuser) func(context.Context, http.ResponseWriter, error) error {
|
||||
encodeError := goahttp.ErrorEncoder(encoder, formatter)
|
||||
return func(ctx context.Context, w http.ResponseWriter, v error) error {
|
||||
var en ErrorNamer
|
||||
if !errors.As(v, &en) {
|
||||
return encodeError(ctx, w, v)
|
||||
}
|
||||
switch en.ErrorName() {
|
||||
case "unauthorized":
|
||||
res := v.(service1.Unauthorized)
|
||||
enc := encoder(ctx, w)
|
||||
body := res
|
||||
w.Header().Set("goa-error", res.ErrorName())
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return enc.Encode(body)
|
||||
default:
|
||||
return encodeError(ctx, w, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
18
go/goa_example/gen/http/service1/server/paths.go
Normal file
18
go/goa_example/gen/http/service1/server/paths.go
Normal file
@@ -0,0 +1,18 @@
|
||||
// Code generated by goa v3.6.0, DO NOT EDIT.
|
||||
//
|
||||
// HTTP request path constructors for the Service1 service.
|
||||
//
|
||||
// Command:
|
||||
// $ goa gen goa_example/design
|
||||
|
||||
package server
|
||||
|
||||
// SigninService1Path returns the URL path to the Service1 service signin HTTP endpoint.
|
||||
func SigninService1Path() string {
|
||||
return "/signin"
|
||||
}
|
||||
|
||||
// SecureService1Path returns the URL path to the Service1 service secure HTTP endpoint.
|
||||
func SecureService1Path() string {
|
||||
return "/secure"
|
||||
}
|
||||
187
go/goa_example/gen/http/service1/server/server.go
Normal file
187
go/goa_example/gen/http/service1/server/server.go
Normal file
@@ -0,0 +1,187 @@
|
||||
// Code generated by goa v3.6.0, DO NOT EDIT.
|
||||
//
|
||||
// Service1 HTTP server
|
||||
//
|
||||
// Command:
|
||||
// $ goa gen goa_example/design
|
||||
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
service1 "goa_example/gen/service1"
|
||||
"net/http"
|
||||
|
||||
goahttp "goa.design/goa/v3/http"
|
||||
goa "goa.design/goa/v3/pkg"
|
||||
)
|
||||
|
||||
// Server lists the Service1 service endpoint HTTP handlers.
|
||||
type Server struct {
|
||||
Mounts []*MountPoint
|
||||
Signin http.Handler
|
||||
Secure http.Handler
|
||||
}
|
||||
|
||||
// ErrorNamer is an interface implemented by generated error structs that
|
||||
// exposes the name of the error as defined in the design.
|
||||
type ErrorNamer interface {
|
||||
ErrorName() string
|
||||
}
|
||||
|
||||
// MountPoint holds information about the mounted endpoints.
|
||||
type MountPoint struct {
|
||||
// Method is the name of the service method served by the mounted HTTP handler.
|
||||
Method string
|
||||
// Verb is the HTTP method used to match requests to the mounted handler.
|
||||
Verb string
|
||||
// Pattern is the HTTP request path pattern used to match requests to the
|
||||
// mounted handler.
|
||||
Pattern string
|
||||
}
|
||||
|
||||
// New instantiates HTTP handlers for all the Service1 service endpoints using
|
||||
// the provided encoder and decoder. The handlers are mounted on the given mux
|
||||
// using the HTTP verb and path defined in the design. errhandler is called
|
||||
// whenever a response fails to be encoded. formatter is used to format errors
|
||||
// returned by the service methods prior to encoding. Both errhandler and
|
||||
// formatter are optional and can be nil.
|
||||
func New(
|
||||
e *service1.Endpoints,
|
||||
mux goahttp.Muxer,
|
||||
decoder func(*http.Request) goahttp.Decoder,
|
||||
encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
|
||||
errhandler func(context.Context, http.ResponseWriter, error),
|
||||
formatter func(err error) goahttp.Statuser,
|
||||
) *Server {
|
||||
return &Server{
|
||||
Mounts: []*MountPoint{
|
||||
{"Signin", "POST", "/signin"},
|
||||
{"Secure", "GET", "/secure"},
|
||||
},
|
||||
Signin: NewSigninHandler(e.Signin, mux, decoder, encoder, errhandler, formatter),
|
||||
Secure: NewSecureHandler(e.Secure, mux, decoder, encoder, errhandler, formatter),
|
||||
}
|
||||
}
|
||||
|
||||
// Service returns the name of the service served.
|
||||
func (s *Server) Service() string { return "Service1" }
|
||||
|
||||
// Use wraps the server handlers with the given middleware.
|
||||
func (s *Server) Use(m func(http.Handler) http.Handler) {
|
||||
s.Signin = m(s.Signin)
|
||||
s.Secure = m(s.Secure)
|
||||
}
|
||||
|
||||
// Mount configures the mux to serve the Service1 endpoints.
|
||||
func Mount(mux goahttp.Muxer, h *Server) {
|
||||
MountSigninHandler(mux, h.Signin)
|
||||
MountSecureHandler(mux, h.Secure)
|
||||
}
|
||||
|
||||
// Mount configures the mux to serve the Service1 endpoints.
|
||||
func (s *Server) Mount(mux goahttp.Muxer) {
|
||||
Mount(mux, s)
|
||||
}
|
||||
|
||||
// MountSigninHandler configures the mux to serve the "Service1" service
|
||||
// "signin" endpoint.
|
||||
func MountSigninHandler(mux goahttp.Muxer, h http.Handler) {
|
||||
f, ok := h.(http.HandlerFunc)
|
||||
if !ok {
|
||||
f = func(w http.ResponseWriter, r *http.Request) {
|
||||
h.ServeHTTP(w, r)
|
||||
}
|
||||
}
|
||||
mux.Handle("POST", "/signin", f)
|
||||
}
|
||||
|
||||
// NewSigninHandler creates a HTTP handler which loads the HTTP request and
|
||||
// calls the "Service1" service "signin" endpoint.
|
||||
func NewSigninHandler(
|
||||
endpoint goa.Endpoint,
|
||||
mux goahttp.Muxer,
|
||||
decoder func(*http.Request) goahttp.Decoder,
|
||||
encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
|
||||
errhandler func(context.Context, http.ResponseWriter, error),
|
||||
formatter func(err error) goahttp.Statuser,
|
||||
) http.Handler {
|
||||
var (
|
||||
decodeRequest = DecodeSigninRequest(mux, decoder)
|
||||
encodeResponse = EncodeSigninResponse(encoder)
|
||||
encodeError = EncodeSigninError(encoder, formatter)
|
||||
)
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
|
||||
ctx = context.WithValue(ctx, goa.MethodKey, "signin")
|
||||
ctx = context.WithValue(ctx, goa.ServiceKey, "Service1")
|
||||
payload, err := decodeRequest(r)
|
||||
if err != nil {
|
||||
if err := encodeError(ctx, w, err); err != nil {
|
||||
errhandler(ctx, w, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
res, err := endpoint(ctx, payload)
|
||||
if err != nil {
|
||||
if err := encodeError(ctx, w, err); err != nil {
|
||||
errhandler(ctx, w, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err := encodeResponse(ctx, w, res); err != nil {
|
||||
errhandler(ctx, w, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// MountSecureHandler configures the mux to serve the "Service1" service
|
||||
// "secure" endpoint.
|
||||
func MountSecureHandler(mux goahttp.Muxer, h http.Handler) {
|
||||
f, ok := h.(http.HandlerFunc)
|
||||
if !ok {
|
||||
f = func(w http.ResponseWriter, r *http.Request) {
|
||||
h.ServeHTTP(w, r)
|
||||
}
|
||||
}
|
||||
mux.Handle("GET", "/secure", f)
|
||||
}
|
||||
|
||||
// NewSecureHandler creates a HTTP handler which loads the HTTP request and
|
||||
// calls the "Service1" service "secure" endpoint.
|
||||
func NewSecureHandler(
|
||||
endpoint goa.Endpoint,
|
||||
mux goahttp.Muxer,
|
||||
decoder func(*http.Request) goahttp.Decoder,
|
||||
encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
|
||||
errhandler func(context.Context, http.ResponseWriter, error),
|
||||
formatter func(err error) goahttp.Statuser,
|
||||
) http.Handler {
|
||||
var (
|
||||
decodeRequest = DecodeSecureRequest(mux, decoder)
|
||||
encodeResponse = EncodeSecureResponse(encoder)
|
||||
encodeError = EncodeSecureError(encoder, formatter)
|
||||
)
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
|
||||
ctx = context.WithValue(ctx, goa.MethodKey, "secure")
|
||||
ctx = context.WithValue(ctx, goa.ServiceKey, "Service1")
|
||||
payload, err := decodeRequest(r)
|
||||
if err != nil {
|
||||
if err := encodeError(ctx, w, err); err != nil {
|
||||
errhandler(ctx, w, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
res, err := endpoint(ctx, payload)
|
||||
if err != nil {
|
||||
if err := encodeError(ctx, w, err); err != nil {
|
||||
errhandler(ctx, w, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err := encodeResponse(ctx, w, res); err != nil {
|
||||
errhandler(ctx, w, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
58
go/goa_example/gen/http/service1/server/types.go
Normal file
58
go/goa_example/gen/http/service1/server/types.go
Normal file
@@ -0,0 +1,58 @@
|
||||
// Code generated by goa v3.6.0, DO NOT EDIT.
|
||||
//
|
||||
// Service1 HTTP server types
|
||||
//
|
||||
// Command:
|
||||
// $ goa gen goa_example/design
|
||||
|
||||
package server
|
||||
|
||||
import (
|
||||
service1 "goa_example/gen/service1"
|
||||
)
|
||||
|
||||
// SecureRequestBody is the type of the "Service1" service "secure" endpoint
|
||||
// HTTP request body.
|
||||
type SecureRequestBody struct {
|
||||
// Whether to force auth failure even with a valid JWT
|
||||
Fail *bool `form:"fail,omitempty" json:"fail,omitempty" xml:"fail,omitempty"`
|
||||
}
|
||||
|
||||
// SigninOKResponseBody is the type of the "Service1" service "signin" endpoint
|
||||
// HTTP response body.
|
||||
type SigninOKResponseBody struct {
|
||||
// JWT token
|
||||
JWT string `form:"jwt" json:"jwt" xml:"jwt"`
|
||||
// API Key
|
||||
APIKey string `form:"api_key" json:"api_key" xml:"api_key"`
|
||||
// OAuth2 token
|
||||
OauthToken string `form:"oauth_token" json:"oauth_token" xml:"oauth_token"`
|
||||
}
|
||||
|
||||
// NewSigninOKResponseBody builds the HTTP response body from the result of the
|
||||
// "signin" endpoint of the "Service1" service.
|
||||
func NewSigninOKResponseBody(res *service1.Creds) *SigninOKResponseBody {
|
||||
body := &SigninOKResponseBody{
|
||||
JWT: res.JWT,
|
||||
APIKey: res.APIKey,
|
||||
OauthToken: res.OauthToken,
|
||||
}
|
||||
return body
|
||||
}
|
||||
|
||||
// NewSigninPayload builds a Service1 service signin endpoint payload.
|
||||
func NewSigninPayload() *service1.SigninPayload {
|
||||
v := &service1.SigninPayload{}
|
||||
|
||||
return v
|
||||
}
|
||||
|
||||
// NewSecurePayload builds a Service1 service secure endpoint payload.
|
||||
func NewSecurePayload(body *SecureRequestBody, token string) *service1.SecurePayload {
|
||||
v := &service1.SecurePayload{
|
||||
Fail: body.Fail,
|
||||
}
|
||||
v.Token = token
|
||||
|
||||
return v
|
||||
}
|
||||
48
go/goa_example/gen/service1/client.go
Normal file
48
go/goa_example/gen/service1/client.go
Normal file
@@ -0,0 +1,48 @@
|
||||
// Code generated by goa v3.6.0, DO NOT EDIT.
|
||||
//
|
||||
// Service1 client
|
||||
//
|
||||
// Command:
|
||||
// $ goa gen goa_example/design
|
||||
|
||||
package service1
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
goa "goa.design/goa/v3/pkg"
|
||||
)
|
||||
|
||||
// Client is the "Service1" service client.
|
||||
type Client struct {
|
||||
SigninEndpoint goa.Endpoint
|
||||
SecureEndpoint goa.Endpoint
|
||||
}
|
||||
|
||||
// NewClient initializes a "Service1" service client given the endpoints.
|
||||
func NewClient(signin, secure goa.Endpoint) *Client {
|
||||
return &Client{
|
||||
SigninEndpoint: signin,
|
||||
SecureEndpoint: secure,
|
||||
}
|
||||
}
|
||||
|
||||
// Signin calls the "signin" endpoint of the "Service1" service.
|
||||
func (c *Client) Signin(ctx context.Context, p *SigninPayload) (res *Creds, err error) {
|
||||
var ires interface{}
|
||||
ires, err = c.SigninEndpoint(ctx, p)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return ires.(*Creds), nil
|
||||
}
|
||||
|
||||
// Secure calls the "secure" endpoint of the "Service1" service.
|
||||
func (c *Client) Secure(ctx context.Context, p *SecurePayload) (res string, err error) {
|
||||
var ires interface{}
|
||||
ires, err = c.SecureEndpoint(ctx, p)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return ires.(string), nil
|
||||
}
|
||||
75
go/goa_example/gen/service1/endpoints.go
Normal file
75
go/goa_example/gen/service1/endpoints.go
Normal file
@@ -0,0 +1,75 @@
|
||||
// Code generated by goa v3.6.0, DO NOT EDIT.
|
||||
//
|
||||
// Service1 endpoints
|
||||
//
|
||||
// Command:
|
||||
// $ goa gen goa_example/design
|
||||
|
||||
package service1
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
goa "goa.design/goa/v3/pkg"
|
||||
"goa.design/goa/v3/security"
|
||||
)
|
||||
|
||||
// Endpoints wraps the "Service1" service endpoints.
|
||||
type Endpoints struct {
|
||||
Signin goa.Endpoint
|
||||
Secure goa.Endpoint
|
||||
}
|
||||
|
||||
// NewEndpoints wraps the methods of the "Service1" service with endpoints.
|
||||
func NewEndpoints(s Service) *Endpoints {
|
||||
// Casting service to Auther interface
|
||||
a := s.(Auther)
|
||||
return &Endpoints{
|
||||
Signin: NewSigninEndpoint(s, a.BasicAuth),
|
||||
Secure: NewSecureEndpoint(s, a.JWTAuth),
|
||||
}
|
||||
}
|
||||
|
||||
// Use applies the given middleware to all the "Service1" service endpoints.
|
||||
func (e *Endpoints) Use(m func(goa.Endpoint) goa.Endpoint) {
|
||||
e.Signin = m(e.Signin)
|
||||
e.Secure = m(e.Secure)
|
||||
}
|
||||
|
||||
// NewSigninEndpoint returns an endpoint function that calls the method
|
||||
// "signin" of service "Service1".
|
||||
func NewSigninEndpoint(s Service, authBasicFn security.AuthBasicFunc) goa.Endpoint {
|
||||
return func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
p := req.(*SigninPayload)
|
||||
var err error
|
||||
sc := security.BasicScheme{
|
||||
Name: "basic",
|
||||
Scopes: []string{},
|
||||
RequiredScopes: []string{},
|
||||
}
|
||||
ctx, err = authBasicFn(ctx, p.Username, p.Password, &sc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.Signin(ctx, p)
|
||||
}
|
||||
}
|
||||
|
||||
// NewSecureEndpoint returns an endpoint function that calls the method
|
||||
// "secure" of service "Service1".
|
||||
func NewSecureEndpoint(s Service, authJWTFn security.AuthJWTFunc) goa.Endpoint {
|
||||
return func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
p := req.(*SecurePayload)
|
||||
var err error
|
||||
sc := security.JWTScheme{
|
||||
Name: "jwt",
|
||||
Scopes: []string{},
|
||||
RequiredScopes: []string{},
|
||||
}
|
||||
ctx, err = authJWTFn(ctx, p.Token, &sc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.Secure(ctx, p)
|
||||
}
|
||||
}
|
||||
80
go/goa_example/gen/service1/service.go
Normal file
80
go/goa_example/gen/service1/service.go
Normal file
@@ -0,0 +1,80 @@
|
||||
// Code generated by goa v3.6.0, DO NOT EDIT.
|
||||
//
|
||||
// Service1 service
|
||||
//
|
||||
// Command:
|
||||
// $ goa gen goa_example/design
|
||||
|
||||
package service1
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"goa.design/goa/v3/security"
|
||||
)
|
||||
|
||||
// The secured service exposes endpoints that require valid authorization
|
||||
// credentials.
|
||||
type Service interface {
|
||||
// Signin implements signin.
|
||||
Signin(context.Context, *SigninPayload) (res *Creds, err error)
|
||||
// 这是一个需要JWT认证的接口
|
||||
Secure(context.Context, *SecurePayload) (res string, err error)
|
||||
}
|
||||
|
||||
// Auther defines the authorization functions to be implemented by the service.
|
||||
type Auther interface {
|
||||
// BasicAuth implements the authorization logic for the Basic security scheme.
|
||||
BasicAuth(ctx context.Context, user, pass string, schema *security.BasicScheme) (context.Context, error)
|
||||
// JWTAuth implements the authorization logic for the JWT security scheme.
|
||||
JWTAuth(ctx context.Context, token string, schema *security.JWTScheme) (context.Context, error)
|
||||
}
|
||||
|
||||
// ServiceName is the name of the service as defined in the design. This is the
|
||||
// same value that is set in the endpoint request contexts under the ServiceKey
|
||||
// key.
|
||||
const ServiceName = "Service1"
|
||||
|
||||
// MethodNames lists the service method names as defined in the design. These
|
||||
// are the same values that are set in the endpoint request contexts under the
|
||||
// MethodKey key.
|
||||
var MethodNames = [2]string{"signin", "secure"}
|
||||
|
||||
// Creds is the result type of the Service1 service signin method.
|
||||
type Creds struct {
|
||||
// JWT token
|
||||
JWT string
|
||||
// API Key
|
||||
APIKey string
|
||||
// OAuth2 token
|
||||
OauthToken string
|
||||
}
|
||||
|
||||
// SecurePayload is the payload type of the Service1 service secure method.
|
||||
type SecurePayload struct {
|
||||
// Whether to force auth failure even with a valid JWT
|
||||
Fail *bool
|
||||
// JWT used for authentication
|
||||
Token string
|
||||
}
|
||||
|
||||
// Credentials used to authenticate to retrieve JWT token
|
||||
type SigninPayload struct {
|
||||
// Username used to perform signin
|
||||
Username string
|
||||
// Password used to perform signin
|
||||
Password string
|
||||
}
|
||||
|
||||
// Credentials are invalid
|
||||
type Unauthorized string
|
||||
|
||||
// Error returns an error description.
|
||||
func (e Unauthorized) Error() string {
|
||||
return "Credentials are invalid"
|
||||
}
|
||||
|
||||
// ErrorName returns "unauthorized".
|
||||
func (e Unauthorized) ErrorName() string {
|
||||
return "unauthorized"
|
||||
}
|
||||
Reference in New Issue
Block a user