You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

private.go 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package context
  5. import (
  6. "context"
  7. "net/http"
  8. )
  9. // PrivateContext represents a context for private routes
  10. type PrivateContext struct {
  11. *Context
  12. }
  13. var (
  14. privateContextKey interface{} = "default_private_context"
  15. )
  16. // WithPrivateContext set up private context in request
  17. func WithPrivateContext(req *http.Request, ctx *PrivateContext) *http.Request {
  18. return req.WithContext(context.WithValue(req.Context(), privateContextKey, ctx))
  19. }
  20. // GetPrivateContext returns a context for Private routes
  21. func GetPrivateContext(req *http.Request) *PrivateContext {
  22. return req.Context().Value(privateContextKey).(*PrivateContext)
  23. }
  24. // PrivateContexter returns apicontext as middleware
  25. func PrivateContexter() func(http.Handler) http.Handler {
  26. return func(next http.Handler) http.Handler {
  27. return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  28. ctx := &PrivateContext{
  29. Context: &Context{
  30. Resp: NewResponse(w),
  31. Data: map[string]interface{}{},
  32. },
  33. }
  34. ctx.Req = WithPrivateContext(req, ctx)
  35. next.ServeHTTP(ctx.Resp, ctx.Req)
  36. })
  37. }
  38. }