maps-service/pkg/api/oas_handlers_gen.go

4272 lines
116 KiB
Go
Raw Permalink Normal View History

2024-11-26 01:22:45 +00:00
// Code generated by ogen, DO NOT EDIT.
package api
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"
2024-11-26 20:39:10 +00:00
semconv "go.opentelemetry.io/otel/semconv/v1.26.0"
2024-11-26 01:22:45 +00:00
"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-12-06 00:36:15 +00:00
type codeRecorder struct {
http.ResponseWriter
status int
}
func (c *codeRecorder) WriteHeader(status int) {
c.status = status
c.ResponseWriter.WriteHeader(status)
}
// handleActionSubmissionPublishRequest handles actionSubmissionPublish operation.
//
2024-12-12 01:54:11 +00:00
// (Internal endpoint) Role Validator changes status from Publishing -> Published.
//
2024-12-12 01:54:11 +00:00
// POST /submissions/{SubmissionID}/status/validator-published
func (s *Server) handleActionSubmissionPublishRequest(args [1]string, argsEscaped bool, w http.ResponseWriter, r *http.Request) {
2024-12-06 00:36:15 +00:00
statusWriter := &codeRecorder{ResponseWriter: w}
w = statusWriter
otelAttrs := []attribute.KeyValue{
otelogen.OperationID("actionSubmissionPublish"),
2024-12-10 06:06:33 +00:00
semconv.HTTPRequestMethodKey.String("POST"),
2024-12-12 01:54:11 +00:00
semconv.HTTPRouteKey.String("/submissions/{SubmissionID}/status/validator-published"),
}
// Start a span for this request.
2024-11-29 22:33:48 +00:00
ctx, span := s.cfg.Tracer.Start(r.Context(), ActionSubmissionPublishOperation,
trace.WithAttributes(otelAttrs...),
serverSpanKind,
)
defer span.End()
// Add Labeler to context.
labeler := &Labeler{attrs: otelAttrs}
ctx = contextWithLabeler(ctx, labeler)
// Run stopwatch.
startTime := time.Now()
defer func() {
elapsedDuration := time.Since(startTime)
2024-12-06 00:36:15 +00:00
attrSet := labeler.AttributeSet()
attrs := attrSet.ToSlice()
code := statusWriter.status
if code != 0 {
codeAttr := semconv.HTTPResponseStatusCode(code)
attrs = append(attrs, codeAttr)
span.SetAttributes(codeAttr)
}
attrOpt := metric.WithAttributes(attrs...)
// Increment request counter.
s.requests.Add(ctx, 1, attrOpt)
// Use floating point division here for higher precision (instead of Millisecond method).
2024-12-06 00:36:15 +00:00
s.duration.Record(ctx, float64(elapsedDuration)/float64(time.Millisecond), attrOpt)
}()
var (
recordError = func(stage string, err error) {
span.RecordError(err)
2024-12-06 00:36:15 +00:00
// https://opentelemetry.io/docs/specs/semconv/http/http-spans/#status
// Span Status MUST be left unset if HTTP status code was in the 1xx, 2xx or 3xx ranges,
// unless there was another error (e.g., network error receiving the response body; or 3xx codes with
// max redirects exceeded), in which case status MUST be set to Error.
code := statusWriter.status
if code >= 100 && code < 500 {
span.SetStatus(codes.Error, stage)
}
attrSet := labeler.AttributeSet()
attrs := attrSet.ToSlice()
if code != 0 {
attrs = append(attrs, semconv.HTTPResponseStatusCode(code))
}
s.errors.Add(ctx, 1, metric.WithAttributes(attrs...))
}
err error
opErrContext = ogenerrors.OperationContext{
2024-11-29 22:33:48 +00:00
Name: ActionSubmissionPublishOperation,
ID: "actionSubmissionPublish",
}
)
params, err := decodeActionSubmissionPublishParams(args, argsEscaped, r)
if err != nil {
err = &ogenerrors.DecodeParamsError{
OperationContext: opErrContext,
Err: err,
}
defer recordError("DecodeParams", err)
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
2024-12-11 06:23:08 +00:00
var response *ActionSubmissionPublishNoContent
if m := s.cfg.Middleware; m != nil {
mreq := middleware.Request{
Context: ctx,
2024-11-29 22:33:48 +00:00
OperationName: ActionSubmissionPublishOperation,
2024-12-12 01:54:11 +00:00
OperationSummary: "(Internal endpoint) Role Validator changes status from Publishing -> Published",
OperationID: "actionSubmissionPublish",
Body: nil,
Params: middleware.Parameters{
{
Name: "SubmissionID",
In: "path",
}: params.SubmissionID,
},
Raw: r,
}
type (
Request = struct{}
Params = ActionSubmissionPublishParams
2024-12-11 06:23:08 +00:00
Response = *ActionSubmissionPublishNoContent
)
response, err = middleware.HookMiddleware[
Request,
Params,
Response,
](
m,
mreq,
unpackActionSubmissionPublishParams,
func(ctx context.Context, request Request, params Params) (response Response, err error) {
err = s.h.ActionSubmissionPublish(ctx, params)
return response, err
},
)
} else {
err = s.h.ActionSubmissionPublish(ctx, params)
}
if err != nil {
if errRes, ok := errors.Into[*ErrorStatusCode](err); ok {
if err := encodeErrorResponse(errRes, w, span); err != nil {
defer recordError("Internal", err)
}
return
}
if errors.Is(err, ht.ErrNotImplemented) {
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
if err := encodeErrorResponse(s.h.NewError(ctx, err), w, span); err != nil {
defer recordError("Internal", err)
}
return
}
if err := encodeActionSubmissionPublishResponse(response, w, span); err != nil {
defer recordError("EncodeResponse", err)
if !errors.Is(err, ht.ErrInternalServerErrorResponse) {
s.cfg.ErrorHandler(ctx, w, r, err)
}
return
}
}
// handleActionSubmissionRejectRequest handles actionSubmissionReject operation.
//
// Role Reviewer changes status from Submitted -> Rejected.
//
2024-12-10 06:06:33 +00:00
// POST /submissions/{SubmissionID}/status/reject
func (s *Server) handleActionSubmissionRejectRequest(args [1]string, argsEscaped bool, w http.ResponseWriter, r *http.Request) {
2024-12-06 00:36:15 +00:00
statusWriter := &codeRecorder{ResponseWriter: w}
w = statusWriter
otelAttrs := []attribute.KeyValue{
otelogen.OperationID("actionSubmissionReject"),
2024-12-10 06:06:33 +00:00
semconv.HTTPRequestMethodKey.String("POST"),
semconv.HTTPRouteKey.String("/submissions/{SubmissionID}/status/reject"),
}
// Start a span for this request.
2024-11-29 22:33:48 +00:00
ctx, span := s.cfg.Tracer.Start(r.Context(), ActionSubmissionRejectOperation,
trace.WithAttributes(otelAttrs...),
serverSpanKind,
)
defer span.End()
// Add Labeler to context.
labeler := &Labeler{attrs: otelAttrs}
ctx = contextWithLabeler(ctx, labeler)
// Run stopwatch.
startTime := time.Now()
defer func() {
elapsedDuration := time.Since(startTime)
2024-12-06 00:36:15 +00:00
attrSet := labeler.AttributeSet()
attrs := attrSet.ToSlice()
code := statusWriter.status
if code != 0 {
codeAttr := semconv.HTTPResponseStatusCode(code)
attrs = append(attrs, codeAttr)
span.SetAttributes(codeAttr)
}
attrOpt := metric.WithAttributes(attrs...)
// Increment request counter.
s.requests.Add(ctx, 1, attrOpt)
// Use floating point division here for higher precision (instead of Millisecond method).
2024-12-06 00:36:15 +00:00
s.duration.Record(ctx, float64(elapsedDuration)/float64(time.Millisecond), attrOpt)
}()
var (
recordError = func(stage string, err error) {
span.RecordError(err)
2024-12-06 00:36:15 +00:00
// https://opentelemetry.io/docs/specs/semconv/http/http-spans/#status
// Span Status MUST be left unset if HTTP status code was in the 1xx, 2xx or 3xx ranges,
// unless there was another error (e.g., network error receiving the response body; or 3xx codes with
// max redirects exceeded), in which case status MUST be set to Error.
code := statusWriter.status
if code >= 100 && code < 500 {
span.SetStatus(codes.Error, stage)
}
attrSet := labeler.AttributeSet()
attrs := attrSet.ToSlice()
if code != 0 {
attrs = append(attrs, semconv.HTTPResponseStatusCode(code))
}
s.errors.Add(ctx, 1, metric.WithAttributes(attrs...))
}
err error
opErrContext = ogenerrors.OperationContext{
2024-11-29 22:33:48 +00:00
Name: ActionSubmissionRejectOperation,
ID: "actionSubmissionReject",
}
)
2024-11-29 22:33:48 +00:00
{
type bitset = [1]uint8
var satisfied bitset
{
sctx, ok, err := s.securityCookieAuth(ctx, ActionSubmissionRejectOperation, r)
if err != nil {
err = &ogenerrors.SecurityError{
OperationContext: opErrContext,
Security: "CookieAuth",
Err: err,
}
if encodeErr := encodeErrorResponse(s.h.NewError(ctx, err), w, span); encodeErr != nil {
defer recordError("Security:CookieAuth", err)
}
return
}
if ok {
satisfied[0] |= 1 << 0
ctx = sctx
}
}
if ok := func() bool {
nextRequirement:
for _, requirement := range []bitset{
{0b00000001},
} {
for i, mask := range requirement {
if satisfied[i]&mask != mask {
continue nextRequirement
}
}
return true
}
return false
}(); !ok {
err = &ogenerrors.SecurityError{
OperationContext: opErrContext,
Err: ogenerrors.ErrSecurityRequirementIsNotSatisfied,
}
if encodeErr := encodeErrorResponse(s.h.NewError(ctx, err), w, span); encodeErr != nil {
defer recordError("Security", err)
}
return
}
}
params, err := decodeActionSubmissionRejectParams(args, argsEscaped, r)
if err != nil {
err = &ogenerrors.DecodeParamsError{
OperationContext: opErrContext,
Err: err,
}
defer recordError("DecodeParams", err)
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
2024-12-11 06:23:08 +00:00
var response *ActionSubmissionRejectNoContent
if m := s.cfg.Middleware; m != nil {
mreq := middleware.Request{
Context: ctx,
2024-11-29 22:33:48 +00:00
OperationName: ActionSubmissionRejectOperation,
OperationSummary: "Role Reviewer changes status from Submitted -> Rejected",
OperationID: "actionSubmissionReject",
Body: nil,
Params: middleware.Parameters{
{
Name: "SubmissionID",
In: "path",
}: params.SubmissionID,
},
Raw: r,
}
type (
Request = struct{}
Params = ActionSubmissionRejectParams
2024-12-11 06:23:08 +00:00
Response = *ActionSubmissionRejectNoContent
)
response, err = middleware.HookMiddleware[
Request,
Params,
Response,
](
m,
mreq,
unpackActionSubmissionRejectParams,
func(ctx context.Context, request Request, params Params) (response Response, err error) {
err = s.h.ActionSubmissionReject(ctx, params)
return response, err
},
)
} else {
err = s.h.ActionSubmissionReject(ctx, params)
}
if err != nil {
if errRes, ok := errors.Into[*ErrorStatusCode](err); ok {
if err := encodeErrorResponse(errRes, w, span); err != nil {
defer recordError("Internal", err)
}
return
}
if errors.Is(err, ht.ErrNotImplemented) {
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
if err := encodeErrorResponse(s.h.NewError(ctx, err), w, span); err != nil {
defer recordError("Internal", err)
}
return
}
if err := encodeActionSubmissionRejectResponse(response, w, span); err != nil {
defer recordError("EncodeResponse", err)
if !errors.Is(err, ht.ErrInternalServerErrorResponse) {
s.cfg.ErrorHandler(ctx, w, r, err)
}
return
}
}
// handleActionSubmissionRequestChangesRequest handles actionSubmissionRequestChanges operation.
//
// Role Reviewer changes status from Validated|Accepted|Submitted -> ChangesRequested.
//
2024-12-10 06:06:33 +00:00
// POST /submissions/{SubmissionID}/status/request-changes
func (s *Server) handleActionSubmissionRequestChangesRequest(args [1]string, argsEscaped bool, w http.ResponseWriter, r *http.Request) {
2024-12-06 00:36:15 +00:00
statusWriter := &codeRecorder{ResponseWriter: w}
w = statusWriter
otelAttrs := []attribute.KeyValue{
otelogen.OperationID("actionSubmissionRequestChanges"),
2024-12-10 06:06:33 +00:00
semconv.HTTPRequestMethodKey.String("POST"),
semconv.HTTPRouteKey.String("/submissions/{SubmissionID}/status/request-changes"),
}
// Start a span for this request.
2024-11-29 22:33:48 +00:00
ctx, span := s.cfg.Tracer.Start(r.Context(), ActionSubmissionRequestChangesOperation,
trace.WithAttributes(otelAttrs...),
serverSpanKind,
)
defer span.End()
// Add Labeler to context.
labeler := &Labeler{attrs: otelAttrs}
ctx = contextWithLabeler(ctx, labeler)
// Run stopwatch.
startTime := time.Now()
defer func() {
elapsedDuration := time.Since(startTime)
2024-12-06 00:36:15 +00:00
attrSet := labeler.AttributeSet()
attrs := attrSet.ToSlice()
code := statusWriter.status
if code != 0 {
codeAttr := semconv.HTTPResponseStatusCode(code)
attrs = append(attrs, codeAttr)
span.SetAttributes(codeAttr)
}
attrOpt := metric.WithAttributes(attrs...)
// Increment request counter.
s.requests.Add(ctx, 1, attrOpt)
// Use floating point division here for higher precision (instead of Millisecond method).
2024-12-06 00:36:15 +00:00
s.duration.Record(ctx, float64(elapsedDuration)/float64(time.Millisecond), attrOpt)
}()
var (
recordError = func(stage string, err error) {
span.RecordError(err)
2024-12-06 00:36:15 +00:00
// https://opentelemetry.io/docs/specs/semconv/http/http-spans/#status
// Span Status MUST be left unset if HTTP status code was in the 1xx, 2xx or 3xx ranges,
// unless there was another error (e.g., network error receiving the response body; or 3xx codes with
// max redirects exceeded), in which case status MUST be set to Error.
code := statusWriter.status
if code >= 100 && code < 500 {
span.SetStatus(codes.Error, stage)
}
attrSet := labeler.AttributeSet()
attrs := attrSet.ToSlice()
if code != 0 {
attrs = append(attrs, semconv.HTTPResponseStatusCode(code))
}
s.errors.Add(ctx, 1, metric.WithAttributes(attrs...))
}
err error
opErrContext = ogenerrors.OperationContext{
2024-11-29 22:33:48 +00:00
Name: ActionSubmissionRequestChangesOperation,
ID: "actionSubmissionRequestChanges",
}
)
2024-11-29 22:33:48 +00:00
{
type bitset = [1]uint8
var satisfied bitset
{
sctx, ok, err := s.securityCookieAuth(ctx, ActionSubmissionRequestChangesOperation, r)
if err != nil {
err = &ogenerrors.SecurityError{
OperationContext: opErrContext,
Security: "CookieAuth",
Err: err,
}
if encodeErr := encodeErrorResponse(s.h.NewError(ctx, err), w, span); encodeErr != nil {
defer recordError("Security:CookieAuth", err)
}
return
}
if ok {
satisfied[0] |= 1 << 0
ctx = sctx
}
}
if ok := func() bool {
nextRequirement:
for _, requirement := range []bitset{
{0b00000001},
} {
for i, mask := range requirement {
if satisfied[i]&mask != mask {
continue nextRequirement
}
}
return true
}
return false
}(); !ok {
err = &ogenerrors.SecurityError{
OperationContext: opErrContext,
Err: ogenerrors.ErrSecurityRequirementIsNotSatisfied,
}
if encodeErr := encodeErrorResponse(s.h.NewError(ctx, err), w, span); encodeErr != nil {
defer recordError("Security", err)
}
return
}
}
params, err := decodeActionSubmissionRequestChangesParams(args, argsEscaped, r)
if err != nil {
err = &ogenerrors.DecodeParamsError{
OperationContext: opErrContext,
Err: err,
}
defer recordError("DecodeParams", err)
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
2024-12-11 06:23:08 +00:00
var response *ActionSubmissionRequestChangesNoContent
if m := s.cfg.Middleware; m != nil {
mreq := middleware.Request{
Context: ctx,
2024-11-29 22:33:48 +00:00
OperationName: ActionSubmissionRequestChangesOperation,
OperationSummary: "Role Reviewer changes status from Validated|Accepted|Submitted -> ChangesRequested",
OperationID: "actionSubmissionRequestChanges",
Body: nil,
Params: middleware.Parameters{
{
Name: "SubmissionID",
In: "path",
}: params.SubmissionID,
},
Raw: r,
}
type (
Request = struct{}
Params = ActionSubmissionRequestChangesParams
2024-12-11 06:23:08 +00:00
Response = *ActionSubmissionRequestChangesNoContent
)
response, err = middleware.HookMiddleware[
Request,
Params,
Response,
](
m,
mreq,
unpackActionSubmissionRequestChangesParams,
func(ctx context.Context, request Request, params Params) (response Response, err error) {
err = s.h.ActionSubmissionRequestChanges(ctx, params)
return response, err
},
)
} else {
err = s.h.ActionSubmissionRequestChanges(ctx, params)
}
if err != nil {
if errRes, ok := errors.Into[*ErrorStatusCode](err); ok {
if err := encodeErrorResponse(errRes, w, span); err != nil {
defer recordError("Internal", err)
}
return
}
if errors.Is(err, ht.ErrNotImplemented) {
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
if err := encodeErrorResponse(s.h.NewError(ctx, err), w, span); err != nil {
defer recordError("Internal", err)
}
return
}
if err := encodeActionSubmissionRequestChangesResponse(response, w, span); err != nil {
defer recordError("EncodeResponse", err)
if !errors.Is(err, ht.ErrInternalServerErrorResponse) {
s.cfg.ErrorHandler(ctx, w, r, err)
}
return
}
}
// handleActionSubmissionRevokeRequest handles actionSubmissionRevoke operation.
//
// Role Submitter changes status from Submitted|ChangesRequested -> UnderConstruction.
//
2024-12-10 06:06:33 +00:00
// POST /submissions/{SubmissionID}/status/revoke
func (s *Server) handleActionSubmissionRevokeRequest(args [1]string, argsEscaped bool, w http.ResponseWriter, r *http.Request) {
2024-12-06 00:36:15 +00:00
statusWriter := &codeRecorder{ResponseWriter: w}
w = statusWriter
otelAttrs := []attribute.KeyValue{
otelogen.OperationID("actionSubmissionRevoke"),
2024-12-10 06:06:33 +00:00
semconv.HTTPRequestMethodKey.String("POST"),
semconv.HTTPRouteKey.String("/submissions/{SubmissionID}/status/revoke"),
}
// Start a span for this request.
2024-11-29 22:33:48 +00:00
ctx, span := s.cfg.Tracer.Start(r.Context(), ActionSubmissionRevokeOperation,
trace.WithAttributes(otelAttrs...),
serverSpanKind,
)
defer span.End()
// Add Labeler to context.
labeler := &Labeler{attrs: otelAttrs}
ctx = contextWithLabeler(ctx, labeler)
// Run stopwatch.
startTime := time.Now()
defer func() {
elapsedDuration := time.Since(startTime)
2024-12-06 00:36:15 +00:00
attrSet := labeler.AttributeSet()
attrs := attrSet.ToSlice()
code := statusWriter.status
if code != 0 {
codeAttr := semconv.HTTPResponseStatusCode(code)
attrs = append(attrs, codeAttr)
span.SetAttributes(codeAttr)
}
attrOpt := metric.WithAttributes(attrs...)
// Increment request counter.
s.requests.Add(ctx, 1, attrOpt)
// Use floating point division here for higher precision (instead of Millisecond method).
2024-12-06 00:36:15 +00:00
s.duration.Record(ctx, float64(elapsedDuration)/float64(time.Millisecond), attrOpt)
}()
var (
recordError = func(stage string, err error) {
span.RecordError(err)
2024-12-06 00:36:15 +00:00
// https://opentelemetry.io/docs/specs/semconv/http/http-spans/#status
// Span Status MUST be left unset if HTTP status code was in the 1xx, 2xx or 3xx ranges,
// unless there was another error (e.g., network error receiving the response body; or 3xx codes with
// max redirects exceeded), in which case status MUST be set to Error.
code := statusWriter.status
if code >= 100 && code < 500 {
span.SetStatus(codes.Error, stage)
}
attrSet := labeler.AttributeSet()
attrs := attrSet.ToSlice()
if code != 0 {
attrs = append(attrs, semconv.HTTPResponseStatusCode(code))
}
s.errors.Add(ctx, 1, metric.WithAttributes(attrs...))
}
err error
opErrContext = ogenerrors.OperationContext{
2024-11-29 22:33:48 +00:00
Name: ActionSubmissionRevokeOperation,
ID: "actionSubmissionRevoke",
}
)
2024-11-29 22:33:48 +00:00
{
type bitset = [1]uint8
var satisfied bitset
{
sctx, ok, err := s.securityCookieAuth(ctx, ActionSubmissionRevokeOperation, r)
if err != nil {
err = &ogenerrors.SecurityError{
OperationContext: opErrContext,
Security: "CookieAuth",
Err: err,
}
if encodeErr := encodeErrorResponse(s.h.NewError(ctx, err), w, span); encodeErr != nil {
defer recordError("Security:CookieAuth", err)
}
return
}
if ok {
satisfied[0] |= 1 << 0
ctx = sctx
}
}
if ok := func() bool {
nextRequirement:
for _, requirement := range []bitset{
{0b00000001},
} {
for i, mask := range requirement {
if satisfied[i]&mask != mask {
continue nextRequirement
}
}
return true
}
return false
}(); !ok {
err = &ogenerrors.SecurityError{
OperationContext: opErrContext,
Err: ogenerrors.ErrSecurityRequirementIsNotSatisfied,
}
if encodeErr := encodeErrorResponse(s.h.NewError(ctx, err), w, span); encodeErr != nil {
defer recordError("Security", err)
}
return
}
}
params, err := decodeActionSubmissionRevokeParams(args, argsEscaped, r)
if err != nil {
err = &ogenerrors.DecodeParamsError{
OperationContext: opErrContext,
Err: err,
}
defer recordError("DecodeParams", err)
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
2024-12-11 06:23:08 +00:00
var response *ActionSubmissionRevokeNoContent
if m := s.cfg.Middleware; m != nil {
mreq := middleware.Request{
Context: ctx,
2024-11-29 22:33:48 +00:00
OperationName: ActionSubmissionRevokeOperation,
OperationSummary: "Role Submitter changes status from Submitted|ChangesRequested -> UnderConstruction",
OperationID: "actionSubmissionRevoke",
Body: nil,
Params: middleware.Parameters{
{
Name: "SubmissionID",
In: "path",
}: params.SubmissionID,
},
Raw: r,
}
type (
Request = struct{}
Params = ActionSubmissionRevokeParams
2024-12-11 06:23:08 +00:00
Response = *ActionSubmissionRevokeNoContent
)
response, err = middleware.HookMiddleware[
Request,
Params,
Response,
](
m,
mreq,
unpackActionSubmissionRevokeParams,
func(ctx context.Context, request Request, params Params) (response Response, err error) {
err = s.h.ActionSubmissionRevoke(ctx, params)
return response, err
},
)
} else {
err = s.h.ActionSubmissionRevoke(ctx, params)
}
if err != nil {
if errRes, ok := errors.Into[*ErrorStatusCode](err); ok {
if err := encodeErrorResponse(errRes, w, span); err != nil {
defer recordError("Internal", err)
}
return
}
if errors.Is(err, ht.ErrNotImplemented) {
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
if err := encodeErrorResponse(s.h.NewError(ctx, err), w, span); err != nil {
defer recordError("Internal", err)
}
return
}
if err := encodeActionSubmissionRevokeResponse(response, w, span); err != nil {
defer recordError("EncodeResponse", err)
if !errors.Is(err, ht.ErrInternalServerErrorResponse) {
s.cfg.ErrorHandler(ctx, w, r, err)
}
return
}
}
// handleActionSubmissionSubmitRequest handles actionSubmissionSubmit operation.
//
// Role Submitter changes status from UnderConstruction|ChangesRequested -> Submitted.
//
2024-12-10 06:06:33 +00:00
// POST /submissions/{SubmissionID}/status/submit
func (s *Server) handleActionSubmissionSubmitRequest(args [1]string, argsEscaped bool, w http.ResponseWriter, r *http.Request) {
2024-12-06 00:36:15 +00:00
statusWriter := &codeRecorder{ResponseWriter: w}
w = statusWriter
otelAttrs := []attribute.KeyValue{
otelogen.OperationID("actionSubmissionSubmit"),
2024-12-10 06:06:33 +00:00
semconv.HTTPRequestMethodKey.String("POST"),
semconv.HTTPRouteKey.String("/submissions/{SubmissionID}/status/submit"),
}
// Start a span for this request.
2024-11-29 22:33:48 +00:00
ctx, span := s.cfg.Tracer.Start(r.Context(), ActionSubmissionSubmitOperation,
trace.WithAttributes(otelAttrs...),
serverSpanKind,
)
defer span.End()
// Add Labeler to context.
labeler := &Labeler{attrs: otelAttrs}
ctx = contextWithLabeler(ctx, labeler)
// Run stopwatch.
startTime := time.Now()
defer func() {
elapsedDuration := time.Since(startTime)
2024-12-06 00:36:15 +00:00
attrSet := labeler.AttributeSet()
attrs := attrSet.ToSlice()
code := statusWriter.status
if code != 0 {
codeAttr := semconv.HTTPResponseStatusCode(code)
attrs = append(attrs, codeAttr)
span.SetAttributes(codeAttr)
}
attrOpt := metric.WithAttributes(attrs...)
// Increment request counter.
s.requests.Add(ctx, 1, attrOpt)
// Use floating point division here for higher precision (instead of Millisecond method).
2024-12-06 00:36:15 +00:00
s.duration.Record(ctx, float64(elapsedDuration)/float64(time.Millisecond), attrOpt)
}()
var (
recordError = func(stage string, err error) {
span.RecordError(err)
2024-12-06 00:36:15 +00:00
// https://opentelemetry.io/docs/specs/semconv/http/http-spans/#status
// Span Status MUST be left unset if HTTP status code was in the 1xx, 2xx or 3xx ranges,
// unless there was another error (e.g., network error receiving the response body; or 3xx codes with
// max redirects exceeded), in which case status MUST be set to Error.
code := statusWriter.status
if code >= 100 && code < 500 {
span.SetStatus(codes.Error, stage)
}
attrSet := labeler.AttributeSet()
attrs := attrSet.ToSlice()
if code != 0 {
attrs = append(attrs, semconv.HTTPResponseStatusCode(code))
}
s.errors.Add(ctx, 1, metric.WithAttributes(attrs...))
}
err error
opErrContext = ogenerrors.OperationContext{
2024-11-29 22:33:48 +00:00
Name: ActionSubmissionSubmitOperation,
ID: "actionSubmissionSubmit",
}
)
2024-11-29 22:33:48 +00:00
{
type bitset = [1]uint8
var satisfied bitset
{
sctx, ok, err := s.securityCookieAuth(ctx, ActionSubmissionSubmitOperation, r)
if err != nil {
err = &ogenerrors.SecurityError{
OperationContext: opErrContext,
Security: "CookieAuth",
Err: err,
}
if encodeErr := encodeErrorResponse(s.h.NewError(ctx, err), w, span); encodeErr != nil {
defer recordError("Security:CookieAuth", err)
}
return
}
if ok {
satisfied[0] |= 1 << 0
ctx = sctx
}
}
if ok := func() bool {
nextRequirement:
for _, requirement := range []bitset{
{0b00000001},
} {
for i, mask := range requirement {
if satisfied[i]&mask != mask {
continue nextRequirement
}
}
return true
}
return false
}(); !ok {
err = &ogenerrors.SecurityError{
OperationContext: opErrContext,
Err: ogenerrors.ErrSecurityRequirementIsNotSatisfied,
}
if encodeErr := encodeErrorResponse(s.h.NewError(ctx, err), w, span); encodeErr != nil {
defer recordError("Security", err)
}
return
}
}
params, err := decodeActionSubmissionSubmitParams(args, argsEscaped, r)
if err != nil {
err = &ogenerrors.DecodeParamsError{
OperationContext: opErrContext,
Err: err,
}
defer recordError("DecodeParams", err)
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
2024-12-11 06:23:08 +00:00
var response *ActionSubmissionSubmitNoContent
if m := s.cfg.Middleware; m != nil {
mreq := middleware.Request{
Context: ctx,
2024-11-29 22:33:48 +00:00
OperationName: ActionSubmissionSubmitOperation,
OperationSummary: "Role Submitter changes status from UnderConstruction|ChangesRequested -> Submitted",
OperationID: "actionSubmissionSubmit",
Body: nil,
Params: middleware.Parameters{
{
Name: "SubmissionID",
In: "path",
}: params.SubmissionID,
},
Raw: r,
}
type (
Request = struct{}
Params = ActionSubmissionSubmitParams
2024-12-11 06:23:08 +00:00
Response = *ActionSubmissionSubmitNoContent
)
response, err = middleware.HookMiddleware[
Request,
Params,
Response,
](
m,
mreq,
unpackActionSubmissionSubmitParams,
func(ctx context.Context, request Request, params Params) (response Response, err error) {
err = s.h.ActionSubmissionSubmit(ctx, params)
return response, err
},
)
} else {
err = s.h.ActionSubmissionSubmit(ctx, params)
}
if err != nil {
if errRes, ok := errors.Into[*ErrorStatusCode](err); ok {
if err := encodeErrorResponse(errRes, w, span); err != nil {
defer recordError("Internal", err)
}
return
}
if errors.Is(err, ht.ErrNotImplemented) {
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
if err := encodeErrorResponse(s.h.NewError(ctx, err), w, span); err != nil {
defer recordError("Internal", err)
}
return
}
if err := encodeActionSubmissionSubmitResponse(response, w, span); err != nil {
defer recordError("EncodeResponse", err)
if !errors.Is(err, ht.ErrInternalServerErrorResponse) {
s.cfg.ErrorHandler(ctx, w, r, err)
}
return
}
}
// handleActionSubmissionTriggerPublishRequest handles actionSubmissionTriggerPublish operation.
//
// Role Admin changes status from Validated -> Publishing.
//
2024-12-10 06:06:33 +00:00
// POST /submissions/{SubmissionID}/status/trigger-publish
func (s *Server) handleActionSubmissionTriggerPublishRequest(args [1]string, argsEscaped bool, w http.ResponseWriter, r *http.Request) {
2024-12-06 00:36:15 +00:00
statusWriter := &codeRecorder{ResponseWriter: w}
w = statusWriter
otelAttrs := []attribute.KeyValue{
otelogen.OperationID("actionSubmissionTriggerPublish"),
2024-12-10 06:06:33 +00:00
semconv.HTTPRequestMethodKey.String("POST"),
semconv.HTTPRouteKey.String("/submissions/{SubmissionID}/status/trigger-publish"),
}
// Start a span for this request.
2024-11-29 22:33:48 +00:00
ctx, span := s.cfg.Tracer.Start(r.Context(), ActionSubmissionTriggerPublishOperation,
trace.WithAttributes(otelAttrs...),
serverSpanKind,
)
defer span.End()
// Add Labeler to context.
labeler := &Labeler{attrs: otelAttrs}
ctx = contextWithLabeler(ctx, labeler)
// Run stopwatch.
startTime := time.Now()
defer func() {
elapsedDuration := time.Since(startTime)
2024-12-06 00:36:15 +00:00
attrSet := labeler.AttributeSet()
attrs := attrSet.ToSlice()
code := statusWriter.status
if code != 0 {
codeAttr := semconv.HTTPResponseStatusCode(code)
attrs = append(attrs, codeAttr)
span.SetAttributes(codeAttr)
}
attrOpt := metric.WithAttributes(attrs...)
// Increment request counter.
s.requests.Add(ctx, 1, attrOpt)
// Use floating point division here for higher precision (instead of Millisecond method).
2024-12-06 00:36:15 +00:00
s.duration.Record(ctx, float64(elapsedDuration)/float64(time.Millisecond), attrOpt)
}()
var (
recordError = func(stage string, err error) {
span.RecordError(err)
2024-12-06 00:36:15 +00:00
// https://opentelemetry.io/docs/specs/semconv/http/http-spans/#status
// Span Status MUST be left unset if HTTP status code was in the 1xx, 2xx or 3xx ranges,
// unless there was another error (e.g., network error receiving the response body; or 3xx codes with
// max redirects exceeded), in which case status MUST be set to Error.
code := statusWriter.status
if code >= 100 && code < 500 {
span.SetStatus(codes.Error, stage)
}
attrSet := labeler.AttributeSet()
attrs := attrSet.ToSlice()
if code != 0 {
attrs = append(attrs, semconv.HTTPResponseStatusCode(code))
}
s.errors.Add(ctx, 1, metric.WithAttributes(attrs...))
}
err error
opErrContext = ogenerrors.OperationContext{
2024-11-29 22:33:48 +00:00
Name: ActionSubmissionTriggerPublishOperation,
ID: "actionSubmissionTriggerPublish",
}
)
2024-11-29 22:33:48 +00:00
{
type bitset = [1]uint8
var satisfied bitset
{
sctx, ok, err := s.securityCookieAuth(ctx, ActionSubmissionTriggerPublishOperation, r)
if err != nil {
err = &ogenerrors.SecurityError{
OperationContext: opErrContext,
Security: "CookieAuth",
Err: err,
}
if encodeErr := encodeErrorResponse(s.h.NewError(ctx, err), w, span); encodeErr != nil {
defer recordError("Security:CookieAuth", err)
}
return
}
if ok {
satisfied[0] |= 1 << 0
ctx = sctx
}
}
if ok := func() bool {
nextRequirement:
for _, requirement := range []bitset{
{0b00000001},
} {
for i, mask := range requirement {
if satisfied[i]&mask != mask {
continue nextRequirement
}
}
return true
}
return false
}(); !ok {
err = &ogenerrors.SecurityError{
OperationContext: opErrContext,
Err: ogenerrors.ErrSecurityRequirementIsNotSatisfied,
}
if encodeErr := encodeErrorResponse(s.h.NewError(ctx, err), w, span); encodeErr != nil {
defer recordError("Security", err)
}
return
}
}
params, err := decodeActionSubmissionTriggerPublishParams(args, argsEscaped, r)
if err != nil {
err = &ogenerrors.DecodeParamsError{
OperationContext: opErrContext,
Err: err,
}
defer recordError("DecodeParams", err)
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
2024-12-11 06:23:08 +00:00
var response *ActionSubmissionTriggerPublishNoContent
if m := s.cfg.Middleware; m != nil {
mreq := middleware.Request{
Context: ctx,
2024-11-29 22:33:48 +00:00
OperationName: ActionSubmissionTriggerPublishOperation,
OperationSummary: "Role Admin changes status from Validated -> Publishing",
OperationID: "actionSubmissionTriggerPublish",
Body: nil,
Params: middleware.Parameters{
{
Name: "SubmissionID",
In: "path",
}: params.SubmissionID,
},
Raw: r,
}
type (
Request = struct{}
Params = ActionSubmissionTriggerPublishParams
2024-12-11 06:23:08 +00:00
Response = *ActionSubmissionTriggerPublishNoContent
)
response, err = middleware.HookMiddleware[
Request,
Params,
Response,
](
m,
mreq,
unpackActionSubmissionTriggerPublishParams,
func(ctx context.Context, request Request, params Params) (response Response, err error) {
err = s.h.ActionSubmissionTriggerPublish(ctx, params)
return response, err
},
)
} else {
err = s.h.ActionSubmissionTriggerPublish(ctx, params)
}
if err != nil {
if errRes, ok := errors.Into[*ErrorStatusCode](err); ok {
if err := encodeErrorResponse(errRes, w, span); err != nil {
defer recordError("Internal", err)
}
return
}
if errors.Is(err, ht.ErrNotImplemented) {
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
if err := encodeErrorResponse(s.h.NewError(ctx, err), w, span); err != nil {
defer recordError("Internal", err)
}
return
}
if err := encodeActionSubmissionTriggerPublishResponse(response, w, span); err != nil {
defer recordError("EncodeResponse", err)
if !errors.Is(err, ht.ErrInternalServerErrorResponse) {
s.cfg.ErrorHandler(ctx, w, r, err)
}
return
}
}
// handleActionSubmissionTriggerValidateRequest handles actionSubmissionTriggerValidate operation.
//
// Role Reviewer triggers validation and changes status from Submitted|Accepted -> Validating.
//
2024-12-10 06:06:33 +00:00
// POST /submissions/{SubmissionID}/status/trigger-validate
func (s *Server) handleActionSubmissionTriggerValidateRequest(args [1]string, argsEscaped bool, w http.ResponseWriter, r *http.Request) {
2024-12-06 00:36:15 +00:00
statusWriter := &codeRecorder{ResponseWriter: w}
w = statusWriter
otelAttrs := []attribute.KeyValue{
otelogen.OperationID("actionSubmissionTriggerValidate"),
2024-12-10 06:06:33 +00:00
semconv.HTTPRequestMethodKey.String("POST"),
semconv.HTTPRouteKey.String("/submissions/{SubmissionID}/status/trigger-validate"),
}
// Start a span for this request.
2024-11-29 22:33:48 +00:00
ctx, span := s.cfg.Tracer.Start(r.Context(), ActionSubmissionTriggerValidateOperation,
trace.WithAttributes(otelAttrs...),
serverSpanKind,
)
defer span.End()
// Add Labeler to context.
labeler := &Labeler{attrs: otelAttrs}
ctx = contextWithLabeler(ctx, labeler)
// Run stopwatch.
startTime := time.Now()
defer func() {
elapsedDuration := time.Since(startTime)
2024-12-06 00:36:15 +00:00
attrSet := labeler.AttributeSet()
attrs := attrSet.ToSlice()
code := statusWriter.status
if code != 0 {
codeAttr := semconv.HTTPResponseStatusCode(code)
attrs = append(attrs, codeAttr)
span.SetAttributes(codeAttr)
}
attrOpt := metric.WithAttributes(attrs...)
// Increment request counter.
s.requests.Add(ctx, 1, attrOpt)
// Use floating point division here for higher precision (instead of Millisecond method).
2024-12-06 00:36:15 +00:00
s.duration.Record(ctx, float64(elapsedDuration)/float64(time.Millisecond), attrOpt)
}()
var (
recordError = func(stage string, err error) {
span.RecordError(err)
2024-12-06 00:36:15 +00:00
// https://opentelemetry.io/docs/specs/semconv/http/http-spans/#status
// Span Status MUST be left unset if HTTP status code was in the 1xx, 2xx or 3xx ranges,
// unless there was another error (e.g., network error receiving the response body; or 3xx codes with
// max redirects exceeded), in which case status MUST be set to Error.
code := statusWriter.status
if code >= 100 && code < 500 {
span.SetStatus(codes.Error, stage)
}
attrSet := labeler.AttributeSet()
attrs := attrSet.ToSlice()
if code != 0 {
attrs = append(attrs, semconv.HTTPResponseStatusCode(code))
}
s.errors.Add(ctx, 1, metric.WithAttributes(attrs...))
}
err error
opErrContext = ogenerrors.OperationContext{
2024-11-29 22:33:48 +00:00
Name: ActionSubmissionTriggerValidateOperation,
ID: "actionSubmissionTriggerValidate",
}
)
2024-11-29 22:33:48 +00:00
{
type bitset = [1]uint8
var satisfied bitset
{
sctx, ok, err := s.securityCookieAuth(ctx, ActionSubmissionTriggerValidateOperation, r)
if err != nil {
err = &ogenerrors.SecurityError{
OperationContext: opErrContext,
Security: "CookieAuth",
Err: err,
}
if encodeErr := encodeErrorResponse(s.h.NewError(ctx, err), w, span); encodeErr != nil {
defer recordError("Security:CookieAuth", err)
}
return
}
if ok {
satisfied[0] |= 1 << 0
ctx = sctx
}
}
if ok := func() bool {
nextRequirement:
for _, requirement := range []bitset{
{0b00000001},
} {
for i, mask := range requirement {
if satisfied[i]&mask != mask {
continue nextRequirement
}
}
return true
}
return false
}(); !ok {
err = &ogenerrors.SecurityError{
OperationContext: opErrContext,
Err: ogenerrors.ErrSecurityRequirementIsNotSatisfied,
}
if encodeErr := encodeErrorResponse(s.h.NewError(ctx, err), w, span); encodeErr != nil {
defer recordError("Security", err)
}
return
}
}
params, err := decodeActionSubmissionTriggerValidateParams(args, argsEscaped, r)
if err != nil {
err = &ogenerrors.DecodeParamsError{
OperationContext: opErrContext,
Err: err,
}
defer recordError("DecodeParams", err)
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
2024-12-11 06:23:08 +00:00
var response *ActionSubmissionTriggerValidateNoContent
if m := s.cfg.Middleware; m != nil {
mreq := middleware.Request{
Context: ctx,
2024-11-29 22:33:48 +00:00
OperationName: ActionSubmissionTriggerValidateOperation,
OperationSummary: "Role Reviewer triggers validation and changes status from Submitted|Accepted -> Validating",
OperationID: "actionSubmissionTriggerValidate",
Body: nil,
Params: middleware.Parameters{
{
Name: "SubmissionID",
In: "path",
}: params.SubmissionID,
},
Raw: r,
}
type (
Request = struct{}
Params = ActionSubmissionTriggerValidateParams
2024-12-11 06:23:08 +00:00
Response = *ActionSubmissionTriggerValidateNoContent
)
response, err = middleware.HookMiddleware[
Request,
Params,
Response,
](
m,
mreq,
unpackActionSubmissionTriggerValidateParams,
func(ctx context.Context, request Request, params Params) (response Response, err error) {
err = s.h.ActionSubmissionTriggerValidate(ctx, params)
return response, err
},
)
} else {
err = s.h.ActionSubmissionTriggerValidate(ctx, params)
}
if err != nil {
if errRes, ok := errors.Into[*ErrorStatusCode](err); ok {
if err := encodeErrorResponse(errRes, w, span); err != nil {
defer recordError("Internal", err)
}
return
}
if errors.Is(err, ht.ErrNotImplemented) {
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
if err := encodeErrorResponse(s.h.NewError(ctx, err), w, span); err != nil {
defer recordError("Internal", err)
}
return
}
if err := encodeActionSubmissionTriggerValidateResponse(response, w, span); err != nil {
defer recordError("EncodeResponse", err)
if !errors.Is(err, ht.ErrInternalServerErrorResponse) {
s.cfg.ErrorHandler(ctx, w, r, err)
}
return
}
}
// handleActionSubmissionValidateRequest handles actionSubmissionValidate operation.
//
2024-12-12 01:54:11 +00:00
// (Internal endpoint) Role Validator changes status from Validating -> Validated.
//
2024-12-12 01:54:11 +00:00
// POST /submissions/{SubmissionID}/status/validator-validated
func (s *Server) handleActionSubmissionValidateRequest(args [1]string, argsEscaped bool, w http.ResponseWriter, r *http.Request) {
2024-12-06 00:36:15 +00:00
statusWriter := &codeRecorder{ResponseWriter: w}
w = statusWriter
otelAttrs := []attribute.KeyValue{
otelogen.OperationID("actionSubmissionValidate"),
2024-12-10 06:06:33 +00:00
semconv.HTTPRequestMethodKey.String("POST"),
2024-12-12 01:54:11 +00:00
semconv.HTTPRouteKey.String("/submissions/{SubmissionID}/status/validator-validated"),
}
// Start a span for this request.
2024-11-29 22:33:48 +00:00
ctx, span := s.cfg.Tracer.Start(r.Context(), ActionSubmissionValidateOperation,
trace.WithAttributes(otelAttrs...),
serverSpanKind,
)
defer span.End()
// Add Labeler to context.
labeler := &Labeler{attrs: otelAttrs}
ctx = contextWithLabeler(ctx, labeler)
// Run stopwatch.
startTime := time.Now()
defer func() {
elapsedDuration := time.Since(startTime)
2024-12-06 00:36:15 +00:00
attrSet := labeler.AttributeSet()
attrs := attrSet.ToSlice()
code := statusWriter.status
if code != 0 {
codeAttr := semconv.HTTPResponseStatusCode(code)
attrs = append(attrs, codeAttr)
span.SetAttributes(codeAttr)
}
attrOpt := metric.WithAttributes(attrs...)
// Increment request counter.
s.requests.Add(ctx, 1, attrOpt)
// Use floating point division here for higher precision (instead of Millisecond method).
2024-12-06 00:36:15 +00:00
s.duration.Record(ctx, float64(elapsedDuration)/float64(time.Millisecond), attrOpt)
}()
var (
recordError = func(stage string, err error) {
span.RecordError(err)
2024-12-06 00:36:15 +00:00
// https://opentelemetry.io/docs/specs/semconv/http/http-spans/#status
// Span Status MUST be left unset if HTTP status code was in the 1xx, 2xx or 3xx ranges,
// unless there was another error (e.g., network error receiving the response body; or 3xx codes with
// max redirects exceeded), in which case status MUST be set to Error.
code := statusWriter.status
if code >= 100 && code < 500 {
span.SetStatus(codes.Error, stage)
}
attrSet := labeler.AttributeSet()
attrs := attrSet.ToSlice()
if code != 0 {
attrs = append(attrs, semconv.HTTPResponseStatusCode(code))
}
s.errors.Add(ctx, 1, metric.WithAttributes(attrs...))
}
err error
opErrContext = ogenerrors.OperationContext{
2024-11-29 22:33:48 +00:00
Name: ActionSubmissionValidateOperation,
ID: "actionSubmissionValidate",
}
)
params, err := decodeActionSubmissionValidateParams(args, argsEscaped, r)
if err != nil {
err = &ogenerrors.DecodeParamsError{
OperationContext: opErrContext,
Err: err,
}
defer recordError("DecodeParams", err)
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
2024-12-11 06:23:08 +00:00
var response *ActionSubmissionValidateNoContent
if m := s.cfg.Middleware; m != nil {
mreq := middleware.Request{
Context: ctx,
2024-11-29 22:33:48 +00:00
OperationName: ActionSubmissionValidateOperation,
2024-12-12 01:54:11 +00:00
OperationSummary: "(Internal endpoint) Role Validator changes status from Validating -> Validated",
OperationID: "actionSubmissionValidate",
Body: nil,
Params: middleware.Parameters{
{
Name: "SubmissionID",
In: "path",
}: params.SubmissionID,
},
Raw: r,
}
type (
Request = struct{}
Params = ActionSubmissionValidateParams
2024-12-11 06:23:08 +00:00
Response = *ActionSubmissionValidateNoContent
)
response, err = middleware.HookMiddleware[
Request,
Params,
Response,
](
m,
mreq,
unpackActionSubmissionValidateParams,
func(ctx context.Context, request Request, params Params) (response Response, err error) {
err = s.h.ActionSubmissionValidate(ctx, params)
return response, err
},
)
} else {
err = s.h.ActionSubmissionValidate(ctx, params)
}
if err != nil {
if errRes, ok := errors.Into[*ErrorStatusCode](err); ok {
if err := encodeErrorResponse(errRes, w, span); err != nil {
defer recordError("Internal", err)
}
return
}
if errors.Is(err, ht.ErrNotImplemented) {
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
if err := encodeErrorResponse(s.h.NewError(ctx, err), w, span); err != nil {
defer recordError("Internal", err)
}
return
}
if err := encodeActionSubmissionValidateResponse(response, w, span); err != nil {
defer recordError("EncodeResponse", err)
if !errors.Is(err, ht.ErrInternalServerErrorResponse) {
s.cfg.ErrorHandler(ctx, w, r, err)
}
return
}
}
2024-12-06 00:36:15 +00:00
// handleCreateScriptRequest handles createScript operation.
2024-11-26 01:22:45 +00:00
//
2024-12-06 00:36:15 +00:00
// Create a new script.
2024-11-26 01:22:45 +00:00
//
2024-12-06 00:36:15 +00:00
// POST /scripts
func (s *Server) handleCreateScriptRequest(args [0]string, argsEscaped bool, w http.ResponseWriter, r *http.Request) {
statusWriter := &codeRecorder{ResponseWriter: w}
w = statusWriter
2024-11-26 01:22:45 +00:00
otelAttrs := []attribute.KeyValue{
2024-12-06 00:36:15 +00:00
otelogen.OperationID("createScript"),
2024-11-26 20:39:10 +00:00
semconv.HTTPRequestMethodKey.String("POST"),
2024-12-06 00:36:15 +00:00
semconv.HTTPRouteKey.String("/scripts"),
2024-11-26 01:22:45 +00:00
}
// Start a span for this request.
2024-12-06 00:36:15 +00:00
ctx, span := s.cfg.Tracer.Start(r.Context(), CreateScriptOperation,
2024-11-26 20:39:10 +00:00
trace.WithAttributes(otelAttrs...),
serverSpanKind,
)
defer span.End()
// Add Labeler to context.
labeler := &Labeler{attrs: otelAttrs}
ctx = contextWithLabeler(ctx, labeler)
// Run stopwatch.
startTime := time.Now()
defer func() {
elapsedDuration := time.Since(startTime)
2024-12-06 00:36:15 +00:00
attrSet := labeler.AttributeSet()
attrs := attrSet.ToSlice()
code := statusWriter.status
if code != 0 {
codeAttr := semconv.HTTPResponseStatusCode(code)
attrs = append(attrs, codeAttr)
span.SetAttributes(codeAttr)
}
attrOpt := metric.WithAttributes(attrs...)
2024-11-26 20:39:10 +00:00
// Increment request counter.
s.requests.Add(ctx, 1, attrOpt)
// Use floating point division here for higher precision (instead of Millisecond method).
2024-12-06 00:36:15 +00:00
s.duration.Record(ctx, float64(elapsedDuration)/float64(time.Millisecond), attrOpt)
2024-11-26 20:39:10 +00:00
}()
var (
recordError = func(stage string, err error) {
span.RecordError(err)
2024-12-06 00:36:15 +00:00
// https://opentelemetry.io/docs/specs/semconv/http/http-spans/#status
// Span Status MUST be left unset if HTTP status code was in the 1xx, 2xx or 3xx ranges,
// unless there was another error (e.g., network error receiving the response body; or 3xx codes with
// max redirects exceeded), in which case status MUST be set to Error.
code := statusWriter.status
if code >= 100 && code < 500 {
span.SetStatus(codes.Error, stage)
}
attrSet := labeler.AttributeSet()
attrs := attrSet.ToSlice()
if code != 0 {
attrs = append(attrs, semconv.HTTPResponseStatusCode(code))
}
s.errors.Add(ctx, 1, metric.WithAttributes(attrs...))
2024-11-26 20:39:10 +00:00
}
2024-11-27 23:38:17 +00:00
err error
opErrContext = ogenerrors.OperationContext{
2024-12-06 00:36:15 +00:00
Name: CreateScriptOperation,
ID: "createScript",
2024-11-27 23:38:17 +00:00
}
2024-11-26 20:39:10 +00:00
)
2024-11-29 22:33:48 +00:00
{
type bitset = [1]uint8
var satisfied bitset
{
2024-12-06 00:36:15 +00:00
sctx, ok, err := s.securityCookieAuth(ctx, CreateScriptOperation, r)
2024-11-29 22:33:48 +00:00
if err != nil {
err = &ogenerrors.SecurityError{
OperationContext: opErrContext,
Security: "CookieAuth",
Err: err,
}
if encodeErr := encodeErrorResponse(s.h.NewError(ctx, err), w, span); encodeErr != nil {
defer recordError("Security:CookieAuth", err)
}
return
}
if ok {
satisfied[0] |= 1 << 0
ctx = sctx
}
}
if ok := func() bool {
nextRequirement:
for _, requirement := range []bitset{
{0b00000001},
} {
for i, mask := range requirement {
if satisfied[i]&mask != mask {
continue nextRequirement
}
}
return true
}
return false
}(); !ok {
err = &ogenerrors.SecurityError{
OperationContext: opErrContext,
Err: ogenerrors.ErrSecurityRequirementIsNotSatisfied,
}
if encodeErr := encodeErrorResponse(s.h.NewError(ctx, err), w, span); encodeErr != nil {
defer recordError("Security", err)
}
return
}
}
2024-12-06 00:36:15 +00:00
request, close, err := s.decodeCreateScriptRequest(r)
2024-11-27 23:38:17 +00:00
if err != nil {
err = &ogenerrors.DecodeRequestError{
OperationContext: opErrContext,
Err: err,
}
defer recordError("DecodeRequest", err)
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
defer func() {
if err := close(); err != nil {
recordError("CloseRequest", err)
}
}()
2024-11-26 20:39:10 +00:00
2024-11-28 00:13:43 +00:00
var response *ID
2024-11-26 20:39:10 +00:00
if m := s.cfg.Middleware; m != nil {
mreq := middleware.Request{
Context: ctx,
2024-12-06 00:36:15 +00:00
OperationName: CreateScriptOperation,
OperationSummary: "Create a new script",
OperationID: "createScript",
2024-11-27 23:38:17 +00:00
Body: request,
2024-11-26 20:39:10 +00:00
Params: middleware.Parameters{},
Raw: r,
}
type (
2024-12-06 02:48:26 +00:00
Request = *ScriptCreate
2024-11-26 20:39:10 +00:00
Params = struct{}
2024-11-28 00:13:43 +00:00
Response = *ID
2024-11-26 20:39:10 +00:00
)
response, err = middleware.HookMiddleware[
Request,
Params,
Response,
](
m,
mreq,
nil,
func(ctx context.Context, request Request, params Params) (response Response, err error) {
2024-12-06 00:36:15 +00:00
response, err = s.h.CreateScript(ctx, request)
2024-11-26 20:39:10 +00:00
return response, err
},
)
} else {
2024-12-06 00:36:15 +00:00
response, err = s.h.CreateScript(ctx, request)
2024-11-26 20:39:10 +00:00
}
if err != nil {
if errRes, ok := errors.Into[*ErrorStatusCode](err); ok {
if err := encodeErrorResponse(errRes, w, span); err != nil {
defer recordError("Internal", err)
}
return
}
if errors.Is(err, ht.ErrNotImplemented) {
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
if err := encodeErrorResponse(s.h.NewError(ctx, err), w, span); err != nil {
defer recordError("Internal", err)
}
return
}
2024-12-06 00:36:15 +00:00
if err := encodeCreateScriptResponse(response, w, span); err != nil {
2024-11-26 20:39:10 +00:00
defer recordError("EncodeResponse", err)
if !errors.Is(err, ht.ErrInternalServerErrorResponse) {
s.cfg.ErrorHandler(ctx, w, r, err)
}
return
}
}
2024-12-06 00:36:15 +00:00
// handleCreateScriptPolicyRequest handles createScriptPolicy operation.
2024-11-26 20:39:10 +00:00
//
2024-12-06 00:36:15 +00:00
// Create a new script policy.
2024-11-26 20:39:10 +00:00
//
2024-12-06 00:36:15 +00:00
// POST /script-policy
func (s *Server) handleCreateScriptPolicyRequest(args [0]string, argsEscaped bool, w http.ResponseWriter, r *http.Request) {
statusWriter := &codeRecorder{ResponseWriter: w}
w = statusWriter
2024-11-26 20:39:10 +00:00
otelAttrs := []attribute.KeyValue{
2024-12-06 00:36:15 +00:00
otelogen.OperationID("createScriptPolicy"),
semconv.HTTPRequestMethodKey.String("POST"),
semconv.HTTPRouteKey.String("/script-policy"),
2024-11-26 20:39:10 +00:00
}
// Start a span for this request.
2024-12-06 00:36:15 +00:00
ctx, span := s.cfg.Tracer.Start(r.Context(), CreateScriptPolicyOperation,
2024-11-26 01:22:45 +00:00
trace.WithAttributes(otelAttrs...),
serverSpanKind,
)
defer span.End()
// Add Labeler to context.
labeler := &Labeler{attrs: otelAttrs}
ctx = contextWithLabeler(ctx, labeler)
// Run stopwatch.
startTime := time.Now()
defer func() {
elapsedDuration := time.Since(startTime)
2024-12-06 00:36:15 +00:00
attrSet := labeler.AttributeSet()
attrs := attrSet.ToSlice()
code := statusWriter.status
if code != 0 {
codeAttr := semconv.HTTPResponseStatusCode(code)
attrs = append(attrs, codeAttr)
span.SetAttributes(codeAttr)
}
attrOpt := metric.WithAttributes(attrs...)
2024-11-26 01:22:45 +00:00
// Increment request counter.
s.requests.Add(ctx, 1, attrOpt)
// Use floating point division here for higher precision (instead of Millisecond method).
2024-12-06 00:36:15 +00:00
s.duration.Record(ctx, float64(elapsedDuration)/float64(time.Millisecond), attrOpt)
2024-11-26 01:22:45 +00:00
}()
var (
recordError = func(stage string, err error) {
span.RecordError(err)
2024-12-06 00:36:15 +00:00
// https://opentelemetry.io/docs/specs/semconv/http/http-spans/#status
// Span Status MUST be left unset if HTTP status code was in the 1xx, 2xx or 3xx ranges,
// unless there was another error (e.g., network error receiving the response body; or 3xx codes with
// max redirects exceeded), in which case status MUST be set to Error.
code := statusWriter.status
if code >= 100 && code < 500 {
span.SetStatus(codes.Error, stage)
}
attrSet := labeler.AttributeSet()
attrs := attrSet.ToSlice()
if code != 0 {
attrs = append(attrs, semconv.HTTPResponseStatusCode(code))
}
s.errors.Add(ctx, 1, metric.WithAttributes(attrs...))
2024-11-26 01:22:45 +00:00
}
err error
opErrContext = ogenerrors.OperationContext{
2024-12-06 00:36:15 +00:00
Name: CreateScriptPolicyOperation,
ID: "createScriptPolicy",
2024-11-26 01:22:45 +00:00
}
)
2024-11-29 22:33:48 +00:00
{
type bitset = [1]uint8
var satisfied bitset
{
2024-12-06 00:36:15 +00:00
sctx, ok, err := s.securityCookieAuth(ctx, CreateScriptPolicyOperation, r)
2024-11-29 22:33:48 +00:00
if err != nil {
err = &ogenerrors.SecurityError{
OperationContext: opErrContext,
Security: "CookieAuth",
Err: err,
}
if encodeErr := encodeErrorResponse(s.h.NewError(ctx, err), w, span); encodeErr != nil {
defer recordError("Security:CookieAuth", err)
}
return
}
if ok {
satisfied[0] |= 1 << 0
ctx = sctx
}
}
if ok := func() bool {
nextRequirement:
for _, requirement := range []bitset{
{0b00000001},
} {
for i, mask := range requirement {
if satisfied[i]&mask != mask {
continue nextRequirement
}
}
return true
}
return false
}(); !ok {
err = &ogenerrors.SecurityError{
OperationContext: opErrContext,
Err: ogenerrors.ErrSecurityRequirementIsNotSatisfied,
}
if encodeErr := encodeErrorResponse(s.h.NewError(ctx, err), w, span); encodeErr != nil {
defer recordError("Security", err)
}
return
}
}
2024-12-06 00:36:15 +00:00
request, close, err := s.decodeCreateScriptPolicyRequest(r)
2024-11-26 01:22:45 +00:00
if err != nil {
2024-12-06 00:36:15 +00:00
err = &ogenerrors.DecodeRequestError{
2024-11-26 01:22:45 +00:00
OperationContext: opErrContext,
Err: err,
}
2024-12-06 00:36:15 +00:00
defer recordError("DecodeRequest", err)
2024-11-26 01:22:45 +00:00
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
2024-12-06 00:36:15 +00:00
defer func() {
if err := close(); err != nil {
recordError("CloseRequest", err)
}
}()
2024-11-26 01:22:45 +00:00
2024-12-06 00:36:15 +00:00
var response *ID
2024-11-26 01:22:45 +00:00
if m := s.cfg.Middleware; m != nil {
mreq := middleware.Request{
Context: ctx,
2024-12-06 00:36:15 +00:00
OperationName: CreateScriptPolicyOperation,
OperationSummary: "Create a new script policy",
OperationID: "createScriptPolicy",
Body: request,
Params: middleware.Parameters{},
Raw: r,
2024-11-26 01:22:45 +00:00
}
type (
2024-12-06 02:48:26 +00:00
Request = *ScriptPolicyCreate
2024-12-06 00:36:15 +00:00
Params = struct{}
Response = *ID
2024-11-26 01:22:45 +00:00
)
response, err = middleware.HookMiddleware[
Request,
Params,
Response,
](
m,
mreq,
2024-12-06 00:36:15 +00:00
nil,
2024-11-26 01:22:45 +00:00
func(ctx context.Context, request Request, params Params) (response Response, err error) {
2024-12-06 00:36:15 +00:00
response, err = s.h.CreateScriptPolicy(ctx, request)
2024-11-26 01:22:45 +00:00
return response, err
},
)
} else {
2024-12-06 00:36:15 +00:00
response, err = s.h.CreateScriptPolicy(ctx, request)
2024-11-26 01:22:45 +00:00
}
if err != nil {
if errRes, ok := errors.Into[*ErrorStatusCode](err); ok {
if err := encodeErrorResponse(errRes, w, span); err != nil {
defer recordError("Internal", err)
}
return
}
if errors.Is(err, ht.ErrNotImplemented) {
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
if err := encodeErrorResponse(s.h.NewError(ctx, err), w, span); err != nil {
defer recordError("Internal", err)
}
return
}
2024-12-06 00:36:15 +00:00
if err := encodeCreateScriptPolicyResponse(response, w, span); err != nil {
2024-11-26 01:22:45 +00:00
defer recordError("EncodeResponse", err)
if !errors.Is(err, ht.ErrInternalServerErrorResponse) {
s.cfg.ErrorHandler(ctx, w, r, err)
}
return
}
}
2024-12-06 00:36:15 +00:00
// handleCreateSubmissionRequest handles createSubmission operation.
2024-11-26 01:22:45 +00:00
//
2024-12-06 00:36:15 +00:00
// Create new submission.
2024-11-26 01:22:45 +00:00
//
2024-12-06 00:36:15 +00:00
// POST /submissions
func (s *Server) handleCreateSubmissionRequest(args [0]string, argsEscaped bool, w http.ResponseWriter, r *http.Request) {
statusWriter := &codeRecorder{ResponseWriter: w}
w = statusWriter
2024-11-26 01:22:45 +00:00
otelAttrs := []attribute.KeyValue{
2024-12-06 00:36:15 +00:00
otelogen.OperationID("createSubmission"),
semconv.HTTPRequestMethodKey.String("POST"),
2024-11-26 20:39:10 +00:00
semconv.HTTPRouteKey.String("/submissions"),
2024-11-26 01:22:45 +00:00
}
// Start a span for this request.
2024-12-06 00:36:15 +00:00
ctx, span := s.cfg.Tracer.Start(r.Context(), CreateSubmissionOperation,
2024-11-26 01:22:45 +00:00
trace.WithAttributes(otelAttrs...),
serverSpanKind,
)
defer span.End()
// Add Labeler to context.
labeler := &Labeler{attrs: otelAttrs}
ctx = contextWithLabeler(ctx, labeler)
// Run stopwatch.
startTime := time.Now()
defer func() {
elapsedDuration := time.Since(startTime)
2024-12-06 00:36:15 +00:00
attrSet := labeler.AttributeSet()
attrs := attrSet.ToSlice()
code := statusWriter.status
if code != 0 {
codeAttr := semconv.HTTPResponseStatusCode(code)
attrs = append(attrs, codeAttr)
span.SetAttributes(codeAttr)
}
attrOpt := metric.WithAttributes(attrs...)
2024-11-26 01:22:45 +00:00
// Increment request counter.
s.requests.Add(ctx, 1, attrOpt)
// Use floating point division here for higher precision (instead of Millisecond method).
2024-12-06 00:36:15 +00:00
s.duration.Record(ctx, float64(elapsedDuration)/float64(time.Millisecond), attrOpt)
2024-11-26 01:22:45 +00:00
}()
var (
recordError = func(stage string, err error) {
span.RecordError(err)
2024-12-06 00:36:15 +00:00
// https://opentelemetry.io/docs/specs/semconv/http/http-spans/#status
// Span Status MUST be left unset if HTTP status code was in the 1xx, 2xx or 3xx ranges,
// unless there was another error (e.g., network error receiving the response body; or 3xx codes with
// max redirects exceeded), in which case status MUST be set to Error.
code := statusWriter.status
if code >= 100 && code < 500 {
span.SetStatus(codes.Error, stage)
}
attrSet := labeler.AttributeSet()
attrs := attrSet.ToSlice()
if code != 0 {
attrs = append(attrs, semconv.HTTPResponseStatusCode(code))
}
s.errors.Add(ctx, 1, metric.WithAttributes(attrs...))
2024-11-26 01:22:45 +00:00
}
err error
opErrContext = ogenerrors.OperationContext{
2024-12-06 00:36:15 +00:00
Name: CreateSubmissionOperation,
ID: "createSubmission",
2024-11-26 01:22:45 +00:00
}
)
2024-11-29 22:33:48 +00:00
{
type bitset = [1]uint8
var satisfied bitset
{
2024-12-06 00:36:15 +00:00
sctx, ok, err := s.securityCookieAuth(ctx, CreateSubmissionOperation, r)
2024-11-29 22:33:48 +00:00
if err != nil {
err = &ogenerrors.SecurityError{
OperationContext: opErrContext,
Security: "CookieAuth",
Err: err,
}
if encodeErr := encodeErrorResponse(s.h.NewError(ctx, err), w, span); encodeErr != nil {
defer recordError("Security:CookieAuth", err)
}
return
}
if ok {
satisfied[0] |= 1 << 0
ctx = sctx
}
}
if ok := func() bool {
nextRequirement:
for _, requirement := range []bitset{
{0b00000001},
} {
for i, mask := range requirement {
if satisfied[i]&mask != mask {
continue nextRequirement
}
}
return true
}
return false
}(); !ok {
err = &ogenerrors.SecurityError{
OperationContext: opErrContext,
Err: ogenerrors.ErrSecurityRequirementIsNotSatisfied,
}
if encodeErr := encodeErrorResponse(s.h.NewError(ctx, err), w, span); encodeErr != nil {
defer recordError("Security", err)
}
return
}
}
2024-12-06 00:36:15 +00:00
request, close, err := s.decodeCreateSubmissionRequest(r)
2024-11-26 01:22:45 +00:00
if err != nil {
2024-12-06 00:36:15 +00:00
err = &ogenerrors.DecodeRequestError{
2024-11-26 01:22:45 +00:00
OperationContext: opErrContext,
Err: err,
}
2024-12-06 00:36:15 +00:00
defer recordError("DecodeRequest", err)
2024-11-26 01:22:45 +00:00
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
2024-12-06 00:36:15 +00:00
defer func() {
if err := close(); err != nil {
recordError("CloseRequest", err)
}
}()
2024-11-26 01:22:45 +00:00
2024-12-06 00:36:15 +00:00
var response *ID
2024-11-26 01:22:45 +00:00
if m := s.cfg.Middleware; m != nil {
mreq := middleware.Request{
Context: ctx,
2024-12-06 00:36:15 +00:00
OperationName: CreateSubmissionOperation,
OperationSummary: "Create new submission",
OperationID: "createSubmission",
Body: request,
Params: middleware.Parameters{},
Raw: r,
2024-11-26 20:39:10 +00:00
}
type (
2024-12-06 02:48:26 +00:00
Request = *SubmissionCreate
2024-12-06 00:36:15 +00:00
Params = struct{}
Response = *ID
2024-11-26 20:39:10 +00:00
)
response, err = middleware.HookMiddleware[
Request,
Params,
Response,
](
m,
mreq,
2024-12-06 00:36:15 +00:00
nil,
2024-11-26 20:39:10 +00:00
func(ctx context.Context, request Request, params Params) (response Response, err error) {
2024-12-06 00:36:15 +00:00
response, err = s.h.CreateSubmission(ctx, request)
2024-11-26 20:39:10 +00:00
return response, err
},
)
} else {
2024-12-06 00:36:15 +00:00
response, err = s.h.CreateSubmission(ctx, request)
2024-11-26 20:39:10 +00:00
}
if err != nil {
if errRes, ok := errors.Into[*ErrorStatusCode](err); ok {
if err := encodeErrorResponse(errRes, w, span); err != nil {
defer recordError("Internal", err)
}
return
}
if errors.Is(err, ht.ErrNotImplemented) {
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
if err := encodeErrorResponse(s.h.NewError(ctx, err), w, span); err != nil {
defer recordError("Internal", err)
}
return
}
2024-12-06 00:36:15 +00:00
if err := encodeCreateSubmissionResponse(response, w, span); err != nil {
2024-11-26 20:39:10 +00:00
defer recordError("EncodeResponse", err)
if !errors.Is(err, ht.ErrInternalServerErrorResponse) {
s.cfg.ErrorHandler(ctx, w, r, err)
}
return
}
}
2024-12-06 00:36:15 +00:00
// handleDeleteScriptRequest handles deleteScript operation.
2024-11-26 20:39:10 +00:00
//
2024-12-06 00:36:15 +00:00
// Delete the specified script by ID.
2024-11-26 20:39:10 +00:00
//
2024-12-06 00:36:15 +00:00
// DELETE /scripts/{ScriptID}
func (s *Server) handleDeleteScriptRequest(args [1]string, argsEscaped bool, w http.ResponseWriter, r *http.Request) {
statusWriter := &codeRecorder{ResponseWriter: w}
w = statusWriter
2024-11-26 20:39:10 +00:00
otelAttrs := []attribute.KeyValue{
2024-12-06 00:36:15 +00:00
otelogen.OperationID("deleteScript"),
semconv.HTTPRequestMethodKey.String("DELETE"),
semconv.HTTPRouteKey.String("/scripts/{ScriptID}"),
2024-11-26 20:39:10 +00:00
}
// Start a span for this request.
2024-12-06 00:36:15 +00:00
ctx, span := s.cfg.Tracer.Start(r.Context(), DeleteScriptOperation,
2024-11-26 20:39:10 +00:00
trace.WithAttributes(otelAttrs...),
serverSpanKind,
)
defer span.End()
// Add Labeler to context.
labeler := &Labeler{attrs: otelAttrs}
ctx = contextWithLabeler(ctx, labeler)
// Run stopwatch.
startTime := time.Now()
defer func() {
elapsedDuration := time.Since(startTime)
2024-12-06 00:36:15 +00:00
attrSet := labeler.AttributeSet()
attrs := attrSet.ToSlice()
code := statusWriter.status
if code != 0 {
codeAttr := semconv.HTTPResponseStatusCode(code)
attrs = append(attrs, codeAttr)
span.SetAttributes(codeAttr)
}
attrOpt := metric.WithAttributes(attrs...)
2024-11-26 20:39:10 +00:00
// Increment request counter.
s.requests.Add(ctx, 1, attrOpt)
// Use floating point division here for higher precision (instead of Millisecond method).
2024-12-06 00:36:15 +00:00
s.duration.Record(ctx, float64(elapsedDuration)/float64(time.Millisecond), attrOpt)
2024-11-26 20:39:10 +00:00
}()
var (
recordError = func(stage string, err error) {
span.RecordError(err)
2024-12-06 00:36:15 +00:00
// https://opentelemetry.io/docs/specs/semconv/http/http-spans/#status
// Span Status MUST be left unset if HTTP status code was in the 1xx, 2xx or 3xx ranges,
// unless there was another error (e.g., network error receiving the response body; or 3xx codes with
// max redirects exceeded), in which case status MUST be set to Error.
code := statusWriter.status
if code >= 100 && code < 500 {
span.SetStatus(codes.Error, stage)
}
attrSet := labeler.AttributeSet()
attrs := attrSet.ToSlice()
if code != 0 {
attrs = append(attrs, semconv.HTTPResponseStatusCode(code))
}
s.errors.Add(ctx, 1, metric.WithAttributes(attrs...))
2024-11-26 20:39:10 +00:00
}
err error
opErrContext = ogenerrors.OperationContext{
2024-12-06 00:36:15 +00:00
Name: DeleteScriptOperation,
ID: "deleteScript",
2024-11-26 20:39:10 +00:00
}
)
2024-11-29 22:33:48 +00:00
{
type bitset = [1]uint8
var satisfied bitset
{
2024-12-06 00:36:15 +00:00
sctx, ok, err := s.securityCookieAuth(ctx, DeleteScriptOperation, r)
2024-11-29 22:33:48 +00:00
if err != nil {
err = &ogenerrors.SecurityError{
OperationContext: opErrContext,
Security: "CookieAuth",
Err: err,
}
if encodeErr := encodeErrorResponse(s.h.NewError(ctx, err), w, span); encodeErr != nil {
defer recordError("Security:CookieAuth", err)
}
return
}
if ok {
satisfied[0] |= 1 << 0
ctx = sctx
}
}
if ok := func() bool {
nextRequirement:
for _, requirement := range []bitset{
{0b00000001},
} {
for i, mask := range requirement {
if satisfied[i]&mask != mask {
continue nextRequirement
}
}
return true
}
return false
}(); !ok {
err = &ogenerrors.SecurityError{
OperationContext: opErrContext,
Err: ogenerrors.ErrSecurityRequirementIsNotSatisfied,
}
if encodeErr := encodeErrorResponse(s.h.NewError(ctx, err), w, span); encodeErr != nil {
defer recordError("Security", err)
}
return
}
}
2024-12-06 00:36:15 +00:00
params, err := decodeDeleteScriptParams(args, argsEscaped, r)
2024-11-26 20:39:10 +00:00
if err != nil {
err = &ogenerrors.DecodeParamsError{
OperationContext: opErrContext,
Err: err,
}
defer recordError("DecodeParams", err)
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
2024-12-11 06:23:08 +00:00
var response *DeleteScriptNoContent
2024-11-26 20:39:10 +00:00
if m := s.cfg.Middleware; m != nil {
mreq := middleware.Request{
Context: ctx,
2024-12-06 00:36:15 +00:00
OperationName: DeleteScriptOperation,
OperationSummary: "Delete the specified script by ID",
OperationID: "deleteScript",
2024-11-26 20:39:10 +00:00
Body: nil,
Params: middleware.Parameters{
{
2024-12-06 00:36:15 +00:00
Name: "ScriptID",
2024-11-26 20:39:10 +00:00
In: "path",
2024-12-06 00:36:15 +00:00
}: params.ScriptID,
2024-11-26 01:22:45 +00:00
},
Raw: r,
}
type (
Request = struct{}
2024-12-06 00:36:15 +00:00
Params = DeleteScriptParams
2024-12-11 06:23:08 +00:00
Response = *DeleteScriptNoContent
2024-11-26 01:22:45 +00:00
)
response, err = middleware.HookMiddleware[
Request,
Params,
Response,
](
m,
mreq,
2024-12-06 00:36:15 +00:00
unpackDeleteScriptParams,
2024-11-26 01:22:45 +00:00
func(ctx context.Context, request Request, params Params) (response Response, err error) {
2024-12-06 00:36:15 +00:00
err = s.h.DeleteScript(ctx, params)
2024-11-26 01:22:45 +00:00
return response, err
},
)
} else {
2024-12-06 00:36:15 +00:00
err = s.h.DeleteScript(ctx, params)
2024-11-26 01:22:45 +00:00
}
if err != nil {
if errRes, ok := errors.Into[*ErrorStatusCode](err); ok {
if err := encodeErrorResponse(errRes, w, span); err != nil {
defer recordError("Internal", err)
}
return
}
if errors.Is(err, ht.ErrNotImplemented) {
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
if err := encodeErrorResponse(s.h.NewError(ctx, err), w, span); err != nil {
defer recordError("Internal", err)
}
return
}
2024-12-06 00:36:15 +00:00
if err := encodeDeleteScriptResponse(response, w, span); err != nil {
2024-11-26 01:22:45 +00:00
defer recordError("EncodeResponse", err)
if !errors.Is(err, ht.ErrInternalServerErrorResponse) {
s.cfg.ErrorHandler(ctx, w, r, err)
}
return
}
}
2024-12-06 00:36:15 +00:00
// handleDeleteScriptPolicyRequest handles deleteScriptPolicy operation.
2024-11-26 01:22:45 +00:00
//
2024-12-06 00:36:15 +00:00
// Delete the specified script policy by ID.
2024-11-26 01:22:45 +00:00
//
2024-12-06 00:36:15 +00:00
// DELETE /script-policy/id/{ScriptPolicyID}
func (s *Server) handleDeleteScriptPolicyRequest(args [1]string, argsEscaped bool, w http.ResponseWriter, r *http.Request) {
statusWriter := &codeRecorder{ResponseWriter: w}
w = statusWriter
2024-11-26 01:22:45 +00:00
otelAttrs := []attribute.KeyValue{
2024-12-06 00:36:15 +00:00
otelogen.OperationID("deleteScriptPolicy"),
semconv.HTTPRequestMethodKey.String("DELETE"),
semconv.HTTPRouteKey.String("/script-policy/id/{ScriptPolicyID}"),
2024-11-26 01:22:45 +00:00
}
// Start a span for this request.
2024-12-06 00:36:15 +00:00
ctx, span := s.cfg.Tracer.Start(r.Context(), DeleteScriptPolicyOperation,
2024-11-26 01:22:45 +00:00
trace.WithAttributes(otelAttrs...),
serverSpanKind,
)
defer span.End()
// Add Labeler to context.
labeler := &Labeler{attrs: otelAttrs}
ctx = contextWithLabeler(ctx, labeler)
// Run stopwatch.
startTime := time.Now()
defer func() {
elapsedDuration := time.Since(startTime)
2024-12-06 00:36:15 +00:00
attrSet := labeler.AttributeSet()
attrs := attrSet.ToSlice()
code := statusWriter.status
if code != 0 {
codeAttr := semconv.HTTPResponseStatusCode(code)
attrs = append(attrs, codeAttr)
span.SetAttributes(codeAttr)
}
attrOpt := metric.WithAttributes(attrs...)
2024-11-26 01:22:45 +00:00
// Increment request counter.
s.requests.Add(ctx, 1, attrOpt)
// Use floating point division here for higher precision (instead of Millisecond method).
2024-12-06 00:36:15 +00:00
s.duration.Record(ctx, float64(elapsedDuration)/float64(time.Millisecond), attrOpt)
2024-11-26 01:22:45 +00:00
}()
var (
recordError = func(stage string, err error) {
span.RecordError(err)
2024-12-06 00:36:15 +00:00
// https://opentelemetry.io/docs/specs/semconv/http/http-spans/#status
// Span Status MUST be left unset if HTTP status code was in the 1xx, 2xx or 3xx ranges,
// unless there was another error (e.g., network error receiving the response body; or 3xx codes with
// max redirects exceeded), in which case status MUST be set to Error.
code := statusWriter.status
if code >= 100 && code < 500 {
span.SetStatus(codes.Error, stage)
}
attrSet := labeler.AttributeSet()
attrs := attrSet.ToSlice()
if code != 0 {
attrs = append(attrs, semconv.HTTPResponseStatusCode(code))
}
s.errors.Add(ctx, 1, metric.WithAttributes(attrs...))
2024-11-26 01:22:45 +00:00
}
err error
opErrContext = ogenerrors.OperationContext{
2024-12-06 00:36:15 +00:00
Name: DeleteScriptPolicyOperation,
ID: "deleteScriptPolicy",
2024-11-26 01:22:45 +00:00
}
)
2024-11-29 22:33:48 +00:00
{
type bitset = [1]uint8
var satisfied bitset
{
2024-12-06 00:36:15 +00:00
sctx, ok, err := s.securityCookieAuth(ctx, DeleteScriptPolicyOperation, r)
2024-11-29 22:33:48 +00:00
if err != nil {
err = &ogenerrors.SecurityError{
OperationContext: opErrContext,
Security: "CookieAuth",
Err: err,
}
if encodeErr := encodeErrorResponse(s.h.NewError(ctx, err), w, span); encodeErr != nil {
defer recordError("Security:CookieAuth", err)
}
return
}
if ok {
satisfied[0] |= 1 << 0
ctx = sctx
}
}
if ok := func() bool {
nextRequirement:
for _, requirement := range []bitset{
{0b00000001},
} {
for i, mask := range requirement {
if satisfied[i]&mask != mask {
continue nextRequirement
}
}
return true
}
return false
}(); !ok {
err = &ogenerrors.SecurityError{
OperationContext: opErrContext,
Err: ogenerrors.ErrSecurityRequirementIsNotSatisfied,
}
if encodeErr := encodeErrorResponse(s.h.NewError(ctx, err), w, span); encodeErr != nil {
defer recordError("Security", err)
}
return
}
}
2024-12-06 00:36:15 +00:00
params, err := decodeDeleteScriptPolicyParams(args, argsEscaped, r)
2024-11-26 01:22:45 +00:00
if err != nil {
err = &ogenerrors.DecodeParamsError{
OperationContext: opErrContext,
Err: err,
}
defer recordError("DecodeParams", err)
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
2024-12-11 06:23:08 +00:00
var response *DeleteScriptPolicyNoContent
2024-11-26 01:22:45 +00:00
if m := s.cfg.Middleware; m != nil {
mreq := middleware.Request{
Context: ctx,
2024-12-06 00:36:15 +00:00
OperationName: DeleteScriptPolicyOperation,
OperationSummary: "Delete the specified script policy by ID",
OperationID: "deleteScriptPolicy",
2024-11-26 01:22:45 +00:00
Body: nil,
Params: middleware.Parameters{
{
2024-12-06 00:36:15 +00:00
Name: "ScriptPolicyID",
2024-11-26 20:39:10 +00:00
In: "path",
2024-12-06 00:36:15 +00:00
}: params.ScriptPolicyID,
},
Raw: r,
}
type (
Request = struct{}
Params = DeleteScriptPolicyParams
2024-12-11 06:23:08 +00:00
Response = *DeleteScriptPolicyNoContent
2024-12-06 00:36:15 +00:00
)
response, err = middleware.HookMiddleware[
Request,
Params,
Response,
](
m,
mreq,
unpackDeleteScriptPolicyParams,
func(ctx context.Context, request Request, params Params) (response Response, err error) {
err = s.h.DeleteScriptPolicy(ctx, params)
return response, err
},
)
} else {
err = s.h.DeleteScriptPolicy(ctx, params)
}
if err != nil {
if errRes, ok := errors.Into[*ErrorStatusCode](err); ok {
if err := encodeErrorResponse(errRes, w, span); err != nil {
defer recordError("Internal", err)
}
return
}
if errors.Is(err, ht.ErrNotImplemented) {
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
if err := encodeErrorResponse(s.h.NewError(ctx, err), w, span); err != nil {
defer recordError("Internal", err)
}
return
}
if err := encodeDeleteScriptPolicyResponse(response, w, span); err != nil {
defer recordError("EncodeResponse", err)
if !errors.Is(err, ht.ErrInternalServerErrorResponse) {
s.cfg.ErrorHandler(ctx, w, r, err)
}
return
}
}
// handleGetScriptRequest handles getScript operation.
//
// Get the specified script by ID.
//
// GET /scripts/{ScriptID}
func (s *Server) handleGetScriptRequest(args [1]string, argsEscaped bool, w http.ResponseWriter, r *http.Request) {
statusWriter := &codeRecorder{ResponseWriter: w}
w = statusWriter
otelAttrs := []attribute.KeyValue{
otelogen.OperationID("getScript"),
semconv.HTTPRequestMethodKey.String("GET"),
semconv.HTTPRouteKey.String("/scripts/{ScriptID}"),
}
// Start a span for this request.
ctx, span := s.cfg.Tracer.Start(r.Context(), GetScriptOperation,
trace.WithAttributes(otelAttrs...),
serverSpanKind,
)
defer span.End()
// Add Labeler to context.
labeler := &Labeler{attrs: otelAttrs}
ctx = contextWithLabeler(ctx, labeler)
// Run stopwatch.
startTime := time.Now()
defer func() {
elapsedDuration := time.Since(startTime)
attrSet := labeler.AttributeSet()
attrs := attrSet.ToSlice()
code := statusWriter.status
if code != 0 {
codeAttr := semconv.HTTPResponseStatusCode(code)
attrs = append(attrs, codeAttr)
span.SetAttributes(codeAttr)
}
attrOpt := metric.WithAttributes(attrs...)
// Increment request counter.
s.requests.Add(ctx, 1, attrOpt)
// Use floating point division here for higher precision (instead of Millisecond method).
s.duration.Record(ctx, float64(elapsedDuration)/float64(time.Millisecond), attrOpt)
}()
var (
recordError = func(stage string, err error) {
span.RecordError(err)
// https://opentelemetry.io/docs/specs/semconv/http/http-spans/#status
// Span Status MUST be left unset if HTTP status code was in the 1xx, 2xx or 3xx ranges,
// unless there was another error (e.g., network error receiving the response body; or 3xx codes with
// max redirects exceeded), in which case status MUST be set to Error.
code := statusWriter.status
if code >= 100 && code < 500 {
span.SetStatus(codes.Error, stage)
}
attrSet := labeler.AttributeSet()
attrs := attrSet.ToSlice()
if code != 0 {
attrs = append(attrs, semconv.HTTPResponseStatusCode(code))
}
s.errors.Add(ctx, 1, metric.WithAttributes(attrs...))
}
err error
opErrContext = ogenerrors.OperationContext{
Name: GetScriptOperation,
ID: "getScript",
}
)
{
type bitset = [1]uint8
var satisfied bitset
{
sctx, ok, err := s.securityCookieAuth(ctx, GetScriptOperation, r)
if err != nil {
err = &ogenerrors.SecurityError{
OperationContext: opErrContext,
Security: "CookieAuth",
Err: err,
}
if encodeErr := encodeErrorResponse(s.h.NewError(ctx, err), w, span); encodeErr != nil {
defer recordError("Security:CookieAuth", err)
}
return
}
if ok {
satisfied[0] |= 1 << 0
ctx = sctx
}
}
if ok := func() bool {
nextRequirement:
for _, requirement := range []bitset{
{0b00000001},
} {
for i, mask := range requirement {
if satisfied[i]&mask != mask {
continue nextRequirement
}
}
return true
}
return false
}(); !ok {
err = &ogenerrors.SecurityError{
OperationContext: opErrContext,
Err: ogenerrors.ErrSecurityRequirementIsNotSatisfied,
}
if encodeErr := encodeErrorResponse(s.h.NewError(ctx, err), w, span); encodeErr != nil {
defer recordError("Security", err)
}
return
}
}
params, err := decodeGetScriptParams(args, argsEscaped, r)
if err != nil {
err = &ogenerrors.DecodeParamsError{
OperationContext: opErrContext,
Err: err,
}
defer recordError("DecodeParams", err)
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
var response *Script
if m := s.cfg.Middleware; m != nil {
mreq := middleware.Request{
Context: ctx,
OperationName: GetScriptOperation,
OperationSummary: "Get the specified script by ID",
OperationID: "getScript",
Body: nil,
Params: middleware.Parameters{
2024-11-26 01:22:45 +00:00
{
2024-12-06 00:36:15 +00:00
Name: "ScriptID",
In: "path",
}: params.ScriptID,
2024-11-26 01:22:45 +00:00
},
Raw: r,
}
type (
Request = struct{}
2024-12-06 00:36:15 +00:00
Params = GetScriptParams
Response = *Script
2024-11-26 01:22:45 +00:00
)
response, err = middleware.HookMiddleware[
Request,
Params,
Response,
](
m,
mreq,
2024-12-06 00:36:15 +00:00
unpackGetScriptParams,
2024-11-26 01:22:45 +00:00
func(ctx context.Context, request Request, params Params) (response Response, err error) {
2024-12-06 00:36:15 +00:00
response, err = s.h.GetScript(ctx, params)
2024-11-26 01:22:45 +00:00
return response, err
},
)
} else {
2024-12-06 00:36:15 +00:00
response, err = s.h.GetScript(ctx, params)
2024-11-26 01:22:45 +00:00
}
if err != nil {
if errRes, ok := errors.Into[*ErrorStatusCode](err); ok {
if err := encodeErrorResponse(errRes, w, span); err != nil {
defer recordError("Internal", err)
}
return
}
if errors.Is(err, ht.ErrNotImplemented) {
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
if err := encodeErrorResponse(s.h.NewError(ctx, err), w, span); err != nil {
defer recordError("Internal", err)
}
return
}
2024-12-06 00:36:15 +00:00
if err := encodeGetScriptResponse(response, w, span); err != nil {
defer recordError("EncodeResponse", err)
if !errors.Is(err, ht.ErrInternalServerErrorResponse) {
s.cfg.ErrorHandler(ctx, w, r, err)
}
return
}
}
// handleGetScriptPolicyRequest handles getScriptPolicy operation.
//
// Get the specified script policy by ID.
//
// GET /script-policy/id/{ScriptPolicyID}
func (s *Server) handleGetScriptPolicyRequest(args [1]string, argsEscaped bool, w http.ResponseWriter, r *http.Request) {
statusWriter := &codeRecorder{ResponseWriter: w}
w = statusWriter
otelAttrs := []attribute.KeyValue{
otelogen.OperationID("getScriptPolicy"),
semconv.HTTPRequestMethodKey.String("GET"),
semconv.HTTPRouteKey.String("/script-policy/id/{ScriptPolicyID}"),
}
// Start a span for this request.
ctx, span := s.cfg.Tracer.Start(r.Context(), GetScriptPolicyOperation,
trace.WithAttributes(otelAttrs...),
serverSpanKind,
)
defer span.End()
// Add Labeler to context.
labeler := &Labeler{attrs: otelAttrs}
ctx = contextWithLabeler(ctx, labeler)
// Run stopwatch.
startTime := time.Now()
defer func() {
elapsedDuration := time.Since(startTime)
attrSet := labeler.AttributeSet()
attrs := attrSet.ToSlice()
code := statusWriter.status
if code != 0 {
codeAttr := semconv.HTTPResponseStatusCode(code)
attrs = append(attrs, codeAttr)
span.SetAttributes(codeAttr)
}
attrOpt := metric.WithAttributes(attrs...)
// Increment request counter.
s.requests.Add(ctx, 1, attrOpt)
// Use floating point division here for higher precision (instead of Millisecond method).
s.duration.Record(ctx, float64(elapsedDuration)/float64(time.Millisecond), attrOpt)
}()
var (
recordError = func(stage string, err error) {
span.RecordError(err)
// https://opentelemetry.io/docs/specs/semconv/http/http-spans/#status
// Span Status MUST be left unset if HTTP status code was in the 1xx, 2xx or 3xx ranges,
// unless there was another error (e.g., network error receiving the response body; or 3xx codes with
// max redirects exceeded), in which case status MUST be set to Error.
code := statusWriter.status
if code >= 100 && code < 500 {
span.SetStatus(codes.Error, stage)
}
attrSet := labeler.AttributeSet()
attrs := attrSet.ToSlice()
if code != 0 {
attrs = append(attrs, semconv.HTTPResponseStatusCode(code))
}
s.errors.Add(ctx, 1, metric.WithAttributes(attrs...))
}
err error
opErrContext = ogenerrors.OperationContext{
Name: GetScriptPolicyOperation,
ID: "getScriptPolicy",
}
)
{
type bitset = [1]uint8
var satisfied bitset
{
sctx, ok, err := s.securityCookieAuth(ctx, GetScriptPolicyOperation, r)
if err != nil {
err = &ogenerrors.SecurityError{
OperationContext: opErrContext,
Security: "CookieAuth",
Err: err,
}
if encodeErr := encodeErrorResponse(s.h.NewError(ctx, err), w, span); encodeErr != nil {
defer recordError("Security:CookieAuth", err)
}
return
}
if ok {
satisfied[0] |= 1 << 0
ctx = sctx
}
}
if ok := func() bool {
nextRequirement:
for _, requirement := range []bitset{
{0b00000001},
} {
for i, mask := range requirement {
if satisfied[i]&mask != mask {
continue nextRequirement
}
}
return true
}
return false
}(); !ok {
err = &ogenerrors.SecurityError{
OperationContext: opErrContext,
Err: ogenerrors.ErrSecurityRequirementIsNotSatisfied,
}
if encodeErr := encodeErrorResponse(s.h.NewError(ctx, err), w, span); encodeErr != nil {
defer recordError("Security", err)
}
return
}
}
params, err := decodeGetScriptPolicyParams(args, argsEscaped, r)
if err != nil {
err = &ogenerrors.DecodeParamsError{
OperationContext: opErrContext,
Err: err,
}
defer recordError("DecodeParams", err)
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
var response *ScriptPolicy
if m := s.cfg.Middleware; m != nil {
mreq := middleware.Request{
Context: ctx,
OperationName: GetScriptPolicyOperation,
OperationSummary: "Get the specified script policy by ID",
OperationID: "getScriptPolicy",
Body: nil,
Params: middleware.Parameters{
{
Name: "ScriptPolicyID",
In: "path",
}: params.ScriptPolicyID,
},
Raw: r,
}
type (
Request = struct{}
Params = GetScriptPolicyParams
Response = *ScriptPolicy
)
response, err = middleware.HookMiddleware[
Request,
Params,
Response,
](
m,
mreq,
unpackGetScriptPolicyParams,
func(ctx context.Context, request Request, params Params) (response Response, err error) {
response, err = s.h.GetScriptPolicy(ctx, params)
return response, err
},
)
} else {
response, err = s.h.GetScriptPolicy(ctx, params)
}
if err != nil {
if errRes, ok := errors.Into[*ErrorStatusCode](err); ok {
if err := encodeErrorResponse(errRes, w, span); err != nil {
defer recordError("Internal", err)
}
return
}
if errors.Is(err, ht.ErrNotImplemented) {
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
if err := encodeErrorResponse(s.h.NewError(ctx, err), w, span); err != nil {
defer recordError("Internal", err)
}
return
}
if err := encodeGetScriptPolicyResponse(response, w, span); err != nil {
defer recordError("EncodeResponse", err)
if !errors.Is(err, ht.ErrInternalServerErrorResponse) {
s.cfg.ErrorHandler(ctx, w, r, err)
}
return
}
}
// handleGetScriptPolicyFromHashRequest handles getScriptPolicyFromHash operation.
//
// Get the policy for the given hash of script source code.
//
// GET /script-policy/hash/{FromScriptHash}
func (s *Server) handleGetScriptPolicyFromHashRequest(args [1]string, argsEscaped bool, w http.ResponseWriter, r *http.Request) {
statusWriter := &codeRecorder{ResponseWriter: w}
w = statusWriter
otelAttrs := []attribute.KeyValue{
otelogen.OperationID("getScriptPolicyFromHash"),
semconv.HTTPRequestMethodKey.String("GET"),
semconv.HTTPRouteKey.String("/script-policy/hash/{FromScriptHash}"),
}
// Start a span for this request.
ctx, span := s.cfg.Tracer.Start(r.Context(), GetScriptPolicyFromHashOperation,
trace.WithAttributes(otelAttrs...),
serverSpanKind,
)
defer span.End()
// Add Labeler to context.
labeler := &Labeler{attrs: otelAttrs}
ctx = contextWithLabeler(ctx, labeler)
// Run stopwatch.
startTime := time.Now()
defer func() {
elapsedDuration := time.Since(startTime)
attrSet := labeler.AttributeSet()
attrs := attrSet.ToSlice()
code := statusWriter.status
if code != 0 {
codeAttr := semconv.HTTPResponseStatusCode(code)
attrs = append(attrs, codeAttr)
span.SetAttributes(codeAttr)
}
attrOpt := metric.WithAttributes(attrs...)
// Increment request counter.
s.requests.Add(ctx, 1, attrOpt)
// Use floating point division here for higher precision (instead of Millisecond method).
s.duration.Record(ctx, float64(elapsedDuration)/float64(time.Millisecond), attrOpt)
}()
var (
recordError = func(stage string, err error) {
span.RecordError(err)
// https://opentelemetry.io/docs/specs/semconv/http/http-spans/#status
// Span Status MUST be left unset if HTTP status code was in the 1xx, 2xx or 3xx ranges,
// unless there was another error (e.g., network error receiving the response body; or 3xx codes with
// max redirects exceeded), in which case status MUST be set to Error.
code := statusWriter.status
if code >= 100 && code < 500 {
span.SetStatus(codes.Error, stage)
}
attrSet := labeler.AttributeSet()
attrs := attrSet.ToSlice()
if code != 0 {
attrs = append(attrs, semconv.HTTPResponseStatusCode(code))
}
s.errors.Add(ctx, 1, metric.WithAttributes(attrs...))
}
err error
opErrContext = ogenerrors.OperationContext{
Name: GetScriptPolicyFromHashOperation,
ID: "getScriptPolicyFromHash",
}
)
{
type bitset = [1]uint8
var satisfied bitset
{
sctx, ok, err := s.securityCookieAuth(ctx, GetScriptPolicyFromHashOperation, r)
if err != nil {
err = &ogenerrors.SecurityError{
OperationContext: opErrContext,
Security: "CookieAuth",
Err: err,
}
if encodeErr := encodeErrorResponse(s.h.NewError(ctx, err), w, span); encodeErr != nil {
defer recordError("Security:CookieAuth", err)
}
return
}
if ok {
satisfied[0] |= 1 << 0
ctx = sctx
}
}
if ok := func() bool {
nextRequirement:
for _, requirement := range []bitset{
{0b00000001},
} {
for i, mask := range requirement {
if satisfied[i]&mask != mask {
continue nextRequirement
}
}
return true
}
return false
}(); !ok {
err = &ogenerrors.SecurityError{
OperationContext: opErrContext,
Err: ogenerrors.ErrSecurityRequirementIsNotSatisfied,
}
if encodeErr := encodeErrorResponse(s.h.NewError(ctx, err), w, span); encodeErr != nil {
defer recordError("Security", err)
}
return
}
}
params, err := decodeGetScriptPolicyFromHashParams(args, argsEscaped, r)
if err != nil {
err = &ogenerrors.DecodeParamsError{
OperationContext: opErrContext,
Err: err,
}
defer recordError("DecodeParams", err)
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
var response *ScriptPolicy
if m := s.cfg.Middleware; m != nil {
mreq := middleware.Request{
Context: ctx,
OperationName: GetScriptPolicyFromHashOperation,
OperationSummary: "Get the policy for the given hash of script source code",
OperationID: "getScriptPolicyFromHash",
Body: nil,
Params: middleware.Parameters{
{
Name: "FromScriptHash",
In: "path",
}: params.FromScriptHash,
},
Raw: r,
}
type (
Request = struct{}
Params = GetScriptPolicyFromHashParams
Response = *ScriptPolicy
)
response, err = middleware.HookMiddleware[
Request,
Params,
Response,
](
m,
mreq,
unpackGetScriptPolicyFromHashParams,
func(ctx context.Context, request Request, params Params) (response Response, err error) {
response, err = s.h.GetScriptPolicyFromHash(ctx, params)
return response, err
},
)
} else {
response, err = s.h.GetScriptPolicyFromHash(ctx, params)
}
if err != nil {
if errRes, ok := errors.Into[*ErrorStatusCode](err); ok {
if err := encodeErrorResponse(errRes, w, span); err != nil {
defer recordError("Internal", err)
}
return
}
if errors.Is(err, ht.ErrNotImplemented) {
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
if err := encodeErrorResponse(s.h.NewError(ctx, err), w, span); err != nil {
defer recordError("Internal", err)
}
return
}
if err := encodeGetScriptPolicyFromHashResponse(response, w, span); err != nil {
defer recordError("EncodeResponse", err)
if !errors.Is(err, ht.ErrInternalServerErrorResponse) {
s.cfg.ErrorHandler(ctx, w, r, err)
}
return
}
}
// handleGetSubmissionRequest handles getSubmission operation.
//
// Retrieve map with ID.
//
// GET /submissions/{SubmissionID}
func (s *Server) handleGetSubmissionRequest(args [1]string, argsEscaped bool, w http.ResponseWriter, r *http.Request) {
statusWriter := &codeRecorder{ResponseWriter: w}
w = statusWriter
otelAttrs := []attribute.KeyValue{
otelogen.OperationID("getSubmission"),
semconv.HTTPRequestMethodKey.String("GET"),
semconv.HTTPRouteKey.String("/submissions/{SubmissionID}"),
}
// Start a span for this request.
ctx, span := s.cfg.Tracer.Start(r.Context(), GetSubmissionOperation,
trace.WithAttributes(otelAttrs...),
serverSpanKind,
)
defer span.End()
// Add Labeler to context.
labeler := &Labeler{attrs: otelAttrs}
ctx = contextWithLabeler(ctx, labeler)
// Run stopwatch.
startTime := time.Now()
defer func() {
elapsedDuration := time.Since(startTime)
attrSet := labeler.AttributeSet()
attrs := attrSet.ToSlice()
code := statusWriter.status
if code != 0 {
codeAttr := semconv.HTTPResponseStatusCode(code)
attrs = append(attrs, codeAttr)
span.SetAttributes(codeAttr)
}
attrOpt := metric.WithAttributes(attrs...)
// Increment request counter.
s.requests.Add(ctx, 1, attrOpt)
// Use floating point division here for higher precision (instead of Millisecond method).
s.duration.Record(ctx, float64(elapsedDuration)/float64(time.Millisecond), attrOpt)
}()
var (
recordError = func(stage string, err error) {
span.RecordError(err)
// https://opentelemetry.io/docs/specs/semconv/http/http-spans/#status
// Span Status MUST be left unset if HTTP status code was in the 1xx, 2xx or 3xx ranges,
// unless there was another error (e.g., network error receiving the response body; or 3xx codes with
// max redirects exceeded), in which case status MUST be set to Error.
code := statusWriter.status
if code >= 100 && code < 500 {
span.SetStatus(codes.Error, stage)
}
attrSet := labeler.AttributeSet()
attrs := attrSet.ToSlice()
if code != 0 {
attrs = append(attrs, semconv.HTTPResponseStatusCode(code))
}
s.errors.Add(ctx, 1, metric.WithAttributes(attrs...))
}
err error
opErrContext = ogenerrors.OperationContext{
Name: GetSubmissionOperation,
ID: "getSubmission",
}
)
{
type bitset = [1]uint8
var satisfied bitset
{
sctx, ok, err := s.securityCookieAuth(ctx, GetSubmissionOperation, r)
if err != nil {
err = &ogenerrors.SecurityError{
OperationContext: opErrContext,
Security: "CookieAuth",
Err: err,
}
if encodeErr := encodeErrorResponse(s.h.NewError(ctx, err), w, span); encodeErr != nil {
defer recordError("Security:CookieAuth", err)
}
return
}
if ok {
satisfied[0] |= 1 << 0
ctx = sctx
}
}
if ok := func() bool {
nextRequirement:
for _, requirement := range []bitset{
{0b00000001},
} {
for i, mask := range requirement {
if satisfied[i]&mask != mask {
continue nextRequirement
}
}
return true
}
return false
}(); !ok {
err = &ogenerrors.SecurityError{
OperationContext: opErrContext,
Err: ogenerrors.ErrSecurityRequirementIsNotSatisfied,
}
if encodeErr := encodeErrorResponse(s.h.NewError(ctx, err), w, span); encodeErr != nil {
defer recordError("Security", err)
}
return
}
}
params, err := decodeGetSubmissionParams(args, argsEscaped, r)
if err != nil {
err = &ogenerrors.DecodeParamsError{
OperationContext: opErrContext,
Err: err,
}
defer recordError("DecodeParams", err)
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
var response *Submission
if m := s.cfg.Middleware; m != nil {
mreq := middleware.Request{
Context: ctx,
OperationName: GetSubmissionOperation,
OperationSummary: "Retrieve map with ID",
OperationID: "getSubmission",
Body: nil,
Params: middleware.Parameters{
{
Name: "SubmissionID",
In: "path",
}: params.SubmissionID,
},
Raw: r,
}
type (
Request = struct{}
Params = GetSubmissionParams
Response = *Submission
)
response, err = middleware.HookMiddleware[
Request,
Params,
Response,
](
m,
mreq,
unpackGetSubmissionParams,
func(ctx context.Context, request Request, params Params) (response Response, err error) {
response, err = s.h.GetSubmission(ctx, params)
return response, err
},
)
} else {
response, err = s.h.GetSubmission(ctx, params)
}
if err != nil {
if errRes, ok := errors.Into[*ErrorStatusCode](err); ok {
if err := encodeErrorResponse(errRes, w, span); err != nil {
defer recordError("Internal", err)
}
return
}
if errors.Is(err, ht.ErrNotImplemented) {
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
if err := encodeErrorResponse(s.h.NewError(ctx, err), w, span); err != nil {
defer recordError("Internal", err)
}
return
}
if err := encodeGetSubmissionResponse(response, w, span); err != nil {
defer recordError("EncodeResponse", err)
if !errors.Is(err, ht.ErrInternalServerErrorResponse) {
s.cfg.ErrorHandler(ctx, w, r, err)
}
return
}
}
// handleListSubmissionsRequest handles listSubmissions operation.
//
// Get list of submissions.
//
// GET /submissions
func (s *Server) handleListSubmissionsRequest(args [0]string, argsEscaped bool, w http.ResponseWriter, r *http.Request) {
statusWriter := &codeRecorder{ResponseWriter: w}
w = statusWriter
otelAttrs := []attribute.KeyValue{
otelogen.OperationID("listSubmissions"),
semconv.HTTPRequestMethodKey.String("GET"),
semconv.HTTPRouteKey.String("/submissions"),
}
// Start a span for this request.
ctx, span := s.cfg.Tracer.Start(r.Context(), ListSubmissionsOperation,
trace.WithAttributes(otelAttrs...),
serverSpanKind,
)
defer span.End()
// Add Labeler to context.
labeler := &Labeler{attrs: otelAttrs}
ctx = contextWithLabeler(ctx, labeler)
// Run stopwatch.
startTime := time.Now()
defer func() {
elapsedDuration := time.Since(startTime)
attrSet := labeler.AttributeSet()
attrs := attrSet.ToSlice()
code := statusWriter.status
if code != 0 {
codeAttr := semconv.HTTPResponseStatusCode(code)
attrs = append(attrs, codeAttr)
span.SetAttributes(codeAttr)
}
attrOpt := metric.WithAttributes(attrs...)
// Increment request counter.
s.requests.Add(ctx, 1, attrOpt)
// Use floating point division here for higher precision (instead of Millisecond method).
s.duration.Record(ctx, float64(elapsedDuration)/float64(time.Millisecond), attrOpt)
}()
var (
recordError = func(stage string, err error) {
span.RecordError(err)
// https://opentelemetry.io/docs/specs/semconv/http/http-spans/#status
// Span Status MUST be left unset if HTTP status code was in the 1xx, 2xx or 3xx ranges,
// unless there was another error (e.g., network error receiving the response body; or 3xx codes with
// max redirects exceeded), in which case status MUST be set to Error.
code := statusWriter.status
if code >= 100 && code < 500 {
span.SetStatus(codes.Error, stage)
}
attrSet := labeler.AttributeSet()
attrs := attrSet.ToSlice()
if code != 0 {
attrs = append(attrs, semconv.HTTPResponseStatusCode(code))
}
s.errors.Add(ctx, 1, metric.WithAttributes(attrs...))
}
err error
opErrContext = ogenerrors.OperationContext{
Name: ListSubmissionsOperation,
ID: "listSubmissions",
}
)
{
type bitset = [1]uint8
var satisfied bitset
{
sctx, ok, err := s.securityCookieAuth(ctx, ListSubmissionsOperation, r)
if err != nil {
err = &ogenerrors.SecurityError{
OperationContext: opErrContext,
Security: "CookieAuth",
Err: err,
}
if encodeErr := encodeErrorResponse(s.h.NewError(ctx, err), w, span); encodeErr != nil {
defer recordError("Security:CookieAuth", err)
}
return
}
if ok {
satisfied[0] |= 1 << 0
ctx = sctx
}
}
if ok := func() bool {
nextRequirement:
for _, requirement := range []bitset{
{0b00000001},
} {
for i, mask := range requirement {
if satisfied[i]&mask != mask {
continue nextRequirement
}
}
return true
}
return false
}(); !ok {
err = &ogenerrors.SecurityError{
OperationContext: opErrContext,
Err: ogenerrors.ErrSecurityRequirementIsNotSatisfied,
}
if encodeErr := encodeErrorResponse(s.h.NewError(ctx, err), w, span); encodeErr != nil {
defer recordError("Security", err)
}
return
}
}
params, err := decodeListSubmissionsParams(args, argsEscaped, r)
if err != nil {
err = &ogenerrors.DecodeParamsError{
OperationContext: opErrContext,
Err: err,
}
defer recordError("DecodeParams", err)
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
var response []Submission
if m := s.cfg.Middleware; m != nil {
mreq := middleware.Request{
Context: ctx,
OperationName: ListSubmissionsOperation,
OperationSummary: "Get list of submissions",
OperationID: "listSubmissions",
Body: nil,
Params: middleware.Parameters{
{
Name: "page",
In: "query",
}: params.Page,
{
Name: "filter",
In: "query",
}: params.Filter,
},
Raw: r,
}
type (
Request = struct{}
Params = ListSubmissionsParams
Response = []Submission
)
response, err = middleware.HookMiddleware[
Request,
Params,
Response,
](
m,
mreq,
unpackListSubmissionsParams,
func(ctx context.Context, request Request, params Params) (response Response, err error) {
response, err = s.h.ListSubmissions(ctx, params)
return response, err
},
)
} else {
response, err = s.h.ListSubmissions(ctx, params)
}
if err != nil {
if errRes, ok := errors.Into[*ErrorStatusCode](err); ok {
if err := encodeErrorResponse(errRes, w, span); err != nil {
defer recordError("Internal", err)
}
return
}
if errors.Is(err, ht.ErrNotImplemented) {
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
if err := encodeErrorResponse(s.h.NewError(ctx, err), w, span); err != nil {
defer recordError("Internal", err)
}
return
}
if err := encodeListSubmissionsResponse(response, w, span); err != nil {
defer recordError("EncodeResponse", err)
if !errors.Is(err, ht.ErrInternalServerErrorResponse) {
s.cfg.ErrorHandler(ctx, w, r, err)
}
return
}
}
2024-12-10 06:06:33 +00:00
// handleSetSubmissionCompletedRequest handles setSubmissionCompleted operation.
2024-12-06 00:36:15 +00:00
//
// Retrieve map with ID.
//
2024-12-10 06:06:33 +00:00
// POST /submissions/{SubmissionID}/completed
func (s *Server) handleSetSubmissionCompletedRequest(args [1]string, argsEscaped bool, w http.ResponseWriter, r *http.Request) {
2024-12-06 00:36:15 +00:00
statusWriter := &codeRecorder{ResponseWriter: w}
w = statusWriter
otelAttrs := []attribute.KeyValue{
2024-12-10 06:06:33 +00:00
otelogen.OperationID("setSubmissionCompleted"),
semconv.HTTPRequestMethodKey.String("POST"),
2024-12-06 00:36:15 +00:00
semconv.HTTPRouteKey.String("/submissions/{SubmissionID}/completed"),
}
// Start a span for this request.
2024-12-10 06:06:33 +00:00
ctx, span := s.cfg.Tracer.Start(r.Context(), SetSubmissionCompletedOperation,
2024-12-06 00:36:15 +00:00
trace.WithAttributes(otelAttrs...),
serverSpanKind,
)
defer span.End()
// Add Labeler to context.
labeler := &Labeler{attrs: otelAttrs}
ctx = contextWithLabeler(ctx, labeler)
// Run stopwatch.
startTime := time.Now()
defer func() {
elapsedDuration := time.Since(startTime)
attrSet := labeler.AttributeSet()
attrs := attrSet.ToSlice()
code := statusWriter.status
if code != 0 {
codeAttr := semconv.HTTPResponseStatusCode(code)
attrs = append(attrs, codeAttr)
span.SetAttributes(codeAttr)
}
attrOpt := metric.WithAttributes(attrs...)
// Increment request counter.
s.requests.Add(ctx, 1, attrOpt)
// Use floating point division here for higher precision (instead of Millisecond method).
s.duration.Record(ctx, float64(elapsedDuration)/float64(time.Millisecond), attrOpt)
}()
var (
recordError = func(stage string, err error) {
span.RecordError(err)
// https://opentelemetry.io/docs/specs/semconv/http/http-spans/#status
// Span Status MUST be left unset if HTTP status code was in the 1xx, 2xx or 3xx ranges,
// unless there was another error (e.g., network error receiving the response body; or 3xx codes with
// max redirects exceeded), in which case status MUST be set to Error.
code := statusWriter.status
if code >= 100 && code < 500 {
span.SetStatus(codes.Error, stage)
}
attrSet := labeler.AttributeSet()
attrs := attrSet.ToSlice()
if code != 0 {
attrs = append(attrs, semconv.HTTPResponseStatusCode(code))
}
s.errors.Add(ctx, 1, metric.WithAttributes(attrs...))
}
err error
opErrContext = ogenerrors.OperationContext{
2024-12-10 06:06:33 +00:00
Name: SetSubmissionCompletedOperation,
ID: "setSubmissionCompleted",
2024-12-06 00:36:15 +00:00
}
)
{
type bitset = [1]uint8
var satisfied bitset
{
2024-12-10 06:06:33 +00:00
sctx, ok, err := s.securityCookieAuth(ctx, SetSubmissionCompletedOperation, r)
2024-12-06 00:36:15 +00:00
if err != nil {
err = &ogenerrors.SecurityError{
OperationContext: opErrContext,
Security: "CookieAuth",
Err: err,
}
if encodeErr := encodeErrorResponse(s.h.NewError(ctx, err), w, span); encodeErr != nil {
defer recordError("Security:CookieAuth", err)
}
return
}
if ok {
satisfied[0] |= 1 << 0
ctx = sctx
}
}
if ok := func() bool {
nextRequirement:
for _, requirement := range []bitset{
{0b00000001},
} {
for i, mask := range requirement {
if satisfied[i]&mask != mask {
continue nextRequirement
}
}
return true
}
return false
}(); !ok {
err = &ogenerrors.SecurityError{
OperationContext: opErrContext,
Err: ogenerrors.ErrSecurityRequirementIsNotSatisfied,
}
if encodeErr := encodeErrorResponse(s.h.NewError(ctx, err), w, span); encodeErr != nil {
defer recordError("Security", err)
}
return
}
}
2024-12-10 06:06:33 +00:00
params, err := decodeSetSubmissionCompletedParams(args, argsEscaped, r)
2024-12-06 00:36:15 +00:00
if err != nil {
err = &ogenerrors.DecodeParamsError{
OperationContext: opErrContext,
Err: err,
}
defer recordError("DecodeParams", err)
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
2024-12-11 06:23:08 +00:00
var response *SetSubmissionCompletedNoContent
2024-12-06 00:36:15 +00:00
if m := s.cfg.Middleware; m != nil {
mreq := middleware.Request{
Context: ctx,
2024-12-10 06:06:33 +00:00
OperationName: SetSubmissionCompletedOperation,
2024-12-06 00:36:15 +00:00
OperationSummary: "Retrieve map with ID",
2024-12-10 06:06:33 +00:00
OperationID: "setSubmissionCompleted",
2024-12-06 00:36:15 +00:00
Body: nil,
Params: middleware.Parameters{
{
Name: "SubmissionID",
In: "path",
}: params.SubmissionID,
},
Raw: r,
}
type (
Request = struct{}
2024-12-10 06:06:33 +00:00
Params = SetSubmissionCompletedParams
2024-12-11 06:23:08 +00:00
Response = *SetSubmissionCompletedNoContent
2024-12-06 00:36:15 +00:00
)
response, err = middleware.HookMiddleware[
Request,
Params,
Response,
](
m,
mreq,
2024-12-10 06:06:33 +00:00
unpackSetSubmissionCompletedParams,
2024-12-06 00:36:15 +00:00
func(ctx context.Context, request Request, params Params) (response Response, err error) {
2024-12-10 06:06:33 +00:00
err = s.h.SetSubmissionCompleted(ctx, params)
2024-12-06 00:36:15 +00:00
return response, err
},
)
} else {
2024-12-10 06:06:33 +00:00
err = s.h.SetSubmissionCompleted(ctx, params)
2024-12-06 00:36:15 +00:00
}
if err != nil {
if errRes, ok := errors.Into[*ErrorStatusCode](err); ok {
if err := encodeErrorResponse(errRes, w, span); err != nil {
defer recordError("Internal", err)
}
return
}
if errors.Is(err, ht.ErrNotImplemented) {
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
if err := encodeErrorResponse(s.h.NewError(ctx, err), w, span); err != nil {
defer recordError("Internal", err)
}
return
}
2024-12-10 06:06:33 +00:00
if err := encodeSetSubmissionCompletedResponse(response, w, span); err != nil {
2024-12-06 00:36:15 +00:00
defer recordError("EncodeResponse", err)
if !errors.Is(err, ht.ErrInternalServerErrorResponse) {
s.cfg.ErrorHandler(ctx, w, r, err)
}
return
}
}
2024-12-10 06:06:33 +00:00
// handleUpdateScriptRequest handles updateScript operation.
2024-12-06 00:36:15 +00:00
//
2024-12-10 06:06:33 +00:00
// Update the specified script by ID.
2024-12-06 00:36:15 +00:00
//
2024-12-10 06:06:33 +00:00
// POST /scripts/{ScriptID}
func (s *Server) handleUpdateScriptRequest(args [1]string, argsEscaped bool, w http.ResponseWriter, r *http.Request) {
2024-12-06 00:36:15 +00:00
statusWriter := &codeRecorder{ResponseWriter: w}
w = statusWriter
otelAttrs := []attribute.KeyValue{
2024-12-10 06:06:33 +00:00
otelogen.OperationID("updateScript"),
semconv.HTTPRequestMethodKey.String("POST"),
semconv.HTTPRouteKey.String("/scripts/{ScriptID}"),
2024-12-06 00:36:15 +00:00
}
// Start a span for this request.
2024-12-10 06:06:33 +00:00
ctx, span := s.cfg.Tracer.Start(r.Context(), UpdateScriptOperation,
2024-12-06 00:36:15 +00:00
trace.WithAttributes(otelAttrs...),
serverSpanKind,
)
defer span.End()
// Add Labeler to context.
labeler := &Labeler{attrs: otelAttrs}
ctx = contextWithLabeler(ctx, labeler)
// Run stopwatch.
startTime := time.Now()
defer func() {
elapsedDuration := time.Since(startTime)
attrSet := labeler.AttributeSet()
attrs := attrSet.ToSlice()
code := statusWriter.status
if code != 0 {
codeAttr := semconv.HTTPResponseStatusCode(code)
attrs = append(attrs, codeAttr)
span.SetAttributes(codeAttr)
}
attrOpt := metric.WithAttributes(attrs...)
// Increment request counter.
s.requests.Add(ctx, 1, attrOpt)
// Use floating point division here for higher precision (instead of Millisecond method).
s.duration.Record(ctx, float64(elapsedDuration)/float64(time.Millisecond), attrOpt)
}()
var (
recordError = func(stage string, err error) {
span.RecordError(err)
// https://opentelemetry.io/docs/specs/semconv/http/http-spans/#status
// Span Status MUST be left unset if HTTP status code was in the 1xx, 2xx or 3xx ranges,
// unless there was another error (e.g., network error receiving the response body; or 3xx codes with
// max redirects exceeded), in which case status MUST be set to Error.
code := statusWriter.status
if code >= 100 && code < 500 {
span.SetStatus(codes.Error, stage)
}
attrSet := labeler.AttributeSet()
attrs := attrSet.ToSlice()
if code != 0 {
attrs = append(attrs, semconv.HTTPResponseStatusCode(code))
}
s.errors.Add(ctx, 1, metric.WithAttributes(attrs...))
}
err error
opErrContext = ogenerrors.OperationContext{
2024-12-10 06:06:33 +00:00
Name: UpdateScriptOperation,
ID: "updateScript",
2024-12-06 00:36:15 +00:00
}
)
{
type bitset = [1]uint8
var satisfied bitset
{
2024-12-10 06:06:33 +00:00
sctx, ok, err := s.securityCookieAuth(ctx, UpdateScriptOperation, r)
2024-12-06 00:36:15 +00:00
if err != nil {
err = &ogenerrors.SecurityError{
OperationContext: opErrContext,
Security: "CookieAuth",
Err: err,
}
if encodeErr := encodeErrorResponse(s.h.NewError(ctx, err), w, span); encodeErr != nil {
defer recordError("Security:CookieAuth", err)
}
return
}
if ok {
satisfied[0] |= 1 << 0
ctx = sctx
}
}
if ok := func() bool {
nextRequirement:
for _, requirement := range []bitset{
{0b00000001},
} {
for i, mask := range requirement {
if satisfied[i]&mask != mask {
continue nextRequirement
}
}
return true
}
return false
}(); !ok {
err = &ogenerrors.SecurityError{
OperationContext: opErrContext,
Err: ogenerrors.ErrSecurityRequirementIsNotSatisfied,
}
if encodeErr := encodeErrorResponse(s.h.NewError(ctx, err), w, span); encodeErr != nil {
defer recordError("Security", err)
}
return
}
}
2024-12-10 06:06:33 +00:00
params, err := decodeUpdateScriptParams(args, argsEscaped, r)
2024-12-06 00:36:15 +00:00
if err != nil {
err = &ogenerrors.DecodeParamsError{
OperationContext: opErrContext,
Err: err,
}
defer recordError("DecodeParams", err)
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
2024-12-10 06:06:33 +00:00
request, close, err := s.decodeUpdateScriptRequest(r)
if err != nil {
err = &ogenerrors.DecodeRequestError{
OperationContext: opErrContext,
Err: err,
}
defer recordError("DecodeRequest", err)
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
defer func() {
if err := close(); err != nil {
recordError("CloseRequest", err)
}
}()
2024-12-06 00:36:15 +00:00
2024-12-11 06:23:08 +00:00
var response *UpdateScriptNoContent
2024-12-06 00:36:15 +00:00
if m := s.cfg.Middleware; m != nil {
mreq := middleware.Request{
Context: ctx,
2024-12-10 06:06:33 +00:00
OperationName: UpdateScriptOperation,
OperationSummary: "Update the specified script by ID",
OperationID: "updateScript",
Body: request,
2024-12-06 00:36:15 +00:00
Params: middleware.Parameters{
{
2024-12-10 06:06:33 +00:00
Name: "ScriptID",
2024-12-06 00:36:15 +00:00
In: "path",
2024-12-10 06:06:33 +00:00
}: params.ScriptID,
2024-12-06 00:36:15 +00:00
},
Raw: r,
}
type (
2024-12-10 06:06:33 +00:00
Request = *ScriptUpdate
Params = UpdateScriptParams
2024-12-11 06:23:08 +00:00
Response = *UpdateScriptNoContent
2024-12-06 00:36:15 +00:00
)
response, err = middleware.HookMiddleware[
Request,
Params,
Response,
](
m,
mreq,
2024-12-10 06:06:33 +00:00
unpackUpdateScriptParams,
2024-12-06 00:36:15 +00:00
func(ctx context.Context, request Request, params Params) (response Response, err error) {
2024-12-10 06:06:33 +00:00
err = s.h.UpdateScript(ctx, request, params)
2024-12-06 00:36:15 +00:00
return response, err
},
)
} else {
2024-12-10 06:06:33 +00:00
err = s.h.UpdateScript(ctx, request, params)
2024-12-06 00:36:15 +00:00
}
if err != nil {
if errRes, ok := errors.Into[*ErrorStatusCode](err); ok {
if err := encodeErrorResponse(errRes, w, span); err != nil {
defer recordError("Internal", err)
}
return
}
if errors.Is(err, ht.ErrNotImplemented) {
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
if err := encodeErrorResponse(s.h.NewError(ctx, err), w, span); err != nil {
defer recordError("Internal", err)
}
return
}
2024-12-10 06:06:33 +00:00
if err := encodeUpdateScriptResponse(response, w, span); err != nil {
2024-12-06 00:36:15 +00:00
defer recordError("EncodeResponse", err)
if !errors.Is(err, ht.ErrInternalServerErrorResponse) {
s.cfg.ErrorHandler(ctx, w, r, err)
}
return
}
}
2024-12-10 06:06:33 +00:00
// handleUpdateScriptPolicyRequest handles updateScriptPolicy operation.
2024-12-06 00:36:15 +00:00
//
2024-12-10 06:06:33 +00:00
// Update the specified script policy by ID.
2024-12-06 00:36:15 +00:00
//
2024-12-10 06:06:33 +00:00
// POST /script-policy/id/{ScriptPolicyID}
func (s *Server) handleUpdateScriptPolicyRequest(args [1]string, argsEscaped bool, w http.ResponseWriter, r *http.Request) {
2024-12-06 00:36:15 +00:00
statusWriter := &codeRecorder{ResponseWriter: w}
w = statusWriter
otelAttrs := []attribute.KeyValue{
2024-12-10 06:06:33 +00:00
otelogen.OperationID("updateScriptPolicy"),
semconv.HTTPRequestMethodKey.String("POST"),
semconv.HTTPRouteKey.String("/script-policy/id/{ScriptPolicyID}"),
2024-12-06 00:36:15 +00:00
}
// Start a span for this request.
2024-12-10 06:06:33 +00:00
ctx, span := s.cfg.Tracer.Start(r.Context(), UpdateScriptPolicyOperation,
2024-12-06 00:36:15 +00:00
trace.WithAttributes(otelAttrs...),
serverSpanKind,
)
defer span.End()
// Add Labeler to context.
labeler := &Labeler{attrs: otelAttrs}
ctx = contextWithLabeler(ctx, labeler)
// Run stopwatch.
startTime := time.Now()
defer func() {
elapsedDuration := time.Since(startTime)
attrSet := labeler.AttributeSet()
attrs := attrSet.ToSlice()
code := statusWriter.status
if code != 0 {
codeAttr := semconv.HTTPResponseStatusCode(code)
attrs = append(attrs, codeAttr)
span.SetAttributes(codeAttr)
}
attrOpt := metric.WithAttributes(attrs...)
// Increment request counter.
s.requests.Add(ctx, 1, attrOpt)
// Use floating point division here for higher precision (instead of Millisecond method).
s.duration.Record(ctx, float64(elapsedDuration)/float64(time.Millisecond), attrOpt)
}()
var (
recordError = func(stage string, err error) {
span.RecordError(err)
// https://opentelemetry.io/docs/specs/semconv/http/http-spans/#status
// Span Status MUST be left unset if HTTP status code was in the 1xx, 2xx or 3xx ranges,
// unless there was another error (e.g., network error receiving the response body; or 3xx codes with
// max redirects exceeded), in which case status MUST be set to Error.
code := statusWriter.status
if code >= 100 && code < 500 {
span.SetStatus(codes.Error, stage)
}
attrSet := labeler.AttributeSet()
attrs := attrSet.ToSlice()
if code != 0 {
attrs = append(attrs, semconv.HTTPResponseStatusCode(code))
}
s.errors.Add(ctx, 1, metric.WithAttributes(attrs...))
}
err error
opErrContext = ogenerrors.OperationContext{
2024-12-10 06:06:33 +00:00
Name: UpdateScriptPolicyOperation,
ID: "updateScriptPolicy",
2024-12-06 00:36:15 +00:00
}
)
{
type bitset = [1]uint8
var satisfied bitset
{
2024-12-10 06:06:33 +00:00
sctx, ok, err := s.securityCookieAuth(ctx, UpdateScriptPolicyOperation, r)
2024-12-06 00:36:15 +00:00
if err != nil {
err = &ogenerrors.SecurityError{
OperationContext: opErrContext,
Security: "CookieAuth",
Err: err,
}
if encodeErr := encodeErrorResponse(s.h.NewError(ctx, err), w, span); encodeErr != nil {
defer recordError("Security:CookieAuth", err)
}
return
}
if ok {
satisfied[0] |= 1 << 0
ctx = sctx
}
}
if ok := func() bool {
nextRequirement:
for _, requirement := range []bitset{
{0b00000001},
} {
for i, mask := range requirement {
if satisfied[i]&mask != mask {
continue nextRequirement
}
}
return true
}
return false
}(); !ok {
err = &ogenerrors.SecurityError{
OperationContext: opErrContext,
Err: ogenerrors.ErrSecurityRequirementIsNotSatisfied,
}
if encodeErr := encodeErrorResponse(s.h.NewError(ctx, err), w, span); encodeErr != nil {
defer recordError("Security", err)
}
return
}
}
2024-12-10 06:06:33 +00:00
params, err := decodeUpdateScriptPolicyParams(args, argsEscaped, r)
2024-12-06 00:36:15 +00:00
if err != nil {
err = &ogenerrors.DecodeParamsError{
OperationContext: opErrContext,
Err: err,
}
defer recordError("DecodeParams", err)
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
2024-12-10 06:06:33 +00:00
request, close, err := s.decodeUpdateScriptPolicyRequest(r)
2024-12-06 00:36:15 +00:00
if err != nil {
err = &ogenerrors.DecodeRequestError{
OperationContext: opErrContext,
Err: err,
}
defer recordError("DecodeRequest", err)
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
defer func() {
if err := close(); err != nil {
recordError("CloseRequest", err)
}
}()
2024-12-11 06:23:08 +00:00
var response *UpdateScriptPolicyNoContent
2024-12-06 00:36:15 +00:00
if m := s.cfg.Middleware; m != nil {
mreq := middleware.Request{
Context: ctx,
2024-12-10 06:06:33 +00:00
OperationName: UpdateScriptPolicyOperation,
OperationSummary: "Update the specified script policy by ID",
OperationID: "updateScriptPolicy",
2024-12-06 00:36:15 +00:00
Body: request,
Params: middleware.Parameters{
{
2024-12-10 06:06:33 +00:00
Name: "ScriptPolicyID",
2024-12-06 00:36:15 +00:00
In: "path",
2024-12-10 06:06:33 +00:00
}: params.ScriptPolicyID,
2024-12-06 00:36:15 +00:00
},
Raw: r,
}
type (
2024-12-10 06:06:33 +00:00
Request = *ScriptPolicyUpdate
Params = UpdateScriptPolicyParams
2024-12-11 06:23:08 +00:00
Response = *UpdateScriptPolicyNoContent
2024-12-06 00:36:15 +00:00
)
response, err = middleware.HookMiddleware[
Request,
Params,
Response,
](
m,
mreq,
2024-12-10 06:06:33 +00:00
unpackUpdateScriptPolicyParams,
2024-12-06 00:36:15 +00:00
func(ctx context.Context, request Request, params Params) (response Response, err error) {
2024-12-10 06:06:33 +00:00
err = s.h.UpdateScriptPolicy(ctx, request, params)
2024-12-06 00:36:15 +00:00
return response, err
},
)
} else {
2024-12-10 06:06:33 +00:00
err = s.h.UpdateScriptPolicy(ctx, request, params)
2024-12-06 00:36:15 +00:00
}
if err != nil {
if errRes, ok := errors.Into[*ErrorStatusCode](err); ok {
if err := encodeErrorResponse(errRes, w, span); err != nil {
defer recordError("Internal", err)
}
return
}
if errors.Is(err, ht.ErrNotImplemented) {
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
if err := encodeErrorResponse(s.h.NewError(ctx, err), w, span); err != nil {
defer recordError("Internal", err)
}
return
}
2024-12-10 06:06:33 +00:00
if err := encodeUpdateScriptPolicyResponse(response, w, span); err != nil {
2024-12-06 00:36:15 +00:00
defer recordError("EncodeResponse", err)
if !errors.Is(err, ht.ErrInternalServerErrorResponse) {
s.cfg.ErrorHandler(ctx, w, r, err)
}
return
}
}
2024-12-10 06:06:33 +00:00
// handleUpdateSubmissionModelRequest handles updateSubmissionModel operation.
2024-12-06 00:36:15 +00:00
//
2024-12-10 06:06:33 +00:00
// Update model following role restrictions.
2024-12-06 00:36:15 +00:00
//
2024-12-10 06:06:33 +00:00
// POST /submissions/{SubmissionID}/model
func (s *Server) handleUpdateSubmissionModelRequest(args [1]string, argsEscaped bool, w http.ResponseWriter, r *http.Request) {
2024-12-06 00:36:15 +00:00
statusWriter := &codeRecorder{ResponseWriter: w}
w = statusWriter
otelAttrs := []attribute.KeyValue{
2024-12-10 06:06:33 +00:00
otelogen.OperationID("updateSubmissionModel"),
semconv.HTTPRequestMethodKey.String("POST"),
semconv.HTTPRouteKey.String("/submissions/{SubmissionID}/model"),
2024-12-06 00:36:15 +00:00
}
// Start a span for this request.
2024-12-10 06:06:33 +00:00
ctx, span := s.cfg.Tracer.Start(r.Context(), UpdateSubmissionModelOperation,
2024-12-06 00:36:15 +00:00
trace.WithAttributes(otelAttrs...),
serverSpanKind,
)
defer span.End()
// Add Labeler to context.
labeler := &Labeler{attrs: otelAttrs}
ctx = contextWithLabeler(ctx, labeler)
// Run stopwatch.
startTime := time.Now()
defer func() {
elapsedDuration := time.Since(startTime)
attrSet := labeler.AttributeSet()
attrs := attrSet.ToSlice()
code := statusWriter.status
if code != 0 {
codeAttr := semconv.HTTPResponseStatusCode(code)
attrs = append(attrs, codeAttr)
span.SetAttributes(codeAttr)
}
attrOpt := metric.WithAttributes(attrs...)
// Increment request counter.
s.requests.Add(ctx, 1, attrOpt)
// Use floating point division here for higher precision (instead of Millisecond method).
s.duration.Record(ctx, float64(elapsedDuration)/float64(time.Millisecond), attrOpt)
}()
var (
recordError = func(stage string, err error) {
span.RecordError(err)
// https://opentelemetry.io/docs/specs/semconv/http/http-spans/#status
// Span Status MUST be left unset if HTTP status code was in the 1xx, 2xx or 3xx ranges,
// unless there was another error (e.g., network error receiving the response body; or 3xx codes with
// max redirects exceeded), in which case status MUST be set to Error.
code := statusWriter.status
if code >= 100 && code < 500 {
span.SetStatus(codes.Error, stage)
}
attrSet := labeler.AttributeSet()
attrs := attrSet.ToSlice()
if code != 0 {
attrs = append(attrs, semconv.HTTPResponseStatusCode(code))
}
s.errors.Add(ctx, 1, metric.WithAttributes(attrs...))
}
err error
opErrContext = ogenerrors.OperationContext{
2024-12-10 06:06:33 +00:00
Name: UpdateSubmissionModelOperation,
ID: "updateSubmissionModel",
2024-12-06 00:36:15 +00:00
}
)
{
type bitset = [1]uint8
var satisfied bitset
{
2024-12-10 06:06:33 +00:00
sctx, ok, err := s.securityCookieAuth(ctx, UpdateSubmissionModelOperation, r)
2024-12-06 00:36:15 +00:00
if err != nil {
err = &ogenerrors.SecurityError{
OperationContext: opErrContext,
Security: "CookieAuth",
Err: err,
}
if encodeErr := encodeErrorResponse(s.h.NewError(ctx, err), w, span); encodeErr != nil {
defer recordError("Security:CookieAuth", err)
}
return
}
if ok {
satisfied[0] |= 1 << 0
ctx = sctx
}
}
if ok := func() bool {
nextRequirement:
for _, requirement := range []bitset{
{0b00000001},
} {
for i, mask := range requirement {
if satisfied[i]&mask != mask {
continue nextRequirement
}
}
return true
}
return false
}(); !ok {
err = &ogenerrors.SecurityError{
OperationContext: opErrContext,
Err: ogenerrors.ErrSecurityRequirementIsNotSatisfied,
}
if encodeErr := encodeErrorResponse(s.h.NewError(ctx, err), w, span); encodeErr != nil {
defer recordError("Security", err)
}
return
}
}
2024-12-10 06:06:33 +00:00
params, err := decodeUpdateSubmissionModelParams(args, argsEscaped, r)
2024-12-06 00:36:15 +00:00
if err != nil {
err = &ogenerrors.DecodeParamsError{
OperationContext: opErrContext,
Err: err,
}
defer recordError("DecodeParams", err)
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
2024-12-11 06:23:08 +00:00
var response *UpdateSubmissionModelNoContent
2024-12-06 00:36:15 +00:00
if m := s.cfg.Middleware; m != nil {
mreq := middleware.Request{
Context: ctx,
2024-12-10 06:06:33 +00:00
OperationName: UpdateSubmissionModelOperation,
OperationSummary: "Update model following role restrictions",
OperationID: "updateSubmissionModel",
Body: nil,
2024-12-06 00:36:15 +00:00
Params: middleware.Parameters{
{
2024-12-10 06:06:33 +00:00
Name: "SubmissionID",
2024-12-06 00:36:15 +00:00
In: "path",
2024-12-10 06:06:33 +00:00
}: params.SubmissionID,
{
Name: "ModelID",
In: "query",
}: params.ModelID,
{
Name: "VersionID",
In: "query",
}: params.VersionID,
2024-12-06 00:36:15 +00:00
},
Raw: r,
}
type (
2024-12-10 06:06:33 +00:00
Request = struct{}
Params = UpdateSubmissionModelParams
2024-12-11 06:23:08 +00:00
Response = *UpdateSubmissionModelNoContent
2024-12-06 00:36:15 +00:00
)
response, err = middleware.HookMiddleware[
Request,
Params,
Response,
](
m,
mreq,
2024-12-10 06:06:33 +00:00
unpackUpdateSubmissionModelParams,
2024-12-06 00:36:15 +00:00
func(ctx context.Context, request Request, params Params) (response Response, err error) {
2024-12-10 06:06:33 +00:00
err = s.h.UpdateSubmissionModel(ctx, params)
2024-12-06 00:36:15 +00:00
return response, err
},
)
} else {
2024-12-10 06:06:33 +00:00
err = s.h.UpdateSubmissionModel(ctx, params)
2024-12-06 00:36:15 +00:00
}
if err != nil {
if errRes, ok := errors.Into[*ErrorStatusCode](err); ok {
if err := encodeErrorResponse(errRes, w, span); err != nil {
defer recordError("Internal", err)
}
return
}
if errors.Is(err, ht.ErrNotImplemented) {
s.cfg.ErrorHandler(ctx, w, r, err)
return
}
if err := encodeErrorResponse(s.h.NewError(ctx, err), w, span); err != nil {
defer recordError("Internal", err)
}
return
}
2024-12-10 06:06:33 +00:00
if err := encodeUpdateSubmissionModelResponse(response, w, span); err != nil {
2024-11-26 01:22:45 +00:00
defer recordError("EncodeResponse", err)
if !errors.Is(err, ht.ErrInternalServerErrorResponse) {
s.cfg.ErrorHandler(ctx, w, r, err)
}
return
}
}