MCPcopy Create free account
hub / github.com/go-dev-frame/sponge / NewRouter_pbExample

Function NewRouter_pbExample

internal/routers/routers_pbExample.go:32–120  ·  view source on GitHub ↗

NewRouter_pbExample create a new router

()

Source from the content-addressed store, hash-verified

30
31// NewRouter_pbExample create a new router
32func NewRouter_pbExample() *gin.Engine { //nolint
33 r := gin.New()
34
35 r.Use(gin.Recovery())
36 r.Use(middleware.Cors())
37
38 if config.Get().HTTP.Timeout > 0 {
39 // if you need more fine-grained control over your routes, set the timeout in your routes, unsetting the timeout globally here.
40 r.Use(middleware.Timeout(time.Second * time.Duration(config.Get().HTTP.Timeout)))
41 }
42
43 // request id middleware
44 r.Use(middleware.RequestID())
45
46 // logger middleware, to print simple messages, replace middleware.Logging with middleware.SimpleLog
47 r.Use(middleware.Logging(
48 middleware.WithLog(logger.Get()),
49 middleware.WithRequestIDFromContext(),
50 middleware.WithIgnoreRoutes("/metrics"), // ignore path
51 ))
52
53 // metrics middleware
54 if config.Get().App.EnableMetrics {
55 r.Use(metrics.Metrics(r,
56 //metrics.WithMetricsPath("/metrics"), // default is /metrics
57 metrics.WithIgnoreStatusCodes(http.StatusNotFound), // ignore 404 status codes
58 ))
59 }
60
61 // limit middleware
62 if config.Get().App.EnableLimit {
63 r.Use(middleware.RateLimit(
64 //middleware.WithWindow(time.Second*5), // default 10s
65 //middleware.WithBucket(200), // default 100
66 //middleware.WithCPUThreshold(900), // default 800
67 ))
68 }
69
70 // circuit breaker middleware
71 if config.Get().App.EnableCircuitBreaker {
72 r.Use(middleware.CircuitBreaker(
73 //middleware.WithBreakerOption(
74 //circuitbreaker.WithSuccess(75), // default 60
75 //circuitbreaker.WithRequest(100), // default 100
76 //circuitbreaker.WithBucket(20), // default 10
77 //circuitbreaker.WithWindow(time.Second*3), // default 3s
78 //),
79 //middleware.WithDegradeHandler(handler), // Add degradation processing
80 middleware.WithValidCode( // Add error codes to trigger circuit breaking
81 errcode.InternalServerError.Code(),
82 errcode.ServiceUnavailable.Code(),
83 ),
84 ))
85 }
86
87 // trace middleware
88 if config.Get().App.EnableTrace {
89 r.Use(middleware.Tracing(config.Get().App.Name))

Callers 2

NewHTTPServer_pbExampleFunction · 0.92
TestNewRouter_pbExampleFunction · 0.85

Calls 15

CorsFunction · 0.92
GetFunction · 0.92
TimeoutFunction · 0.92
RequestIDFunction · 0.92
LoggingFunction · 0.92
WithLogFunction · 0.92
GetFunction · 0.92
WithRequestIDFromContextFunction · 0.92
WithIgnoreRoutesFunction · 0.92
MetricsFunction · 0.92
WithIgnoreStatusCodesFunction · 0.92
RateLimitFunction · 0.92

Tested by 1

TestNewRouter_pbExampleFunction · 0.68