1234567891011121314151617181920212223242526272829303132 |
- package middleware
- import (
- "context"
- "fmt"
- "github.com/labstack/echo/v4"
- "net/http"
- )
- func InstanceId() echo.MiddlewareFunc {
- return func(next echo.HandlerFunc) echo.HandlerFunc {
- return func(c echo.Context) error {
- s := c.Request().Header.Get("X-Instance-ID")
- if s == "" {
- return echo.NewHTTPError(http.StatusForbidden, "missing or malformed instance id")
- }
- value := context.WithValue(c.Request().Context(), keyInstanceId, s)
- c.SetRequest(c.Request().WithContext(value))
- return next(c)
- }
- }
- }
- func GetInstanceId(ctx context.Context) (*string, error) {
- id, ok := ctx.Value(keyInstanceId).(string)
- if !ok {
- return nil, fmt.Errorf("missing or malformed instance id")
- }
- return &id, nil
- }
|