mirror of
https://github.com/versity/versitygw.git
synced 2025-12-23 05:05:16 +00:00
Fiber includes a built-in panic recovery middleware that catches panics in route handlers and middlewares, preventing the server from crashing and allowing it to recover. Alongside this, a stack trace handler has been implemented to store system panics in the context locals (stack). Both the S3 API server and the Admin server use a global error handler to catch unexpected exceptions and recovered panics. The middleware’s logic is to log the panic or internal error and return an S3-style internal server error response. Additionally, dedicated **Panic** and **InternalError** loggers have been added to the `s3api` debug logger to record system panics and internal errors in the console.
72 lines
2.1 KiB
Go
72 lines
2.1 KiB
Go
// Copyright 2023 Versity Software
|
|
// This file is licensed under the Apache License, Version 2.0
|
|
// (the "License"); you may not use this file except in compliance
|
|
// with the License. You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing,
|
|
// software distributed under the License is distributed on an
|
|
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
// KIND, either express or implied. See the License for the
|
|
// specific language governing permissions and limitations
|
|
// under the License.
|
|
|
|
package utils
|
|
|
|
import (
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
// Region, StartTime, IsRoot, Account, AccessKey context locals
|
|
// are set to default values in middlewares.SetDefaultValues
|
|
// to avoid the nil interface conversions
|
|
type ContextKey string
|
|
|
|
const (
|
|
ContextKeyRegion ContextKey = "region"
|
|
ContextKeyStartTime ContextKey = "start-time"
|
|
ContextKeyIsRoot ContextKey = "is-root"
|
|
ContextKeyRootAccessKey ContextKey = "root-access-key"
|
|
ContextKeyAccount ContextKey = "account"
|
|
ContextKeyAuthenticated ContextKey = "authenticated"
|
|
ContextKeyPublicBucket ContextKey = "public-bucket"
|
|
ContextKeyParsedAcl ContextKey = "parsed-acl"
|
|
ContextKeySkipResBodyLog ContextKey = "skip-res-body-log"
|
|
ContextKeyBodyReader ContextKey = "body-reader"
|
|
ContextKeySkip ContextKey = "__skip"
|
|
ContextKeyStack ContextKey = "stack"
|
|
)
|
|
|
|
func (ck ContextKey) Values() []ContextKey {
|
|
return []ContextKey{
|
|
ContextKeyRegion,
|
|
ContextKeyStartTime,
|
|
ContextKeyIsRoot,
|
|
ContextKeyRootAccessKey,
|
|
ContextKeyAccount,
|
|
ContextKeyAuthenticated,
|
|
ContextKeyPublicBucket,
|
|
ContextKeyParsedAcl,
|
|
ContextKeySkipResBodyLog,
|
|
ContextKeyBodyReader,
|
|
}
|
|
}
|
|
|
|
func (ck ContextKey) Set(ctx *fiber.Ctx, val any) {
|
|
ctx.Locals(string(ck), val)
|
|
}
|
|
|
|
func (ck ContextKey) IsSet(ctx *fiber.Ctx) bool {
|
|
val := ctx.Locals(string(ck))
|
|
return val != nil
|
|
}
|
|
|
|
func (ck ContextKey) Delete(ctx *fiber.Ctx) {
|
|
ctx.Locals(string(ck), nil)
|
|
}
|
|
|
|
func (ck ContextKey) Get(ctx *fiber.Ctx) any {
|
|
return ctx.Locals(string(ck))
|
|
}
|