/
server.go
158 lines (144 loc) · 5.85 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Code generated by goa v2.2.4, DO NOT EDIT.
//
// secured_service gRPC server
//
// Command:
// $ goa gen goa.design/examples/security/design -o
// $(GOPATH)/src/goa.design/examples/security
package server
import (
"context"
secured_servicepb "goa.design/examples/security/gen/grpc/secured_service/pb"
securedservice "goa.design/examples/security/gen/secured_service"
"goa.design/goa"
goagrpc "goa.design/goa/grpc"
"google.golang.org/grpc/codes"
)
// Server implements the secured_servicepb.SecuredServiceServer interface.
type Server struct {
SigninH goagrpc.UnaryHandler
SecureH goagrpc.UnaryHandler
DoublySecureH goagrpc.UnaryHandler
AlsoDoublySecureH goagrpc.UnaryHandler
}
// 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 secured_service service
// endpoints.
func New(e *securedservice.Endpoints, uh goagrpc.UnaryHandler) *Server {
return &Server{
SigninH: NewSigninHandler(e.Signin, uh),
SecureH: NewSecureHandler(e.Secure, uh),
DoublySecureH: NewDoublySecureHandler(e.DoublySecure, uh),
AlsoDoublySecureH: NewAlsoDoublySecureHandler(e.AlsoDoublySecure, uh),
}
}
// NewSigninHandler creates a gRPC handler which serves the "secured_service"
// 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
// secured_servicepb.SecuredServiceServer interface.
func (s *Server) Signin(ctx context.Context, message *secured_servicepb.SigninRequest) (*secured_servicepb.SigninResponse, error) {
ctx = context.WithValue(ctx, goa.MethodKey, "signin")
ctx = context.WithValue(ctx, goa.ServiceKey, "secured_service")
resp, err := s.SigninH.Handle(ctx, message)
if err != nil {
if en, ok := err.(ErrorNamer); ok {
switch en.ErrorName() {
case "unauthorized":
return nil, goagrpc.NewStatusError(codes.Unauthenticated, err, goagrpc.NewErrorResponse(err))
}
}
return nil, goagrpc.EncodeError(err)
}
return resp.(*secured_servicepb.SigninResponse), nil
}
// NewSecureHandler creates a gRPC handler which serves the "secured_service"
// 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
// secured_servicepb.SecuredServiceServer interface.
func (s *Server) Secure(ctx context.Context, message *secured_servicepb.SecureRequest) (*secured_servicepb.SecureResponse, error) {
ctx = context.WithValue(ctx, goa.MethodKey, "secure")
ctx = context.WithValue(ctx, goa.ServiceKey, "secured_service")
resp, err := s.SecureH.Handle(ctx, message)
if err != nil {
if en, ok := err.(ErrorNamer); ok {
switch en.ErrorName() {
case "invalid-scopes":
return nil, goagrpc.NewStatusError(codes.Unauthenticated, err, goagrpc.NewErrorResponse(err))
case "unauthorized":
return nil, goagrpc.NewStatusError(codes.Unauthenticated, err, goagrpc.NewErrorResponse(err))
}
}
return nil, goagrpc.EncodeError(err)
}
return resp.(*secured_servicepb.SecureResponse), nil
}
// NewDoublySecureHandler creates a gRPC handler which serves the
// "secured_service" service "doubly_secure" endpoint.
func NewDoublySecureHandler(endpoint goa.Endpoint, h goagrpc.UnaryHandler) goagrpc.UnaryHandler {
if h == nil {
h = goagrpc.NewUnaryHandler(endpoint, DecodeDoublySecureRequest, EncodeDoublySecureResponse)
}
return h
}
// DoublySecure implements the "DoublySecure" method in
// secured_servicepb.SecuredServiceServer interface.
func (s *Server) DoublySecure(ctx context.Context, message *secured_servicepb.DoublySecureRequest) (*secured_servicepb.DoublySecureResponse, error) {
ctx = context.WithValue(ctx, goa.MethodKey, "doubly_secure")
ctx = context.WithValue(ctx, goa.ServiceKey, "secured_service")
resp, err := s.DoublySecureH.Handle(ctx, message)
if err != nil {
if en, ok := err.(ErrorNamer); ok {
switch en.ErrorName() {
case "invalid-scopes":
return nil, goagrpc.NewStatusError(codes.Unauthenticated, err, goagrpc.NewErrorResponse(err))
case "unauthorized":
return nil, goagrpc.NewStatusError(codes.Unauthenticated, err, goagrpc.NewErrorResponse(err))
}
}
return nil, goagrpc.EncodeError(err)
}
return resp.(*secured_servicepb.DoublySecureResponse), nil
}
// NewAlsoDoublySecureHandler creates a gRPC handler which serves the
// "secured_service" service "also_doubly_secure" endpoint.
func NewAlsoDoublySecureHandler(endpoint goa.Endpoint, h goagrpc.UnaryHandler) goagrpc.UnaryHandler {
if h == nil {
h = goagrpc.NewUnaryHandler(endpoint, DecodeAlsoDoublySecureRequest, EncodeAlsoDoublySecureResponse)
}
return h
}
// AlsoDoublySecure implements the "AlsoDoublySecure" method in
// secured_servicepb.SecuredServiceServer interface.
func (s *Server) AlsoDoublySecure(ctx context.Context, message *secured_servicepb.AlsoDoublySecureRequest) (*secured_servicepb.AlsoDoublySecureResponse, error) {
ctx = context.WithValue(ctx, goa.MethodKey, "also_doubly_secure")
ctx = context.WithValue(ctx, goa.ServiceKey, "secured_service")
resp, err := s.AlsoDoublySecureH.Handle(ctx, message)
if err != nil {
if en, ok := err.(ErrorNamer); ok {
switch en.ErrorName() {
case "invalid-scopes":
return nil, goagrpc.NewStatusError(codes.Unauthenticated, err, goagrpc.NewErrorResponse(err))
case "unauthorized":
return nil, goagrpc.NewStatusError(codes.Unauthenticated, err, goagrpc.NewErrorResponse(err))
}
}
return nil, goagrpc.EncodeError(err)
}
return resp.(*secured_servicepb.AlsoDoublySecureResponse), nil
}