short/internal/oas/oas_handlers_gen.go

234 lines
5.9 KiB
Go
Raw Permalink Normal View History

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