how to solve my issues with "template" and "include" directive

7 views
Skip to first unread message

cina moradi

unread,
Apr 7, 2025, 9:15:23 AMApr 7
to KrakenD Community
hello everyone, i have following templates:
{{/* Standard Backend Template with support for all modifiers */}}
{{define "standard_backend"}}
{
  "url_pattern": "{{.url_pattern}}",
  "method": "{{.method | default "GET"}}",
  "encoding": "{{.encoding | default "json"}}",
  "group": "{{.group | default ""}}",
  "host": [{{range $i, $h := .host}}{{if $i}}, {{end}}"{{$h}}"{{end}}],
  "disable_host_sanitize": {{.disable_host_sanitize | default true}},
  "sd": "{{.sd | default "static"}}",
  "sd_scheme": "{{.sd_scheme | default "http"}}",
  {{if .target}}"target": "{{.target}}",{{end}}
  {{if .is_collection}}"is_collection": {{.is_collection}},{{end}}
  {{if .mapping}}"mapping": {{.mapping}},{{end}}
  {{if .timeout}}"timeout": "{{.timeout}}",{{end}}
  {{if .allow}}"allow": {{.allow}},{{end}}
  {{if .deny}}"deny": {{.deny}},{{end}}
  {{if .input_headers}}"input_headers": {{.input_headers}},{{end}}
  {{if .input_query_strings}}"input_query_strings": {{.input_query_strings}},{{end}}
  "extra_config": {
    {{if .backend_modifiers}}
    {{template "backend_modifiers" .backend_modifiers}}
    {{end}}
  }
}
{{end}}


{{/* Complete Endpoint Template with support for all modifiers */}}
{{define "complete_endpoint"}}
{
  "endpoint": "{{.endpoint}}",
  "method": "{{.method | default "GET"}}",
  "output_encoding": "{{.output_encoding | default "json"}}",
  {{if .input_headers}}"input_headers": {{.input_headers}},{{end}}
  {{if .input_query_strings}}"input_query_strings": {{.input_query_strings}},{{end}}
  "timeout": "{{.timeout | default "3s"}}",
  "cache_ttl": "{{.cache_ttl | default "0s"}}",
  {{if .concurrent_calls}}"concurrent_calls": {{.concurrent_calls}},{{end}}
  "extra_config": {
    {{if .extra_config}}
      {{range $i, $mod := .extra_config}}
      {{if $i}},{{end}}
      {{$mod}}
      {{end}}
    {{end}}
  },
  "backend": [
    {{if .backends}}
      {{range $i, $b := .backends}}
      {{if $i}},{{end}}
      {{$b}}
      {{end}}
    {{end}}
  ]
}
{{end}}


my problem is i cant assigne template result into a variable and also i cant pass context with include directive, i want to do something like this with above helper templates:
{{/* ============================================================================
     Transaction History Endpoint and Backend
     ============================================================================ */}}
{{$history_backend := include "define_backend" (dict
  "url_pattern" "/v2/user/{user_id}/history"
  "method"      "GET"
  "host"        (list "{{.host}}{{.path}}")
)}}

{{template "define_endpoint" (dict
  "endpoint"           "/v1/users/{user_id}/transaction-history"
  "method"             "GET"
  "input_headers"      $headers
  "input_query_strings" (list "page" "per_page")
  "backends"           (list $history_backend)
)}},

tell me what should i do to do this modular architecture for endpoint creation
Reply all
Reply to author
Forward
0 new messages