@@ -13,7 +13,7 @@ import (
1313 "strings"
1414 "text/template"
1515
16- log "github.com/codeshelldev/secured-signal-api/utils"
16+ log "github.com/codeshelldev/secured-signal-api/utils/logger "
1717)
1818
1919type AuthType string
@@ -81,6 +81,19 @@ func renderTemplate(name string, tmplStr string, data any) (string, error) {
8181 return buf .String (), nil
8282}
8383
84+ func templateJSON (data map [string ]interface {}, variables map [string ]interface {}) map [string ]interface {} {
85+ for k , v := range data {
86+ if str , ok := v .(string ); ok && len (str ) > 4 && str [:3 ] == "{{." {
87+ key := str [3 : len (str )- 2 ]
88+ if val , found := variables [key ]; found {
89+ data [k ] = val
90+ }
91+ }
92+ }
93+
94+ return data
95+ }
96+
8497func AuthMiddleware (next http.Handler , token string ) http.Handler {
8598 return http .HandlerFunc (func (w http.ResponseWriter , req * http.Request ) {
8699 if token == "" {
@@ -169,7 +182,7 @@ func BlockedEndpointMiddleware(next http.Handler, BLOCKED_ENDPOINTS []string) ht
169182 })
170183}
171184
172- func TemplatingMiddleware (next http.Handler , VARIABLES map [string ]string ) http.Handler {
185+ func TemplatingMiddleware (next http.Handler , VARIABLES map [string ]interface {} ) http.Handler {
173186 return http .HandlerFunc (func (w http.ResponseWriter , req * http.Request ) {
174187 if req .Body != nil {
175188 bodyBytes , err := io .ReadAll (req .Body )
@@ -182,23 +195,19 @@ func TemplatingMiddleware(next http.Handler, VARIABLES map[string]string) http.H
182195
183196 req .Body .Close ()
184197
185- modifiedBody := string (bodyBytes )
186-
187- modifiedBody , _ = renderTemplate ("json" , modifiedBody , VARIABLES )
198+ var modifiedBodyData map [string ]interface {}
188199
189- modifiedBodyBytes := [] byte ( modifiedBody )
200+ err = json . Unmarshal ( bodyBytes , & modifiedBodyData )
190201
191- if req .URL .RawQuery != "" {
192- var modifiedBodyData map [string ]interface {}
202+ if err != nil {
203+ log .Error ("Could not decode Body: " , err .Error ())
204+ http .Error (w , "Internal Error" , http .StatusInternalServerError )
205+ return
206+ }
193207
194- err = json .Unmarshal (modifiedBodyBytes , & modifiedBodyData )
195-
196- if err != nil {
197- log .Error ("Could not decode Body: " , err .Error ())
198- http .Error (w , "Internal Error" , http .StatusInternalServerError )
199- return
200- }
208+ modifiedBodyData = templateJSON (modifiedBodyData , VARIABLES )
201209
210+ if req .URL .RawQuery != "" {
202211 query , _ := renderTemplate ("query" , req .URL .RawQuery , VARIABLES )
203212
204213 modifiedQuery := req .URL .Query ()
@@ -217,19 +226,19 @@ func TemplatingMiddleware(next http.Handler, VARIABLES map[string]string) http.H
217226
218227 req .URL .RawQuery = modifiedQuery .Encode ()
219228
220- modifiedBodyBytes , err = json .Marshal (modifiedBodyData )
221-
222- if err != nil {
223- log .Error ("Could not encode Body: " , err .Error ())
224- http .Error (w , "Internal Error" , http .StatusInternalServerError )
225- return
226- }
227-
228229 log .Debug ("Applied Query Templating: " , query )
230+ }
229231
230- modifiedBody = string (modifiedBodyBytes )
232+ modifiedBodyBytes , err := json .Marshal (modifiedBodyData )
233+
234+ if err != nil {
235+ log .Error ("Could not encode Body: " , err .Error ())
236+ http .Error (w , "Internal Error" , http .StatusInternalServerError )
237+ return
231238 }
232-
239+
240+ modifiedBody := string (modifiedBodyBytes )
241+
233242 log .Debug ("Applied Body Templating: " , modifiedBody )
234243
235244 req .Body = io .NopCloser (bytes .NewReader (modifiedBodyBytes ))
0 commit comments