forked from webrpc/webrpc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.gen.go
649 lines (555 loc) · 16.6 KB
/
server.gen.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
// example v0.0.1 hi 33aa93c6d912046df938c7f94cef36d3a30679fa
// --
// This file has been generated by https://github.com/webrpc/webrpc using gen/golang
// Do not edit by hand. Update your webrpc schema and re-generate.
package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
)
// WebRPC description and code-gen version
func WebRPCVersion() string {
return "v1"
}
// Schema version of your RIDL schema
func WebRPCSchemaVersion() string {
return " v0.0.1"
}
//
// Types
//
type Kind uint32
var (
Kind_USER Kind = 1
Kind_ADMIN Kind = 2
)
var Kind_name = map[uint32]string{
1: "USER",
2: "ADMIN",
}
var Kind_value = map[string]uint32{
"USER": 1,
"ADMIN": 2,
}
func (x Kind) String() string {
return Kind_name[uint32(x)]
}
func (x Kind) MarshalJSON() ([]byte, error) {
buf := bytes.NewBufferString(`"`)
buf.WriteString(Kind_name[uint32(x)])
buf.WriteString(`"`)
return buf.Bytes(), nil
}
func (x *Kind) UnmarshalJSON(b []byte) error {
var j string
err := json.Unmarshal(b, &j)
if err != nil {
return err
}
*x = Kind(Kind_value[j])
return nil
}
type Empty struct {
}
type GetUserRequest struct {
UserID uint64 `json:"userID"`
}
type User struct {
ID uint64 `json:"id" db:"id"`
Username string `json:"USERNAME" db:"username"`
CreatedAt *time.Time `json:"created_at,omitempty" db:"created_at"`
}
type RandomStuff struct {
Meta map[string]interface{} `json:"meta"`
MetaNestedExample map[string]map[string]uint32 `json:"metaNestedExample"`
NamesList []string `json:"namesList"`
NumsList []int64 `json:"numsList"`
DoubleArray [][]string `json:"doubleArray"`
ListOfMaps []map[string]uint32 `json:"listOfMaps"`
ListOfUsers []*User `json:"listOfUsers"`
MapOfUsers map[string]*User `json:"mapOfUsers"`
User *User `json:"user"`
}
type ExampleService interface {
Ping(ctx context.Context) (bool, error)
GetUser(ctx context.Context, req *GetUserRequest) (*User, error)
}
var WebRPCServices = map[string][]string{
"ExampleService": {
"Ping",
"GetUser",
},
}
//
// Server
//
type WebRPCServer interface {
http.Handler
}
type ExampleServiceServer interface {
ExampleService
}
type exampleServiceServer struct {
service ExampleServiceServer
}
func NewExampleServiceServer(svc ExampleServiceServer) WebRPCServer {
return &exampleServiceServer{
service: svc,
}
}
func (s *exampleServiceServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
ctx = context.WithValue(ctx, HTTPResponseWriterCtxKey, w)
ctx = context.WithValue(ctx, HTTPRequestCtxKey, r)
ctx = context.WithValue(ctx, ServiceNameCtxKey, "ExampleService")
if r.Method != "POST" {
RespondWithError(w, Errorf(ErrBadRoute, nil, "unsupported method %q (only POST is allowed)", r.Method))
return
}
if !strings.HasPrefix(r.Header.Get("Content-Type"), "application/json") {
RespondWithError(w, Errorf(ErrBadRoute, nil, "unexpected Content-Type: %q", r.Header.Get("Content-Type")))
return
}
switch r.URL.Path {
case "/rpc/ExampleService/Ping":
s.servePing(ctx, w, r)
return
case "/rpc/ExampleService/GetUser":
s.serveGetUser(ctx, w, r)
return
default:
RespondWithError(w, Errorf(ErrBadRoute, nil, "no handler for path %q", r.URL.Path))
return
}
}
func (s *exampleServiceServer) servePing(ctx context.Context, w http.ResponseWriter, r *http.Request) {
var err error
ctx = context.WithValue(ctx, MethodNameCtxKey, "Ping")
// Call service method
var ret0 bool
func() {
defer func() {
// In case of a panic, serve a 500 error and then panic.
if rr := recover(); rr != nil {
RespondWithError(w, ErrorInternal("internal service panic"))
panic(rr)
}
}()
ret0, err = s.service.Ping(ctx)
}()
respContent := struct {
Ret0 bool `json:"status"`
}{ret0}
if err != nil {
RespondWithError(w, err)
return
}
respBody, err := json.Marshal(respContent)
if err != nil {
RespondWithError(w, Errorf(ErrInternal, err, "failed to marshal json response"))
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(respBody)
}
func (s *exampleServiceServer) serveGetUser(ctx context.Context, w http.ResponseWriter, r *http.Request) {
var err error
ctx = context.WithValue(ctx, MethodNameCtxKey, "GetUser")
reqContent := struct {
Arg0 *GetUserRequest `json:"req"`
}{}
reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
RespondWithError(w, Errorf(ErrInternal, err, "failed to read request data"))
return
}
defer r.Body.Close()
err = json.Unmarshal(reqBody, &reqContent)
if err != nil {
RespondWithError(w, Errorf(ErrInvalidArgument, err, "failed to unmarshal request data"))
return
}
// Call service method
var ret0 *User
func() {
defer func() {
// In case of a panic, serve a 500 error and then panic.
if rr := recover(); rr != nil {
RespondWithError(w, ErrorInternal("internal service panic"))
panic(rr)
}
}()
ret0, err = s.service.GetUser(ctx, reqContent.Arg0)
}()
respContent := struct {
Ret0 *User `json:"user"`
}{ret0}
if err != nil {
RespondWithError(w, err)
return
}
respBody, err := json.Marshal(respContent)
if err != nil {
RespondWithError(w, Errorf(ErrInternal, err, "failed to marshal json response"))
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(respBody)
}
//
// Server helpers
//
func RespondWithError(w http.ResponseWriter, err error) {
e, ok := err.(Error)
if !ok {
e = Errorf(ErrInternal, err, err.Error())
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(HTTPStatusFromError(err))
respBody, _ := json.Marshal(e)
w.Write(respBody)
}
//
// Error helpers
//
type Error struct {
Code error `json:"code"`
Message string `json:"message"`
Cause error `json:"-"`
}
func (e Error) Error() string {
return e.Message
}
func (e Error) Is(target error) bool {
if errors.Is(target, e.Code) {
return true
}
if e.Cause != nil && errors.Is(target, e.Cause) {
return true
}
return false
}
func (e Error) Unwrap() error {
if e.Cause != nil {
return e.Cause
} else {
return e.Code
}
}
func (e Error) MarshalJSON() ([]byte, error) {
m, err := json.Marshal(e.Message)
if err != nil {
return nil, err
}
j := bytes.NewBufferString(`{`)
j.WriteString(`"message": `)
j.Write(m)
j.WriteString(`}`)
return j.Bytes(), nil
}
func (e *Error) UnmarshalJSON(b []byte) error {
payload := struct {
Code string `json:"code"`
Message string `json:"message"`
}{}
err := json.Unmarshal(b, &payload)
if err != nil {
return err
}
code := ErrorCodeFromString(payload.Code)
if code == nil {
code = ErrUnknown
}
*e = Error{
Code: code,
Message: payload.Message,
}
return nil
}
var (
// Fail indiciates a general error to processing a request.
ErrFail = errors.New("fail")
// Unknown error. For example when handling errors raised by APIs that do not
// return enough error information.
ErrUnknown = errors.New("unknown")
// Internal errors. When some invariants expected by the underlying system
// have been broken. In other words, something bad happened in the library or
// backend service. Do not confuse with HTTP Internal Server Error; an
// Internal error could also happen on the client code, i.e. when parsing a
// server response.
ErrInternal = errors.New("internal server error")
// Unavailable indicates the service is currently unavailable. This is a most
// likely a transient condition and may be corrected by retrying with a
// backoff.
ErrUnavailable = errors.New("unavailable")
// Unsupported indicates the request was unsupported by the server. Perhaps
// incorrect protocol version or missing feature.
ErrUnsupported = errors.New("unsupported")
// Canceled indicates the operation was cancelled (typically by the caller).
ErrCanceled = errors.New("canceled")
// InvalidArgument indicates client specified an invalid argument. It
// indicates arguments that are problematic regardless of the state of the
// system (i.e. a malformed file name, required argument, number out of range,
// etc.).
ErrInvalidArgument = errors.New("invalid argument")
// DeadlineExceeded means operation expired before completion. For operations
// that change the state of the system, this error may be returned even if the
// operation has completed successfully (timeout).
ErrDeadlineExceeded = errors.New("deadline exceeded")
// NotFound means some requested entity was not found.
ErrNotFound = errors.New("not found")
// BadRoute means that the requested URL path wasn't routable to a webrpc
// service and method. This is returned by the generated server, and usually
// shouldn't be returned by applications. Instead, applications should use
// NotFound or Unimplemented.
ErrBadRoute = errors.New("bad route")
// ErrMethodNotAllowed means that the requested URL path is available and the user
// is authenticated and authorized. The input arguments are valid, but the
// server needs to refuse the request for some reason
ErrMethodNotAllowed = errors.New("method not allowed")
// AlreadyExists means an attempt to create an entity failed because one
// already exists.
ErrAlreadyExists = errors.New("already exists")
// PermissionDenied indicates the caller does not have permission to execute
// the specified operation. It must not be used if the caller cannot be
// identified (Unauthenticated).
ErrPermissionDenied = errors.New("permission denied")
// Unauthenticated indicates the request does not have valid authentication
// credentials for the operation.
ErrUnauthenticated = errors.New("unauthenticated")
// ResourceExhausted indicates some resource has been exhausted, perhaps a
// per-user quota, or perhaps the entire file system is out of space.
ErrResourceExhausted = errors.New("resource exhausted")
// Aborted indicates the operation was aborted, typically due to a concurrency
// issue like sequencer check failures, transaction aborts, etc.
ErrAborted = errors.New("aborted")
// OutOfRange means operation was attempted past the valid range. For example,
// seeking or reading past end of a paginated collection.
ErrOutOfRange = errors.New("out of range")
// Unimplemented indicates operation is not implemented or not
// supported/enabled in this service.
ErrUnimplemented = errors.New("unimplemented")
// StreamClosed indicates that a connection stream has been closed.
ErrStreamClosed = errors.New("stream closed")
// StreamLost indiciates that a client or server connection has been interrupted
// during an active transmission. It's a good idea to reconnect.
ErrStreamLost = errors.New("stream lost")
)
func HTTPStatusFromError(err error) int {
if errors.Is(err, ErrFail) {
return 422 // Unprocessable Entity
}
if errors.Is(err, ErrUnknown) {
return 400 // BadRequest
}
if errors.Is(err, ErrInternal) {
return 500 // Internal Server Error
}
if errors.Is(err, ErrUnavailable) {
return 503 // Service Unavailable
}
if errors.Is(err, ErrUnsupported) {
return 500 // Internal Server Error
}
if errors.Is(err, ErrCanceled) {
return 408 // RequestTimeout
}
if errors.Is(err, ErrInvalidArgument) {
return 400 // BadRequest
}
if errors.Is(err, ErrDeadlineExceeded) {
return 408 // RequestTimeout
}
if errors.Is(err, ErrNotFound) {
return 404 // Not Found
}
if errors.Is(err, ErrBadRoute) {
return 404 // Not Found
}
if errors.Is(err, ErrMethodNotAllowed) {
return 405 // Method not allowed
}
if errors.Is(err, ErrAlreadyExists) {
return 409 // Conflict
}
if errors.Is(err, ErrPermissionDenied) {
return 403 // Forbidden
}
if errors.Is(err, ErrUnauthenticated) {
return 401 // Unauthorized
}
if errors.Is(err, ErrResourceExhausted) {
return 403 // Forbidden
}
if errors.Is(err, ErrAborted) {
return 409 // Conflict
}
if errors.Is(err, ErrOutOfRange) {
return 400 // Bad Request
}
if errors.Is(err, ErrUnimplemented) {
return 501 // Not Implemented
}
if errors.Is(err, ErrStreamClosed) {
return 200 // OK
}
if errors.Is(err, ErrStreamLost) {
return 408 // RequestTimeout
}
return 500 // Invalid!
}
func ErrorCodeFromString(code string) error {
switch code {
case "fail":
return ErrFail
case "unknown":
return ErrUnknown
case "internal server error":
return ErrInternal
case "unavailable":
return ErrUnavailable
case "unsupported":
return ErrUnsupported
case "canceled":
return ErrCanceled
case "invalid argument":
return ErrInvalidArgument
case "deadline exceeded":
return ErrDeadlineExceeded
case "not found":
return ErrNotFound
case "bad route":
return ErrBadRoute
case "method not allowed":
return ErrMethodNotAllowed
case "already exists":
return ErrAlreadyExists
case "permission denied":
return ErrPermissionDenied
case "unauthenticated":
return ErrUnauthenticated
case "resource exhausted":
return ErrResourceExhausted
case "aborted":
return ErrAborted
case "out of range":
return ErrOutOfRange
case "unimplemented":
return ErrUnimplemented
case "stream closed":
return ErrStreamClosed
case "stream lost":
return ErrStreamLost
default:
return nil
}
}
func Errorf(code error, cause error, message string, args ...interface{}) Error {
if ErrorCodeFromString(code.Error()) == nil {
panic("invalid error code")
}
return Error{Code: code, Message: fmt.Sprintf(message, args...), Cause: cause}
}
func Failf(cause error, message string, args ...interface{}) Error {
return Error{Code: ErrFail, Message: fmt.Sprintf(message, args...), Cause: cause}
}
func ErrorUnknown(message string, args ...interface{}) Error {
return Errorf(ErrUnknown, nil, message, args...)
}
func ErrorInternal(message string, args ...interface{}) Error {
return Errorf(ErrInternal, nil, message, args...)
}
func ErrorUnavailable(message string, args ...interface{}) Error {
return Errorf(ErrUnavailable, nil, message, args...)
}
func ErrorUnsupported(message string, args ...interface{}) Error {
return Errorf(ErrUnsupported, nil, message, args...)
}
func ErrorCanceled(message string, args ...interface{}) Error {
return Errorf(ErrCanceled, nil, message, args...)
}
func ErrorInvalidArgument(message string, args ...interface{}) Error {
return Errorf(ErrInvalidArgument, nil, message, args...)
}
func ErrorDeadlineExceeded(message string, args ...interface{}) Error {
return Errorf(ErrDeadlineExceeded, nil, message, args...)
}
func ErrorNotFound(message string, args ...interface{}) Error {
return Errorf(ErrNotFound, nil, message, args...)
}
func ErrorBadRoute(message string, args ...interface{}) Error {
return Errorf(ErrBadRoute, nil, message, args...)
}
func ErrorMethodNotAllowed(message string, args ...interface{}) Error {
return Errorf(ErrMethodNotAllowed, nil, message, args...)
}
func ErrorAlreadyExists(message string, args ...interface{}) Error {
return Errorf(ErrAlreadyExists, nil, message, args...)
}
func ErrorPermissionDenied(message string, args ...interface{}) Error {
return Errorf(ErrPermissionDenied, nil, message, args...)
}
func ErrorUnauthenticated(message string, args ...interface{}) Error {
return Errorf(ErrUnauthenticated, nil, message, args...)
}
func ErrorResourceExhausted(message string, args ...interface{}) Error {
return Errorf(ErrResourceExhausted, nil, message, args...)
}
func ErrorAborted(message string, args ...interface{}) Error {
return Errorf(ErrAborted, nil, message, args...)
}
func ErrorOutOfRange(message string, args ...interface{}) Error {
return Errorf(ErrOutOfRange, nil, message, args...)
}
func ErrorUnimplemented(message string, args ...interface{}) Error {
return Errorf(ErrUnimplemented, nil, message, args...)
}
func ErrorStreamClosed(message string, args ...interface{}) Error {
return Errorf(ErrStreamClosed, nil, message, args...)
}
func ErrorStreamLost(message string, args ...interface{}) Error {
return Errorf(ErrStreamLost, nil, message, args...)
}
func GetErrorStack(err error) []error {
errs := []error{err}
for {
unwrap, ok := err.(interface{ Unwrap() error })
if !ok {
break
}
werr := unwrap.Unwrap()
if werr == nil {
break
}
errs = append(errs, werr)
err = werr
}
return errs
}
//
// Misc helpers
//
type contextKey struct {
name string
}
func (k *contextKey) String() string {
return "webrpc context value " + k.name
}
var (
// For Client
HTTPClientRequestHeadersCtxKey = &contextKey{"HTTPClientRequestHeaders"}
// For Server
HTTPResponseWriterCtxKey = &contextKey{"HTTPResponseWriter"} // http.ResponseWriter
HTTPRequestCtxKey = &contextKey{"HTTPRequest"} // *http.Request
ServiceNameCtxKey = &contextKey{"ServiceName"} // string
MethodNameCtxKey = &contextKey{"MethodName"} // string
)