/
server.go
1321 lines (1114 loc) · 40.5 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
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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package miso
import (
"context"
"fmt"
"io"
"net/http"
"net/http/pprof"
"net/url"
"os"
"os/signal"
"reflect"
"regexp"
"sort"
"strings"
"sync"
"syscall"
"time"
"github.com/sirupsen/logrus"
"github.com/gin-gonic/gin"
jsoniter "github.com/json-iterator/go"
)
const (
// Components like database that are essential and must be ready before anything else.
BootstrapOrderL1 = -20
// Components that are bootstraped before the web server, such as metrics stuff.
BootstrapOrderL2 = -15
// The web server or anything similar, bootstraping web server doesn't really mean that we will receive inbound requests.
BootstrapOrderL3 = -10
// Components that introduce inbound requests or job scheduling.
//
// When these components bootstrap, the server is considered truly running.
// For example, service registration (for service discovery), MQ broker connection and so on.
BootstrapOrderL4 = -5
ExtraDesc = "miso-Desc"
ExtraScope = "miso-Scope"
ExtraResource = "miso-Resource"
ExtraQueryParam = "miso-QueryParam"
ExtraHeaderParam = "miso-HeaderParam"
ExtraJsonRequest = "miso-JsonRequest"
ExtraJsonResponse = "miso-JsonResponse"
ScopePublic = "PUBLIC"
ScopeProtected = "PROTECTED"
defShutdownOrder = 5
TagQueryParam = "form"
TagHeaderParam = "header"
)
var (
loggerOut io.Writer = os.Stdout
loggerErrOut io.Writer = os.Stderr
lazyRouteRegistars []*LazyRouteDecl
routeRegistars []routesRegistar
serverHttpRoutes []HttpRoute
ginPreProcessors []GinPreProcessor
shuttingDown bool = false
shutingDownRwm sync.RWMutex // rwmutex for shuttingDown
shutdownHook []OrderedShutdownHook
shmu sync.Mutex // mutex for shutdownHook
serverBootrapCallbacks []ComponentBootstrap
preServerBootstrapListener []func(r Rail) error
postServerBootstrapListener []func(r Rail) error
// all http methods
anyHttpMethods = []string{
http.MethodGet, http.MethodPost, http.MethodPut, http.MethodPatch,
http.MethodHead, http.MethodOptions, http.MethodDelete, http.MethodConnect,
http.MethodTrace,
}
// channel for signaling server shutdown
manualSigQuit = make(chan int, 1)
resultBodyBuilder = ResultBodyBuilder{
ErrJsonBuilder: func(rail Rail, url string, err error) any { return WrapResp(rail, nil, err, url) },
PayloadJsonBuilder: func(payload any) any { return OkRespWData(payload) },
OkJsonBuilder: func() any { return OkResp() },
}
endpointResultHandler = func(c *gin.Context, rail Rail, payload any, err error) {
if err != nil {
DispatchJson(c, resultBodyBuilder.ErrJsonBuilder(rail, c.Request.RequestURI, err))
return
}
if payload != nil {
DispatchJson(c, resultBodyBuilder.PayloadJsonBuilder(payload))
return
}
DispatchJson(c, resultBodyBuilder.OkJsonBuilder())
}
// enable pprof manually
manualRegisterPprof = false
noRouteHandler = func(ctx *gin.Context, rail Rail) {
ctx.AbortWithStatus(404)
}
)
type HttpRoute struct {
Url string
Method string
Extra map[string][]any
Desc string // description of the route (metadata).
Scope string // the documented access scope of the route, it maybe "PUBLIC" or something else (metadata).
Resource string // the documented resource that the route should be bound to (metadata).
Headers []ParamDoc // the documented header parameters that will be used by the endpoint (metadata).
QueryParams []ParamDoc // the documented query parameters that will used by the endpoint (metadata).
JsonRequestVal any // the documented json request value that is expected by the endpoint (metadata).
JsonResponseVal any // the documented json response value that will be returned by the endpoint (metadata).
}
type ComponentBootstrap struct {
// name of the component.
Name string
// the actual bootstrap function.
Bootstrap func(rail Rail) error
// check whether component should be bootstraped
Condition func(rail Rail) (bool, error)
// order of which the components are bootstraped, natural order, it's by default 15.
Order int
}
func init() {
SetDefProp(PropServerEnabled, true)
SetDefProp(PropServerHost, "0.0.0.0")
SetDefProp(PropServerPort, 8080)
SetDefProp(PropServerGracefulShutdownTimeSec, 5)
SetDefProp(PropServerPerfEnabled, false)
SetDefProp(PropServerPropagateInboundTrace, true)
SetDefProp(PropServerRequestValidateEnabled, true)
SetDefProp(PropServerPprofEnabled, false)
SetDefProp(PropServerRequestAutoMapHeader, true)
SetDefProp(PropServerGinValidationDisabled, true)
SetDefProp(PropLoggingRollingFileMaxAge, 0)
SetDefProp(PropLoggingRollingFileMaxSize, 50)
SetDefProp(PropLoggingRollingFileMaxBackups, 0)
SetDefProp(PropLoggingRollingFileRotateDaily, true)
RegisterBootstrapCallback(ComponentBootstrap{
Name: "Bootstrap HTTP Server",
Bootstrap: WebServerBootstrap,
Condition: WebServerBootstrapCondition,
Order: BootstrapOrderL3,
})
}
type ResultBodyBuilder struct {
// wrap error in json, the returned object will be serialized to json.
ErrJsonBuilder func(rail Rail, url string, err error) any
// wrap payload object, the returned object will be serialized to json.
PayloadJsonBuilder func(payload any) any
// build empty ok response object, the returned object will be serialized to json.
OkJsonBuilder func() any
}
// Replace the default ResultBodyBuilder
func SetResultBodyBuilder(rbb ResultBodyBuilder) error {
resultBodyBuilder = rbb
return nil
}
// Register shutdown hook, hook should never panic
func AddShutdownHook(hook func()) {
addOrderedShutdownHook(defShutdownOrder, hook)
}
type OrderedShutdownHook struct {
Hook func()
Order int
}
func addOrderedShutdownHook(order int, hook func()) {
shmu.Lock()
defer shmu.Unlock()
shutdownHook = append(shutdownHook, OrderedShutdownHook{
Order: order,
Hook: hook,
})
}
// Trigger shutdown hook
func triggerShutdownHook() {
shmu.Lock()
defer shmu.Unlock()
sort.Slice(shutdownHook, func(i, j int) bool { return shutdownHook[i].Order < shutdownHook[j].Order })
for _, hook := range shutdownHook {
hook.Hook()
}
}
// Record server route
func recordHttpServerRoute(url string, method string, extra ...StrPair) {
extras := MergeStrPairs(extra...)
r := HttpRoute{
Url: url,
Method: method,
Extra: extras,
}
if l, ok := extras[ExtraResource]; ok && len(l) > 0 {
if v, ok := l[0].(string); ok {
r.Resource = v
}
}
if l, ok := extras[ExtraScope]; ok && len(l) > 0 {
if v, ok := l[0].(string); ok {
r.Scope = v
}
}
if l, ok := extras[ExtraDesc]; ok && len(l) > 0 {
if v, ok := l[0].(string); ok {
r.Desc = v
}
}
if l, ok := extras[ExtraQueryParam]; ok && len(l) > 0 {
for _, p := range l {
if v, ok := p.(ParamDoc); ok {
r.QueryParams = append(r.QueryParams, v)
}
}
}
if l, ok := extras[ExtraHeaderParam]; ok && len(l) > 0 {
for _, p := range l {
if v, ok := p.(ParamDoc); ok {
r.Headers = append(r.Headers, v)
}
}
}
if l, ok := extras[ExtraJsonRequest]; ok && len(l) > 0 {
r.JsonRequestVal = l[0]
}
if l, ok := extras[ExtraJsonResponse]; ok && len(l) > 0 {
r.JsonResponseVal = l[0]
}
serverHttpRoutes = append(serverHttpRoutes, r)
}
// Get recorded http server routes
func GetHttpRoutes() []HttpRoute {
return serverHttpRoutes
}
// Register ANY request route (raw version)
func RawAny(url string, handler RawTRouteHandler, extra ...StrPair) {
for i := range anyHttpMethods {
recordHttpServerRoute(url, anyHttpMethods[i], extra...)
}
addRoutesRegistar(func(e *gin.Engine) { e.Any(url, newRawTRouteHandler(handler)) })
}
// Register GET request route (raw version)
func RawGet(url string, handler RawTRouteHandler) *LazyRouteDecl {
return NewLazyRouteDecl(url, http.MethodGet, newRawTRouteHandler(handler))
}
// Register POST request route (raw version)
func RawPost(url string, handler RawTRouteHandler) *LazyRouteDecl {
return NewLazyRouteDecl(url, http.MethodPost, newRawTRouteHandler(handler))
}
// Register PUT request route (raw version)
func RawPut(url string, handler RawTRouteHandler) *LazyRouteDecl {
return NewLazyRouteDecl(url, http.MethodPut, newRawTRouteHandler(handler))
}
// Register DELETE request route (raw version)
func RawDelete(url string, handler RawTRouteHandler) *LazyRouteDecl {
return NewLazyRouteDecl(url, http.MethodDelete, newRawTRouteHandler(handler))
}
// Register GET request.
//
// The result and error are automatically wrapped to miso.Resp (see miso.SetResultBodyBuilder func)
// and serialized to json.
func Get[Res any](url string, handler TRouteHandler[Res]) *LazyRouteDecl {
return NewLazyRouteDecl(url, http.MethodGet, newTRouteHandler(handler)).
DocJsonResp(resultBodyBuilder.PayloadJsonBuilder(NewVar[Res]()))
}
// Register POST request.
//
// The result and error are automatically wrapped to miso.Resp (see miso.SetResultBodyBuilder func)
// and serialized to json.
func Post[Res any](url string, handler TRouteHandler[Res]) *LazyRouteDecl {
return NewLazyRouteDecl(url, http.MethodPost, newTRouteHandler(handler)).
DocJsonResp(resultBodyBuilder.PayloadJsonBuilder(NewVar[Res]()))
}
// Register PUT request.
//
// The result and error are automatically wrapped to miso.Resp (see miso.SetResultBodyBuilder func)
// and serialized to json.
func Put[Res any](url string, handler TRouteHandler[Res]) *LazyRouteDecl {
return NewLazyRouteDecl(url, http.MethodPut, newTRouteHandler(handler)).
DocJsonResp(resultBodyBuilder.PayloadJsonBuilder(NewVar[Res]()))
}
// Register DELETE request.
//
// The result and error are automatically wrapped to miso.Resp (see miso.SetResultBodyBuilder func)
// and serialized to json.
func Delete[Res any](url string, handler TRouteHandler[Res]) *LazyRouteDecl {
return NewLazyRouteDecl(url, http.MethodDelete, newTRouteHandler(handler)).
DocJsonResp(resultBodyBuilder.PayloadJsonBuilder(NewVar[Res]()))
}
// Register POST request.
//
// Req type should be a struct, where all fields are automatically mapped from the request
// using 'json' tag or 'form' tag (for form-data, query param) or 'header' tag (only supports string/*string).
//
// Res type should be a struct. By default both Res value and error (if not nil) will be wrapped inside
// miso.Resp and serialized to json. Wrapping to miso.Resp is customizable using miso.SetResultBodyBuilder func.
//
// With both Req and Res type declared, miso will automatically parse these two types using reflect
// and generate an API documentation describing the endpoint.
func IPost[Req any, Res any](url string, handler MappedTRouteHandler[Req, Res]) *LazyRouteDecl {
return NewLazyRouteDecl(url, http.MethodPost, newMappedTRouteHandler(handler)).
DocJsonReq(NewVar[Req]()).
DocJsonResp(resultBodyBuilder.PayloadJsonBuilder(NewVar[Res]()))
}
// Register GET request.
//
// Req type should be a struct, where all fields are automatically mapped from the request
// using 'form' tag (for form-data, query param) or 'header' tag (only supports string/*string).
//
// Res type should be a struct. By default both Res value and error (if not nil) will be wrapped inside
// miso.Resp and serialized to json. Wrapping to miso.Resp is customizable using miso.SetResultBodyBuilder func.
//
// With both Req and Res type declared, miso will automatically parse these two types using reflect
// and generate an API documentation describing the endpoint.
func IGet[Req any, Res any](url string, handler MappedTRouteHandler[Req, Res]) *LazyRouteDecl {
var r Req
return NewLazyRouteDecl(url, http.MethodGet, newMappedTRouteHandler(handler)).
DocQueryReq(r).
DocHeaderReq(r).
DocJsonResp(resultBodyBuilder.PayloadJsonBuilder(NewVar[Res]()))
}
// Register DELETE request.
//
// Req type should be a struct, where all fields are automatically mapped from the request
// using 'json' tag or 'form' tag (for form-data, query param) or 'header' tag (only supports string/*string).
//
// Res type should be a struct. By default both Res value and error (if not nil) will be wrapped inside
// miso.Resp and serialized to json. Wrapping to miso.Resp is customizable using miso.SetResultBodyBuilder func.
//
// With both Req and Res type declared, miso will automatically parse these two types using reflect
// and generate an API documentation describing the endpoint.
func IDelete[Req any, Res any](url string, handler MappedTRouteHandler[Req, Res]) *LazyRouteDecl {
var r Req
return NewLazyRouteDecl(url, http.MethodDelete, newMappedTRouteHandler(handler)).
DocQueryReq(r).
DocHeaderReq(r).
DocJsonResp(resultBodyBuilder.PayloadJsonBuilder(NewVar[Res]()))
}
// Register PUT request.
//
// Req type should be a struct, where all fields are automatically mapped from the request
// using 'json' tag or 'form' tag (for form-data, query param) or 'header' tag (only supports string/*string).
//
// Res type should be a struct. By default both Res value and error (if not nil) will be wrapped inside
// miso.Resp and serialized to json. Wrapping to miso.Resp is customizable using miso.SetResultBodyBuilder func.
//
// With both Req and Res type declared, miso will automatically parse these two types using reflect
// and generate an API documentation describing the endpoint.
func IPut[Req any, Res any](url string, handler MappedTRouteHandler[Req, Res]) *LazyRouteDecl {
return NewLazyRouteDecl(url, http.MethodPut, newMappedTRouteHandler(handler)).
DocJsonReq(NewVar[Req]()).
DocJsonResp(resultBodyBuilder.PayloadJsonBuilder(NewVar[Res]()))
}
type routesRegistar func(*gin.Engine)
func addRoutesRegistar(reg routesRegistar) {
routeRegistars = append(routeRegistars, reg)
}
// Register GIN route for consul healthcheck
func registerRouteForConsulHealthcheck(router *gin.Engine) {
router.GET(GetPropStr(PropConsulHealthcheckUrl), DefaultHealthCheck)
}
func startHttpServer(rail Rail, server *http.Server) {
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
rail.Fatalf("http.Server ListenAndServe: %s", err)
}
}
func createHttpServer(router http.Handler) *http.Server {
addr := fmt.Sprintf("%s:%s", GetPropStr(PropServerHost), GetPropStr(PropServerPort))
server := &http.Server{
Addr: addr,
Handler: router,
}
return server
}
// Configure logging level and output target based on loaded configuration.
func ConfigureLogging(rail Rail) error {
// determine the writer that we will use for logging (loggerOut and loggerErrOut)
if ContainsProp(PropLoggingRollingFile) {
logFile := GetPropStr(PropLoggingRollingFile)
log := BuildRollingLogFileWriter(NewRollingLogFileParam{
Filename: logFile,
MaxSize: GetPropInt(PropLoggingRollingFileMaxSize), // megabytes
MaxAge: GetPropInt(PropLoggingRollingFileMaxAge), //days
MaxBackups: GetPropInt(PropLoggingRollingFileMaxBackups),
})
loggerOut = log
loggerErrOut = log
if GetPropBool(PropLoggingRollingFileRotateDaily) {
// schedule a job to rotate the log at 00:00:00
if err := ScheduleCron(Job{
Name: "RotateLogJob",
Cron: "0 0 0 * * ?",
CronWithSeconds: true,
Run: func(r Rail) error { return log.Rotate() },
}); err != nil {
return fmt.Errorf("failed to register RotateLogJob, %v", err)
}
}
}
logrus.SetOutput(loggerOut)
if HasProp(PropLoggingLevel) {
if level, ok := ParseLogLevel(GetPropStr(PropLoggingLevel)); ok {
logrus.SetLevel(level)
}
}
return nil
}
func callPostServerBootstrapListeners(rail Rail) error {
i := 0
for i < len(postServerBootstrapListener) {
if e := postServerBootstrapListener[i](rail); e != nil {
return e
}
i++
}
postServerBootstrapListener = nil
return nil
}
// Add listener that is invoked when server is finally bootstrapped
//
// This usually means all server components are started, such as MySQL connection, Redis Connection and so on.
//
// Caller is free to call PostServerBootstrapped inside another PostServerBootstrapped callback.
func PostServerBootstrapped(callback func(rail Rail) error) {
if callback == nil {
return
}
postServerBootstrapListener = append(postServerBootstrapListener, callback)
}
// Add listener that is invoked before the server is fully bootstrapped
//
// This usually means that the configuration is loaded, and the logging is configured, but the server components are not yet initialized.
//
// Caller is free to call PostServerBootstrapped or PreServerBootstrap inside another PreServerBootstrap callback.
func PreServerBootstrap(callback func(rail Rail) error) {
if callback == nil {
return
}
preServerBootstrapListener = append(preServerBootstrapListener, callback)
}
func callPreServerBootstrapListeners(rail Rail) error {
i := 0
for i < len(preServerBootstrapListener) {
if e := preServerBootstrapListener[i](rail); e != nil {
return e
}
i++
}
preServerBootstrapListener = nil
return nil
}
// Register server component bootstrap callback
//
// When such callback is invoked, configuration should be fully loaded, the callback is free to read the loaded configuration
// and decide whether or not the server component should be initialized, e.g., by checking if the enable flag is true.
func RegisterBootstrapCallback(bootstrapComponent ComponentBootstrap) {
serverBootrapCallbacks = append(serverBootrapCallbacks, bootstrapComponent)
}
/*
Bootstrap server
This func will attempt to create http server, connect to MySQL, Redis or Consul based on the configuration loaded.
It also handles service registration/de-registration on Consul before Gin bootstraped and after
SIGTERM/INTERRUPT signals are received.
Graceful shutdown for the http server is also enabled and can be configured through props.
To configure server, MySQL, Redis, Consul and so on, see PROPS_* in prop.go.
It's also possible to register callbacks that are triggered before/after server bootstrap
server.PreServerBootstrap(func(c Rail) error {
// do something right after configuration being loaded, but server hasn't been bootstraped yet
});
server.PostServerBootstrapped(func(c Rail) error {
// do something after the server bootstrap
});
// start the server
server.BootstrapServer(os.Args)
*/
func BootstrapServer(args []string) {
osSigQuit := make(chan os.Signal, 2)
signal.Notify(osSigQuit, os.Interrupt, syscall.SIGTERM)
addOrderedShutdownHook(0, MarkServerShuttingDown) // the first hook to be called
var rail Rail = EmptyRail()
start := time.Now().UnixMilli()
defer triggerShutdownHook()
// default way to load configuration
DefaultReadConfig(args, rail)
// configure logging
if err := ConfigureLogging(rail); err != nil {
rail.Errorf("Configure logging failed, %v", err)
return
}
appName := GetPropStr(PropAppName)
if appName == "" {
rail.Fatalf("Property '%s' is required", PropAppName)
}
rail.Infof("\n\n---------------------------------------------- starting %s -------------------------------------------------------\n", appName)
rail.Infof("Miso Version: %s", Version)
rail.Infof("Production Mode: %v", GetPropBool(PropProdMode))
// invoke callbacks to setup server, sometime we need to setup stuff right after the configuration being loaded
if e := callPreServerBootstrapListeners(rail); e != nil {
rail.Errorf("Error occurred while invoking pre server bootstrap callbacks, %v", e)
return
}
// bootstrap components, these are sorted by their orders
sort.Slice(serverBootrapCallbacks, func(i, j int) bool { return serverBootrapCallbacks[i].Order < serverBootrapCallbacks[j].Order })
Debugf("serverBootrapCallbacks: %+v", serverBootrapCallbacks)
for _, sbc := range serverBootrapCallbacks {
if sbc.Condition != nil {
ok, ce := sbc.Condition(rail)
if ce != nil {
rail.Errorf("Failed to bootstrap server component: %v, failed on condition check, %v", sbc.Name, ce)
return
}
if !ok {
continue
}
}
start := time.Now()
if e := sbc.Bootstrap(rail); e != nil {
rail.Errorf("Failed to bootstrap server component: %v, %v", sbc.Name, e)
return
}
rail.Debugf("Callback %-30s - took %v", sbc.Name, time.Since(start))
}
serverBootrapCallbacks = nil
end := time.Now().UnixMilli()
rail.Infof("\n\n---------------------------------------------- %s started (took: %dms) --------------------------------------------\n", appName, end-start)
// invoke listener for serverBootstraped event
if e := callPostServerBootstrapListeners(rail); e != nil {
rail.Errorf("Error occurred while invoking post server bootstrap callbacks, %v", e)
return
}
// wait for Interrupt or SIGTERM, and shutdown gracefully
select {
case sig := <-osSigQuit:
rail.Infof("Received OS signal: %v, exiting", sig)
case <-manualSigQuit: // or wait for maunal shutdown signal
rail.Infof("Received manual shutdown signal, exiting")
}
}
// Shutdown server
func Shutdown() {
manualSigQuit <- 1
}
// Register http routes on gin.Engine
func registerServerRoutes(c Rail, engine *gin.Engine) {
// no route
engine.NoRoute(func(ctx *gin.Context) {
rail := BuildRail(ctx)
rail.Warnf("NoRoute for %s '%s'", ctx.Request.Method, ctx.Request.RequestURI)
noRouteHandler(ctx, rail)
})
// register custom routes
for _, registerRoute := range routeRegistars {
registerRoute(engine)
}
routeRegistars = nil
for _, lrr := range lazyRouteRegistars {
lrr.build(engine)
}
lazyRouteRegistars = nil
for _, r := range GetHttpRoutes() {
c.Debugf("%-6s %s", r.Method, r.Url)
}
}
/*
shutdown http server, including gracefull shutdown within certain duration of time
This func looks for following prop:
"server.gracefulShutdownTimeSec"
*/
func shutdownHttpServer(server *http.Server) {
Info("Shutting down http server gracefully")
// set timeout for graceful shutdown
timeout := GetPropInt(PropServerGracefulShutdownTimeSec)
if timeout <= 0 {
timeout = 30
}
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
defer cancel()
// shutdown web server with the timeout
server.Shutdown(ctx)
Infof("Http server exited")
}
// Default Recovery func
func DefaultRecovery(c *gin.Context, e interface{}) {
rail := BuildRail(c)
rail.Errorf("%v '%v' Recovered from panic, %v", c.Request.Method, c.Request.RequestURI, e)
// response already written, avoid writting it again.
if c.Writer.Written() {
if me, ok := e.(*MisoErr); ok {
rail.Infof("Miso error, code: '%v', msg: '%v', internalMsg: '%v'", me.Code, me.Msg, me.InternalMsg)
return
}
rail.Errorf("Unknown error, %v", e)
return
}
if err, ok := e.(error); ok {
endpointResultHandler(c, rail, nil, err)
return
}
endpointResultHandler(c, rail, nil, NewErrf("Unknown error, please try again later"))
}
// check if the server is shutting down
func IsShuttingDown() bool {
shutingDownRwm.RLock()
defer shutingDownRwm.RUnlock()
return shuttingDown
}
// mark that the server is shutting down
func MarkServerShuttingDown() {
shutingDownRwm.Lock()
defer shutingDownRwm.Unlock()
shuttingDown = true
}
// Tracing Middleware
func TraceMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
// propagate tracing key/value pairs with context
ctx := c.Request.Context()
UsePropagationKeys(func(k string) {
if h := c.GetHeader(k); h != "" {
ctx = context.WithValue(ctx, k, h) //lint:ignore SA1029 keys must be exposed to retrieve the values
}
})
// replace the context
c.Request = c.Request.WithContext(ctx)
// follow the chain
c.Next()
}
}
// Build Rail from gin.Context.
//
// This func creates new Rail for the first time by setting up proper traceId and spanId.
//
// It can also recognize that a traceId (and spanId) was previously created, and do attempt to reuse these tracing values,
// such that the Rail acts as if it's the previous one, this is especially useful when we are recovering from a panic.
// In most cases, we should only call BuildRail for once.
//
// However, if the Rail has attempted to overwrite it's spanId (i.e., creating new span), this newly created spanId will not
// be reflected on the Rail created here. But this should be fine, because new span is usually created for async operation.
func BuildRail(c *gin.Context) Rail {
if !GetPropBool(PropServerPropagateInboundTrace) {
return EmptyRail()
}
if c.Keys == nil {
c.Keys = map[string]any{}
}
ctx := c.Request.Context()
// it's possible that the spanId and traceId have been created already
// if we call BuildRail() for the second time, we should read from the *gin.Context
// instead of creating new ones.
// for the most of the time, we are using one single Rail throughout the method calls
contextModified := false
UsePropagationKeys(func(k string) {
if v, ok := c.Keys[k]; ok && v != "" {
// c.Keys -> c.Request.Context()
ctx = context.WithValue(ctx, k, v) //lint:ignore SA1029 keys must be exposed for client to use
contextModified = true // the trace is not newly created, we are loading it from *gin.Context
}
})
// create a new Rail
rail := NewRail(ctx)
// this is mainly used for panic recovery
if !contextModified {
UsePropagationKeys(func(k string) {
// c.Request.Context() -> c.Keys
// copy the newly created keys back to the gin.Context
if v, ok := c.Keys[k]; !ok || v == "" {
c.Keys[k] = rail.CtxValue(k)
}
})
}
return rail
}
// Traced and parameters mapped route handler.
//
// Req type should be a struct, where all fields are automatically mapped from the request
// using 'json' tag or 'form' tag (for form-data, query param) or 'header' tag (only supports string/*string).
//
// Res type should be a struct. By default both Res value and error (if not nil) will be wrapped inside
// miso.Resp and serialized to json. Wrapping to miso.Resp is customizable using miso.SetResultBodyBuilder func.
//
// With both Req and Res type declared, miso will automatically parse these two types using reflect
// and generate an API documentation describing the endpoint.
type MappedTRouteHandler[Req any, Res any] func(inb *Inbound, req Req) (Res, error)
// Build route handler with the mapped payload object, context, and logger.
//
// value and error returned by handler are automically wrapped in a Resp object
func newMappedTRouteHandler[Req any, Res any](handler MappedTRouteHandler[Req, Res]) func(c *gin.Context) {
return func(c *gin.Context) {
rail := BuildRail(c)
// bind to payload boject
var req Req
MustBind(rail, c, &req)
wtcbCnt := 0
if GetPropBool(PropServerRequestValidateEnabled) {
wtcbCnt += 2
}
if GetPropBool(PropServerRequestAutoMapHeader) {
wtcbCnt += 1
}
if wtcbCnt > 0 {
wtcb := make([]WalkTagCallback, 0, wtcbCnt)
// validate request
if GetPropBool(PropServerRequestValidateEnabled) {
wtcb = append(wtcb, ValidateWalkTagCallback, ValidateWalkTagCallbackDeprecated)
}
// for setting headers
if GetPropBool(PropServerRequestAutoMapHeader) {
wtcb = append(wtcb, reflectSetHeaderCallback(c))
}
if err := WalkTagShallow(&req, wtcb...); err != nil {
endpointResultHandler(c, rail, nil, err)
return
}
}
if GetPropBool(PropServerRequestLogEnabled) {
rail.Infof("%v %v, req: %+v", c.Request.Method, c.Request.RequestURI, req)
}
// handle the requests
res, err := handler(newInbound(c), req)
// wrap result and error
endpointResultHandler(c, rail, res, err)
}
}
// Raw version of traced route handler.
type RawTRouteHandler func(inb *Inbound)
// Build route handler with context, and logger
func newRawTRouteHandler(handler RawTRouteHandler) func(c *gin.Context) {
return func(c *gin.Context) {
handler(newInbound(c))
}
}
// Traced route handler.
//
// Res type should be a struct. By default both Res value and error (if not nil) will be wrapped inside
// miso.Resp and serialized to json. Wrapping to miso.Resp is customizable using miso.SetResultBodyBuilder func.
//
// With Res type declared, miso will automatically parse the Res type using reflect and generate an API documentation
// describing the endpoint.
type TRouteHandler[Res any] func(inb *Inbound) (Res, error)
// Build route handler with context, and logger
//
// value and error returned by handler are automically wrapped in a Resp object
func newTRouteHandler[Res any](handler TRouteHandler[Res]) func(c *gin.Context) {
return func(c *gin.Context) {
rail := BuildRail(c)
r, e := handler(newInbound(c))
endpointResultHandler(c, rail, r, e)
}
}
// Handle endpoint's result using the configured EndpointResultHandler.
func HandleEndpointResult(inb Inbound, rail Rail, result any, err error) {
c := inb.Engine().(*gin.Context)
endpointResultHandler(c, rail, result, err)
}
// Must bind request payload to the given pointer, else panic
func MustBind(rail Rail, c *gin.Context, ptr any) {
onFailed := func(err error) {
rail.Errorf("Bind payload failed, %v", err)
panic(NewErrf("Illegal Arguments"))
}
// we now use jsoniter
if c.Request.Method != http.MethodGet && c.ContentType() == gin.MIMEJSON {
if err := jsoniter.NewDecoder(c.Request.Body).Decode(ptr); err != nil {
onFailed(err)
}
return
}
// other mime types
if err := c.ShouldBind(ptr); err != nil {
onFailed(err)
}
}
// Dispatch a json response
func DispatchJsonCode(c *gin.Context, code int, body interface{}) {
c.Status(code)
c.Header("Content-Type", applicationJson)
err := jsoniter.NewEncoder(c.Writer).Encode(body)
if err != nil {
panic(err)
}
}
// Dispatch error response in json format
func DispatchErrMsgJson(c *gin.Context, msg string) {
DispatchJson(c, ErrorResp(msg))
}
// Dispatch a json response
func DispatchJson(c *gin.Context, body interface{}) {
DispatchJsonCode(c, http.StatusOK, body)
}
func WebServerBootstrapCondition(rail Rail) (bool, error) {
return GetPropBool(PropServerEnabled), nil
}
func WebServerBootstrap(rail Rail) error {
rail.Info("Starting HTTP server")
// Load propagation keys for tracing
LoadPropagationKeys(rail)
// always set to releaseMode
gin.SetMode(gin.ReleaseMode)
if GetPropBool(PropServerGinValidationDisabled) {
rail.Debug("Disabled Gin's builtin validation")
gin.DisableBindValidation()
}
// gin engine
engine := gin.New()
engine.Use(TraceMiddleware())
if !IsProdMode() && IsDebugLevel() {
engine.Use(gin.Logger()) // gin's default logger for debugging
}
if GetPropBool(PropServerPerfEnabled) {
engine.Use(PerfMiddleware())
}
for _, p := range ginPreProcessors {
p(rail, engine)
}
ginPreProcessors = nil
if !manualRegisterPprof && (!IsProdMode() || GetPropBool(PropServerPprofEnabled)) {
GroupRoute("/debug/pprof",
RawGet("", func(inb *Inbound) { pprof.Index(inb.Unwrap()) }),
RawGet("/:name", func(inb *Inbound) { pprof.Index(inb.Unwrap()) }),
RawGet("/cmdline", func(inb *Inbound) { pprof.Cmdline(inb.Unwrap()) }),
RawGet("/profile", func(inb *Inbound) { pprof.Profile(inb.Unwrap()) }),
RawGet("/symbol", func(inb *Inbound) { pprof.Symbol(inb.Unwrap()) }),
RawGet("/trace", func(inb *Inbound) { pprof.Trace(inb.Unwrap()) }),
)
}
// register customer recovery func
engine.Use(gin.RecoveryWithWriter(loggerErrOut, DefaultRecovery))
// register consul health check
if GetPropBool(PropConsulEnabled) && GetPropBool(PropConsulRegisterDefaultHealthcheck) {
registerRouteForConsulHealthcheck(engine)
}
if !IsProdMode() && GetPropBool(PropServerGenerateEndpointDocEnabled) {
if err := serveApiDocTmpl(rail); err != nil {
rail.Errorf("failed to buildEndpointDocTmpl, %v", err)
}
}
// register http routes
registerServerRoutes(rail, engine)
// start the http server
server := createHttpServer(engine)
rail.Infof("Serving HTTP on %s", server.Addr)
go startHttpServer(rail, server)
AddShutdownHook(func() { shutdownHttpServer(server) })
return nil
}
type TreePath interface {
Prepend(baseUrl string)
}
// Lazy route declaration
type LazyRouteDecl struct {
Url string
Method string
Handler func(c *gin.Context)
RegisterFunc func(extra ...StrPair)
Extras []StrPair
}
// Build endpoint.