// 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" "github.com/ogen-go/ogen/otelogen" ) // handleCreateShortURLRequest handles createShortURL operation. // // Create a shorten URL. // // POST /create func (s *Server) handleCreateShortURLRequest(args [0]string, argsEscaped bool, w http.ResponseWriter, r *http.Request) { otelAttrs := []attribute.KeyValue{ otelogen.OperationID("createShortURL"), semconv.HTTPMethodKey.String("POST"), semconv.HTTPRouteKey.String("/create"), } // Start a span for this request. ctx, span := s.cfg.Tracer.Start(r.Context(), "CreateShortURL", 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: "CreateShortURL", ID: "createShortURL", } ) request, close, err := s.decodeCreateShortURLRequest(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 CreateShortURLRes if m := s.cfg.Middleware; m != nil { mreq := middleware.Request{ Context: ctx, OperationName: "CreateShortURL", OperationSummary: "", OperationID: "createShortURL", Body: request, Params: middleware.Parameters{}, Raw: r, } type ( Request = *CreateShortURLReq Params = struct{} Response = CreateShortURLRes ) 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.CreateShortURL(ctx, request) return response, err }, ) } else { response, err = s.h.CreateShortURL(ctx, request) } if err != nil { recordError("Internal", err) s.cfg.ErrorHandler(ctx, w, r, err) return } if err := encodeCreateShortURLResponse(response, w, span); err != nil { recordError("EncodeResponse", err) if !errors.Is(err, ht.ErrInternalServerErrorResponse) { s.cfg.ErrorHandler(ctx, w, r, err) } return } } // handleRedirectLongURLRequest handles redirectLongURL operation. // // Redirect client to long URL. // // GET /{hash} func (s *Server) handleRedirectLongURLRequest(args [1]string, argsEscaped bool, w http.ResponseWriter, r *http.Request) { otelAttrs := []attribute.KeyValue{ otelogen.OperationID("redirectLongURL"), semconv.HTTPMethodKey.String("GET"), semconv.HTTPRouteKey.String("/{hash}"), } // Start a span for this request. ctx, span := s.cfg.Tracer.Start(r.Context(), "RedirectLongURL", 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: "RedirectLongURL", ID: "redirectLongURL", } ) params, err := decodeRedirectLongURLParams(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 RedirectLongURLRes if m := s.cfg.Middleware; m != nil { mreq := middleware.Request{ Context: ctx, OperationName: "RedirectLongURL", OperationSummary: "", OperationID: "redirectLongURL", Body: nil, Params: middleware.Parameters{ { Name: "hash", In: "path", }: params.Hash, }, Raw: r, } type ( Request = struct{} Params = RedirectLongURLParams Response = RedirectLongURLRes ) response, err = middleware.HookMiddleware[ Request, Params, Response, ]( m, mreq, unpackRedirectLongURLParams, func(ctx context.Context, request Request, params Params) (response Response, err error) { response, err = s.h.RedirectLongURL(ctx, params) return response, err }, ) } else { response, err = s.h.RedirectLongURL(ctx, params) } if err != nil { recordError("Internal", err) s.cfg.ErrorHandler(ctx, w, r, err) return } if err := encodeRedirectLongURLResponse(response, w, span); err != nil { recordError("EncodeResponse", err) if !errors.Is(err, ht.ErrInternalServerErrorResponse) { s.cfg.ErrorHandler(ctx, w, r, err) } return } }