Skip to content

📝 [Proposal]: Make fiber.Ctx implement context.Context #3344

@pjebs

Description

@pjebs

Feature Proposal Description

Having too many different types of contexts is confusing - especially for new people transitioning from other languages to Go. If it's going to be simplified, now is the time to do it.

Why not just make fiber.Ctx implement context.Context?

Currently, fiber.Ctx.Context() context.Context and fiber.Ctx.SetContext(ctx context.Context) do nothing but store a user's custom context.Context into fasthttp's uservalues.

Originally the signature of fasthttp's uservalues only accepted strings. I changed it to anything: https://github.com/valyala/fasthttp/pull/1387/files but the purpose wasn't to dump a custom context.Context into it at the expense of added confusion of another type of context.

If the user wants to pass custom values around from middleware to middleware etc, they can directly store it in fasthttp's request's user values OR even better, we make fiber.Ctx implement context.Context and allow them to store it in there.

type context.Context interface {
	Deadline() (deadline time.Time, ok bool)
	Done() <-chan struct{}
	Err() error
	Value(key any)any
}
type Ctx interface {
	Context() context.Context       <============
	SetContext(ctx context.Context) <============
	Accepts(offers ...string) string
	AcceptsCharsets(offers ...string) string
	AcceptsEncodings(offers ...string) string
	AcceptsLanguages(offers ...string) string
	App() *App
	Append(field string, values ...string)
	Attachment(filename ...string)
	BaseURL() string
	BodyRaw() []byte
	Body() []byte
	ClearCookie(key ...string)
	RequestCtx() *fasthttp.RequestCtx
	Cookie(cookie *Cookie)
	Cookies(key string, defaultValue ...string) string
	Download(file string, filename ...string) error
	Request() *fasthttp.Request
	Response() *fasthttp.Response
	Format(handlers ...ResFmt) error
	AutoFormat(body any) error
	FormFile(key string) (*multipart.FileHeader, error)
	FormValue(key string, defaultValue ...string) string
	Fresh() bool
	Get(key string, defaultValue ...string) string
	GetRespHeader(key string, defaultValue ...string) string
	GetRespHeaders() map[string][]string
	GetReqHeaders() map[string][]string
	Host() string
	Hostname() string
	Port() string
	IP() string
	IPs() []string
	Is(extension string) bool
	JSON(data any, ctype ...string) error
	CBOR(data any, ctype ...string) error
	JSONP(data any, callback ...string) error
	XML(data any) error
	Links(link ...string)
	Locals(key any, value ...any) any
	Location(path string)
	Method(override ...string) string
	MultipartForm() (*multipart.Form, error)
	ClientHelloInfo() *tls.ClientHelloInfo
	Next() error
	RestartRouting() error
	OriginalURL() string
	Params(key string, defaultValue ...string) string
	Path(override ...string) string
	Scheme() string
	Protocol() string
	Query(key string, defaultValue ...string) string
	Queries() map[string]string
	Range(size int) (Range, error)
	Redirect() *Redirect
	ViewBind(vars Map) error
	GetRouteURL(routeName string, params Map) (string, error)
	Render(name string, bind Map, layouts ...string) error
	Route() *Route
	SaveFile(fileheader *multipart.FileHeader, path string) error
	SaveFileToStorage(fileheader *multipart.FileHeader, path string, storage Storage) error
	Secure() bool
	Send(body []byte) error
	SendFile(file string, config ...SendFile) error
	SendStatus(status int) error
	SendString(body string) error
	SendStream(stream io.Reader, size ...int) error
	SendStreamWriter(streamWriter func(*bufio.Writer)) error
	Set(key, val string)
	Subdomains(offset ...int) []string
	Stale() bool
	Status(status int) Ctx
	String() string
	Type(extension string, charset ...string) Ctx
	Vary(fields ...string)
	Write(p []byte) (int, error)
	Writef(f string, a ...any) (int, error)
	WriteString(s string) (int, error)
	XHR() bool
	IsProxyTrusted() bool
	IsFromLocal() bool
	Bind() *Bind
	Reset(fctx *fasthttp.RequestCtx)
	Drop() error
}

Alignment with Express API

N/A

HTTP RFC Standards Compliance

N/A

API Stability

Not backward compatible with v2

Feature Examples

N/A

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have searched for existing issues that describe my proposal before opening this one.
  • I understand that a proposal that does not meet these guidelines may be closed without explanation.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions