summaryrefslogtreecommitdiffstats
path: root/modules/context
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2023-06-18 15:59:09 +0800
committerGitHub <noreply@github.com>2023-06-18 09:59:09 +0200
commit4e2f1ee58d1aa49f85c3a28a4f96e915f12bdb21 (patch)
treee2751e1c4673f6a051ef04ca156f428a819fdb66 /modules/context
parentfc2115b494e9ba7e4cf7a1440404dce53738b514 (diff)
downloadgitea-4e2f1ee58d1aa49f85c3a28a4f96e915f12bdb21.tar.gz
gitea-4e2f1ee58d1aa49f85c3a28a4f96e915f12bdb21.zip
Refactor web package and context package (#25298)
1. The "web" package shouldn't depends on "modules/context" package, instead, let each "web context" register themselves to the "web" package. 2. The old Init/Free doesn't make sense, so simplify it * The ctx in "Init(ctx)" is never used, and shouldn't be used that way * The "Free" is never called and shouldn't be called because the SSPI instance is shared --------- Co-authored-by: Giteabot <teabot@gitea.io>
Diffstat (limited to 'modules/context')
-rw-r--r--modules/context/api.go8
-rw-r--r--modules/context/base.go6
-rw-r--r--modules/context/context.go8
-rw-r--r--modules/context/private.go8
-rw-r--r--modules/context/response.go17
5 files changed, 42 insertions, 5 deletions
diff --git a/modules/context/api.go b/modules/context/api.go
index 092ad73f31..3c4d020413 100644
--- a/modules/context/api.go
+++ b/modules/context/api.go
@@ -20,6 +20,8 @@ import (
"code.gitea.io/gitea/modules/httpcache"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
+ "code.gitea.io/gitea/modules/web"
+ web_types "code.gitea.io/gitea/modules/web/types"
"gitea.com/go-chi/cache"
)
@@ -41,6 +43,12 @@ type APIContext struct {
Package *Package
}
+func init() {
+ web.RegisterResponseStatusProvider[*APIContext](func(req *http.Request) web_types.ResponseStatusProvider {
+ return req.Context().Value(apiContextKey).(*APIContext)
+ })
+}
+
// Currently, we have the following common fields in error response:
// * message: the message for end users (it shouldn't be used for error type detection)
// if we need to indicate some errors, we should introduce some new fields like ErrorCode or ErrorType
diff --git a/modules/context/base.go b/modules/context/base.go
index c8238050f9..45f33feb08 100644
--- a/modules/context/base.go
+++ b/modules/context/base.go
@@ -96,7 +96,11 @@ func (b *Base) SetTotalCountHeader(total int64) {
// Written returns true if there are something sent to web browser
func (b *Base) Written() bool {
- return b.Resp.Status() > 0
+ return b.Resp.WrittenStatus() != 0
+}
+
+func (b *Base) WrittenStatus() int {
+ return b.Resp.WrittenStatus()
}
// Status writes status code
diff --git a/modules/context/context.go b/modules/context/context.go
index 9e351432c4..93d448fca5 100644
--- a/modules/context/context.go
+++ b/modules/context/context.go
@@ -21,7 +21,9 @@ import (
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/templates"
"code.gitea.io/gitea/modules/translation"
+ "code.gitea.io/gitea/modules/web"
"code.gitea.io/gitea/modules/web/middleware"
+ web_types "code.gitea.io/gitea/modules/web/types"
"gitea.com/go-chi/cache"
"gitea.com/go-chi/session"
@@ -58,6 +60,12 @@ type Context struct {
Package *Package
}
+func init() {
+ web.RegisterResponseStatusProvider[*Context](func(req *http.Request) web_types.ResponseStatusProvider {
+ return req.Context().Value(WebContextKey).(*Context)
+ })
+}
+
// TrHTMLEscapeArgs runs ".Locale.Tr()" but pre-escapes all arguments with html.EscapeString.
// This is useful if the locale message is intended to only produce HTML content.
func (ctx *Context) TrHTMLEscapeArgs(msg string, args ...string) string {
diff --git a/modules/context/private.go b/modules/context/private.go
index 41ca8a4709..2e9b31140a 100644
--- a/modules/context/private.go
+++ b/modules/context/private.go
@@ -11,6 +11,8 @@ import (
"code.gitea.io/gitea/modules/graceful"
"code.gitea.io/gitea/modules/process"
+ "code.gitea.io/gitea/modules/web"
+ web_types "code.gitea.io/gitea/modules/web/types"
)
// PrivateContext represents a context for private routes
@@ -21,6 +23,12 @@ type PrivateContext struct {
Repo *Repository
}
+func init() {
+ web.RegisterResponseStatusProvider[*PrivateContext](func(req *http.Request) web_types.ResponseStatusProvider {
+ return req.Context().Value(privateContextKey).(*PrivateContext)
+ })
+}
+
// Deadline is part of the interface for context.Context and we pass this to the request context
func (ctx *PrivateContext) Deadline() (deadline time.Time, ok bool) {
if ctx.Override != nil {
diff --git a/modules/context/response.go b/modules/context/response.go
index 8708d77da0..2f271f211b 100644
--- a/modules/context/response.go
+++ b/modules/context/response.go
@@ -5,15 +5,20 @@ package context
import (
"net/http"
+
+ web_types "code.gitea.io/gitea/modules/web/types"
)
// ResponseWriter represents a response writer for HTTP
type ResponseWriter interface {
http.ResponseWriter
http.Flusher
- Status() int
+ web_types.ResponseStatusProvider
+
Before(func(ResponseWriter))
- Size() int // used by access logger template
+
+ Status() int // used by access logger template
+ Size() int // used by access logger template
}
var _ ResponseWriter = &Response{}
@@ -46,6 +51,10 @@ func (r *Response) Write(bs []byte) (int, error) {
return size, nil
}
+func (r *Response) Status() int {
+ return r.status
+}
+
func (r *Response) Size() int {
return r.written
}
@@ -71,8 +80,8 @@ func (r *Response) Flush() {
}
}
-// Status returned status code written
-func (r *Response) Status() int {
+// WrittenStatus returned status code written
+func (r *Response) WrittenStatus() int {
return r.status
}