github.com/gramework/gramework@v1.8.1-0.20231027140105-82555c9057f5/router_internals.go (about) 1 // Copyright 2017-present Kirill Danshin and Gramework contributors 2 // Copyright 2019-present Highload LTD (UK CN: 11893420) 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 11 package gramework 12 13 import ( 14 "sync" 15 16 "github.com/valyala/fasthttp" 17 ) 18 19 func (r *Router) getErrorHandler(h func(*Context) error) func(*Context) { 20 return func(ctx *Context) { 21 if err := h(ctx); err != nil { 22 r.app.internalLog.WithField("url", ctx.URI()).Errorf("Error occurred: %s", err) 23 ctx.Error("Internal Server Error", fasthttp.StatusInternalServerError) 24 } 25 } 26 } 27 28 func (r *Router) getGrameHandler(h func(*fasthttp.RequestCtx)) func(*Context) { 29 return func(ctx *Context) { 30 if ctx != nil { 31 h(ctx.RequestCtx) 32 return 33 } 34 h(new(fasthttp.RequestCtx)) 35 } 36 } 37 38 func (r *Router) getGrameDumbHandler(h func()) func(*Context) { 39 return func(*Context) { 40 h() 41 } 42 } 43 44 func (r *Router) getGrameDumbErrorHandler(h func() error) func(*Context) { 45 return func(ctx *Context) { 46 if err := h(); err != nil { 47 r.app.internalLog.WithField("url", ctx.URI()).Errorf("Error occurred: %s", err) 48 ctx.Error("Internal Server Error", fasthttp.StatusInternalServerError) 49 } 50 } 51 } 52 53 func (r *Router) getGrameErrorHandler(h func(*fasthttp.RequestCtx) error) func(*Context) { 54 return func(ctx *Context) { 55 if err := h(ctx.RequestCtx); err != nil { 56 r.app.internalLog.WithField("url", ctx.URI()).Errorf("Error occurred: %s", err) 57 ctx.Error("Internal Server Error", fasthttp.StatusInternalServerError) 58 } 59 } 60 } 61 62 var ctxPool = &sync.Pool{ 63 New: func() interface{} { 64 return &Context{} 65 }, 66 } 67 68 func acquireCtx() *Context { 69 return ctxPool.Get().(*Context) 70 } 71 72 func releaseCtx(ctx *Context) { 73 *ctx = Context{ 74 Logger: nil, 75 App: nil, 76 auth: nil, 77 requestID: "", 78 } 79 ctxPool.Put(ctx) 80 } 81 82 func (r *Router) initGrameCtx(ctx *fasthttp.RequestCtx) *Context { 83 gctx := acquireCtx() 84 gctx.Logger = r.app.Logger 85 gctx.RequestCtx = ctx 86 gctx.App = r.app 87 gctx.writer = ctx.Write 88 return gctx 89 } 90 91 func (r *Router) initRouter() { 92 if r.router == nil { 93 r.router = newRouter() 94 } 95 } 96 97 func (r *Router) getHandlerEncoder(h func() map[string]interface{}) func(*Context) { 98 return func(ctx *Context) { 99 r := h() 100 if r == nil { 101 ctx.SetStatusCode(fasthttp.StatusNoContent) 102 return 103 } 104 if err := ctx.JSON(r); err != nil { 105 ctx.jsonErrorLog(err) 106 } 107 } 108 } 109 110 func (r *Router) getCtxHandlerEncoder(h func(*Context) map[string]interface{}) func(*Context) { 111 return func(ctx *Context) { 112 r := h(ctx) 113 if r == nil { 114 ctx.SetStatusCode(fasthttp.StatusNoContent) 115 return 116 } 117 if err := ctx.JSON(r); err != nil { 118 ctx.jsonErrorLog(err) 119 } 120 } 121 } 122 123 func (r *Router) getHandlerEncoderErr(h func() (map[string]interface{}, error)) func(*Context) { 124 return func(ctx *Context) { 125 r, err := h() 126 if err != nil { 127 ctx.jsonErrorLog(err) 128 return 129 } 130 if r == nil { // err == nil here 131 ctx.SetStatusCode(fasthttp.StatusNoContent) 132 return 133 } 134 if err = ctx.JSON(r); err != nil { 135 ctx.jsonErrorLog(err) 136 } 137 } 138 } 139 140 func (r *Router) getCtxHandlerEncoderErr(h func(*Context) (map[string]interface{}, error)) func(*Context) { 141 return func(ctx *Context) { 142 r, err := h(ctx) 143 if err != nil { 144 ctx.jsonErrorLog(err) 145 return 146 } 147 if r == nil { // err == nil here 148 ctx.SetStatusCode(fasthttp.StatusNoContent) 149 return 150 } 151 if err = ctx.JSON(r); err != nil { 152 ctx.jsonErrorLog(err) 153 } 154 } 155 } 156 157 func (r *Router) getEfaceEncoder(h func() interface{}) func(*Context) { 158 return func(ctx *Context) { 159 r := h() 160 if r == nil { // err == nil here 161 ctx.SetStatusCode(fasthttp.StatusNoContent) 162 return 163 } 164 if err := ctx.JSON(r); err != nil { 165 ctx.jsonErrorLog(err) 166 } 167 } 168 } 169 170 func (r *Router) getEfaceErrEncoder(h func() (interface{}, error)) func(*Context) { 171 return func(ctx *Context) { 172 r, err := h() 173 if err != nil { 174 ctx.jsonErrorLog(err) 175 return 176 } 177 if r == nil { // err == nil here 178 ctx.SetStatusCode(fasthttp.StatusNoContent) 179 return 180 } 181 if err = ctx.JSON(r); err != nil { 182 ctx.jsonErrorLog(err) 183 } 184 } 185 } 186 187 func (r *Router) getEfaceCtxEncoder(h func(*Context) interface{}) func(*Context) { 188 return func(ctx *Context) { 189 r := h(ctx) 190 if r == nil { // err == nil here 191 ctx.SetStatusCode(fasthttp.StatusNoContent) 192 return 193 } 194 if err := ctx.JSON(r); err != nil { 195 ctx.jsonErrorLog(err) 196 } 197 } 198 } 199 200 func (r *Router) getEfaceCtxErrEncoder(h func(*Context) (interface{}, error)) func(*Context) { 201 return func(ctx *Context) { 202 r, err := h(ctx) 203 if err != nil { 204 ctx.jsonErrorLog(err) 205 return 206 } 207 if r == nil { // err == nil here 208 ctx.SetStatusCode(fasthttp.StatusNoContent) 209 return 210 } 211 if err = ctx.JSON(r); err != nil { 212 ctx.jsonErrorLog(err) 213 } 214 } 215 }