Skip to content
Snippets Groups Projects
logging.gotmpl 1.14 KiB
// Code generated by go-swagger/go-orusapi; DO NOT EDIT.

{{ if .Copyright -}}// {{ comment .Copyright -}}{{ end }}


package {{.Package}}
{{ $package := .Package }}

import (
  {{ range $key, $value := .Imports }}{{ $key }} {{ printf "%q" $value }}
  {{ end }}
)

func LogOperation(tag, operationID string) func (http.Handler) http.Handler {
    return func(next http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
            log := zerolog.Ctx(r.Context())

			log.UpdateContext(func(c zerolog.Context) zerolog.Context {
				return c.Str("api-tag", tag).Str("api-operation-id", operationID)
			})

			next.ServeHTTP(w, r)
		})
	}
}

func (o *{{ pascalize .Name }}API) LoggingInstrumentHandlers() {
    o.Init()
    {{range .Operations}}
    {{.ReceiverName}}.handlers[{{ printf "%q" (upper .Method) }}][{{ if eq .Path "/" }}""{{ else }}{{ printf "%q" (cleanPath .Path) }}{{ end }}] = LogOperation(
        "{{ range .Tags }}{{ . }}{{ end }}", "{{.Name}}",
    )({{.ReceiverName}}.handlers[{{ printf "%q" (upper .Method) }}][{{ if eq .Path "/" }}""{{ else }}{{ printf "%q" (cleanPath .Path) }}{{ end }}])
    {{end}}
}