short/internal/oas/oas_handlers_gen.go

228 lines
5.5 KiB
Go

// Code generated by ogen, DO NOT EDIT.
package oas
import (
"context"
"net/http"
"time"
"github.com/go-faster/errors"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/metric"
semconv "go.opentelemetry.io/otel/semconv/v1.19.0"
"go.opentelemetry.io/otel/trace"
ht "github.com/ogen-go/ogen/http"
"github.com/ogen-go/ogen/middleware"
"github.com/ogen-go/ogen/ogenerrors"
)
// handleCreatePostRequest handles POST /create operation.
//
// POST /create
func (s *Server) handleCreatePostRequest(args [0]string, argsEscaped bool, w http.ResponseWriter, r *http.Request) {
otelAttrs := []attribute.KeyValue{
semconv.HTTPMethodKey.String("POST"),
semconv.HTTPRouteKey.String("/create"),
}
// Start a span for this request.
ctx, span := s.cfg.Tracer.Start(r.Context(), "CreatePost",
trace.WithAttributes(otelAttrs...),
serverSpanKind,
)
defer span.End()
// Run stopwatch.
startTime := time.Now()
defer func() {
elapsedDuration := time.Since(startTime)
// Use floating point division here for higher precision (instead of Millisecond method).
s.duration.Record(ctx, float64(float64(elapsedDuration)/float64(time.Millisecond)), metric.WithAttributes(otelAttrs...))
}()
// Increment request counter.
s.requests.Add(ctx, 1, metric.WithAttributes(otelAttrs...))
var (
recordError = func(stage string, err error) {
span.RecordError(err)
span.SetStatus(codes.Error, stage)
s.errors.Add(ctx, 1, metric.WithAttributes(otelAttrs...))
}
err error
opErrContext = ogenerrors.OperationContext{
Name: "CreatePost",
ID: "",
}
)
request, close, err := s.decodeCreatePostRequest(r)
if err != nil {
err = &ogenerrors.DecodeRequestError{
OperationContext: opErrContext,
Err: err,
}
recordError("DecodeRequest", err)
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
defer func() {
if err := close(); err != nil {
recordError("CloseRequest", err)
}
}()
var response CreatePostRes
if m := s.cfg.Middleware; m != nil {
mreq := middleware.Request{
Context: ctx,
OperationName: "CreatePost",
OperationSummary: "",
OperationID: "",
Body: request,
Params: middleware.Parameters{},
Raw: r,
}
type (
Request = *CreatePostReq
Params = struct{}
Response = CreatePostRes
)
response, err = middleware.HookMiddleware[
Request,
Params,
Response,
](
m,
mreq,
nil,
func(ctx context.Context, request Request, params Params) (response Response, err error) {
response, err = s.h.CreatePost(ctx, request)
return response, err
},
)
} else {
response, err = s.h.CreatePost(ctx, request)
}
if err != nil {
recordError("Internal", err)
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
if err := encodeCreatePostResponse(response, w, span); err != nil {
recordError("EncodeResponse", err)
if !errors.Is(err, ht.ErrInternalServerErrorResponse) {
s.cfg.ErrorHandler(ctx, w, r, err)
}
return
}
}
// handleHashGetRequest handles GET /{hash} operation.
//
// Redirect client to long URL.
//
// GET /{hash}
func (s *Server) handleHashGetRequest(args [1]string, argsEscaped bool, w http.ResponseWriter, r *http.Request) {
otelAttrs := []attribute.KeyValue{
semconv.HTTPMethodKey.String("GET"),
semconv.HTTPRouteKey.String("/{hash}"),
}
// Start a span for this request.
ctx, span := s.cfg.Tracer.Start(r.Context(), "HashGet",
trace.WithAttributes(otelAttrs...),
serverSpanKind,
)
defer span.End()
// Run stopwatch.
startTime := time.Now()
defer func() {
elapsedDuration := time.Since(startTime)
// Use floating point division here for higher precision (instead of Millisecond method).
s.duration.Record(ctx, float64(float64(elapsedDuration)/float64(time.Millisecond)), metric.WithAttributes(otelAttrs...))
}()
// Increment request counter.
s.requests.Add(ctx, 1, metric.WithAttributes(otelAttrs...))
var (
recordError = func(stage string, err error) {
span.RecordError(err)
span.SetStatus(codes.Error, stage)
s.errors.Add(ctx, 1, metric.WithAttributes(otelAttrs...))
}
err error
opErrContext = ogenerrors.OperationContext{
Name: "HashGet",
ID: "",
}
)
params, err := decodeHashGetParams(args, argsEscaped, r)
if err != nil {
err = &ogenerrors.DecodeParamsError{
OperationContext: opErrContext,
Err: err,
}
recordError("DecodeParams", err)
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
var response HashGetRes
if m := s.cfg.Middleware; m != nil {
mreq := middleware.Request{
Context: ctx,
OperationName: "HashGet",
OperationSummary: "",
OperationID: "",
Body: nil,
Params: middleware.Parameters{
{
Name: "hash",
In: "path",
}: params.Hash,
},
Raw: r,
}
type (
Request = struct{}
Params = HashGetParams
Response = HashGetRes
)
response, err = middleware.HookMiddleware[
Request,
Params,
Response,
](
m,
mreq,
unpackHashGetParams,
func(ctx context.Context, request Request, params Params) (response Response, err error) {
response, err = s.h.HashGet(ctx, params)
return response, err
},
)
} else {
response, err = s.h.HashGet(ctx, params)
}
if err != nil {
recordError("Internal", err)
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
if err := encodeHashGetResponse(response, w, span); err != nil {
recordError("EncodeResponse", err)
if !errors.Is(err, ht.ErrInternalServerErrorResponse) {
s.cfg.ErrorHandler(ctx, w, r, err)
}
return
}
}