add goa examples
This commit is contained in:
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