diff --git a/server.yaml b/server.yaml new file mode 100644 index 0000000000000000000000000000000000000000..74d0ea3afa2f3b980cb95edb11ebd98a5112d692_c2VydmVyLnlhbWw= --- /dev/null +++ b/server.yaml @@ -0,0 +1,67 @@ +layout: + application: + # built-in templates + - name: configure + source: asset:serverConfigureapi + target: "{{ joinFilePath .Target .ServerPackage }}" + file_name: "configure_{{ .Name }}.go" + skip_exists: true +# - name: main +# source: asset:serverMain +# target: "{{ joinFilePath .Target \"cmd\" (dasherize (pascalize .Name)) }}-server" +# file_name: "main.go" + - name: embedded_spec + source: asset:swaggerJsonEmbed + target: "{{ joinFilePath .Target .ServerPackage }}" + file_name: "embedded_spec.go" + - name: server + source: asset:serverServer + target: "{{ joinFilePath .Target .ServerPackage }}" + file_name: "server.go" + - name: builder + source: asset:serverBuilder + target: "{{ joinFilePath .Target .ServerPackage .Package }}" + file_name: "{{ snakize (pascalize .Name) }}_api.go" + - name: doc + source: asset:serverDoc + target: "{{ joinFilePath .Target .ServerPackage }}" + file_name: "doc.go" + + # custom templates + - name: builder + source: server/initPrometheus.gotmpl + target: "{{ joinFilePath .Target .ServerPackage .Package }}" + file_name: "prometheus.go" + - name: logging + source: server/logging.gotmpl + target: "{{ if gt (len .Tags) 0 }}{{ joinFilePath .Target .ServerPackage .APIPackage .Package }}{{ else }}{{ joinFilePath .Target .ServerPackage .Package }}{{ end }}" + file_name: "logging.go" + + models: + # built-in templates + - name: definition + source: asset:model + target: "{{ joinFilePath .Target .ModelPackage }}" + file_name: "{{ (snakize (pascalize .Name)) }}.go" + operations: + # built-in templates + - name: parameters + source: asset:serverParameter + target: "{{ if gt (len .Tags) 0 }}{{ joinFilePath .Target .ServerPackage .APIPackage .Package }}{{ else }}{{ joinFilePath .Target .ServerPackage .Package }}{{ end }}" + file_name: "{{ (snakize (pascalize .Name)) }}_parameters.go" + - name: responses + source: asset:serverResponses + target: "{{ if gt (len .Tags) 0 }}{{ joinFilePath .Target .ServerPackage .APIPackage .Package }}{{ else }}{{ joinFilePath .Target .ServerPackage .Package }}{{ end }}" + file_name: "{{ (snakize (pascalize .Name)) }}_responses.go" + - name: handler + source: asset:serverOperation + target: "{{ if gt (len .Tags) 0 }}{{ joinFilePath .Target .ServerPackage .APIPackage .Package }}{{ else }}{{ joinFilePath .Target .ServerPackage .Package }}{{ end }}" + file_name: "{{ (snakize (pascalize .Name)) }}.go" + + # custom templates + - name: prometheus + source: server/prometheus.gotmpl + target: "{{ if gt (len .Tags) 0 }}{{ joinFilePath .Target .ServerPackage .APIPackage .Package }}{{ else }}{{ joinFilePath .Target .ServerPackage .Package }}{{ end }}" + file_name: "{{ (snakize (pascalize .Name)) }}_prometheus.go" + + operation_groups: diff --git a/templates/server/initPrometheus.gotmpl b/templates/server/initPrometheus.gotmpl new file mode 100644 index 0000000000000000000000000000000000000000..74d0ea3afa2f3b980cb95edb11ebd98a5112d692_dGVtcGxhdGVzL3NlcnZlci9pbml0UHJvbWV0aGV1cy5nb3RtcGw= --- /dev/null +++ b/templates/server/initPrometheus.gotmpl @@ -0,0 +1,17 @@ +// Code generated by go-swagger with a alpe-api template; DO NOT EDIT. + +package {{.Package}} +{{ $package := .Package }} + +import ( + {{ range $key, $value := .Imports }}{{ $key }} {{ printf "%q" $value }} + {{ end }} +) + +func (o *{{ pascalize .Name }}API) PrometheusInstrumentHandlers() { + o.Init() + {{range .Operations}} + {{.ReceiverName}}.handlers[{{ printf "%q" (upper .Method) }}][{{ if eq .Path "/" }}""{{ else }}{{ printf "%q" (cleanPath .Path) }}{{ end }}] = {{if ne .Package $package}}{{.Package}}.{{end}}{{ pascalize .Name }}InstrumentHandler( + {{.ReceiverName}}.handlers[{{ printf "%q" (upper .Method) }}][{{ if eq .Path "/" }}""{{ else }}{{ printf "%q" (cleanPath .Path) }}{{ end }}]) + {{end}} +} diff --git a/templates/server/logging.gotmpl b/templates/server/logging.gotmpl new file mode 100644 index 0000000000000000000000000000000000000000..74d0ea3afa2f3b980cb95edb11ebd98a5112d692_dGVtcGxhdGVzL3NlcnZlci9sb2dnaW5nLmdvdG1wbA== --- /dev/null +++ b/templates/server/logging.gotmpl @@ -0,0 +1,32 @@ +// Code generated by go-swagger with a alpe-api template; DO NOT EDIT. + +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}} +} diff --git a/templates/server/prometheus.gotmpl b/templates/server/prometheus.gotmpl new file mode 100644 index 0000000000000000000000000000000000000000..74d0ea3afa2f3b980cb95edb11ebd98a5112d692_dGVtcGxhdGVzL3NlcnZlci9wcm9tZXRoZXVzLmdvdG1wbA== --- /dev/null +++ b/templates/server/prometheus.gotmpl @@ -0,0 +1,49 @@ +// Code generated by go-swagger with a alpe-api template; DO NOT EDIT. + +// Copyright Orus.io team + +package {{ .Package }} + +import ( + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promhttp" +) + +var ( + {{ camelize .Name }}HandlerDuration = prometheus.NewSummaryVec( + prometheus.SummaryOpts{ + Name: "alpe_api_handler", + Help: "The handlers duration and count.", + Objectives: map[float64]float64{0.9: 0.01, 0.95: 0.01, 0.99: 0.001}, + ConstLabels: map[string]string{ + "service": "{{.Name}}", + }, + }, + []string{"code"}, + ) + + {{ camelize .Name }}HandlerInFlight = prometheus.NewGauge( + prometheus.GaugeOpts{ + Name: "alpe_api_handler_in_flights", + Help: "The number of handlers currently running.", + ConstLabels: map[string]string{ + "service": "{{.Name}}", + }, + }, + ) +) + +func {{ pascalize .Name }}InstrumentHandler(handler http.Handler) http.Handler { + return promhttp.InstrumentHandlerInFlight({{ camelize .Name }}HandlerInFlight, + promhttp.InstrumentHandlerDuration({{ camelize .Name }}HandlerDuration, + handler, + ), + ) +} + +func init() { + prometheus.MustRegister( + {{ camelize .Name }}HandlerDuration, + {{ camelize .Name }}HandlerInFlight, + ) +}