integrate context to struct and fix paths

This commit is contained in:
2025-05-09 17:50:18 +02:00
parent ace952cae7
commit fd78380cb4
3 changed files with 157 additions and 31 deletions
+41 -12
View File
@@ -26,9 +26,10 @@ type (
}
CTX struct {
W *responseWriterWrapper
R *http.Request
E *Engine
W *responseWriterWrapper
R *http.Request
E *Engine
Ctx context.Context
}
Config struct {
@@ -57,7 +58,7 @@ const (
HeaderJSON string = "application/json"
HeaderHTML_UTF8 string = "text/html; charset=utf-8"
HeaderCSS_UTF8 string = "text/css; charset=utf-8"
HeaderAppJS string = "application/javascript"
HeaderAppJS_UTF8 string = "text/javascript; charset=utf-8"
HeaderPlain_UTF8 string = "text/plain; charset=utf-8"
)
@@ -139,17 +140,31 @@ func (e *Engine) USE(middleware Middleware) {
e.middleware = append(e.middleware, middleware)
}
func (e *Engine) GET(path string, handler func(*CTX, context.Context)) {
func (e *Engine) GET(path string, handler func(*CTX)) {
e.mux.HandleFunc(fmt.Sprintf("GET %s", path), func(w http.ResponseWriter, r *http.Request) {
rw := &responseWriterWrapper{ResponseWriter: w}
handler(&CTX{W: rw, R: r, E: e}, r.Context())
handler(&CTX{W: rw, R: r, E: e, Ctx: r.Context()})
})
}
func (e *Engine) POST(path string, handler func(*CTX, context.Context)) {
func (e *Engine) POST(path string, handler func(*CTX)) {
e.mux.HandleFunc(fmt.Sprintf("POST %s", path), func(w http.ResponseWriter, r *http.Request) {
rw := &responseWriterWrapper{ResponseWriter: w}
handler(&CTX{W: rw, R: r, E: e}, r.Context())
handler(&CTX{W: rw, R: r, E: e, Ctx: r.Context()})
})
}
func (e *Engine) PUT(path string, handler func(*CTX)) {
e.mux.HandleFunc(fmt.Sprintf("PUT %s", path), func(w http.ResponseWriter, r *http.Request) {
rw := &responseWriterWrapper{ResponseWriter: w}
handler(&CTX{W: rw, R: r, E: e, Ctx: r.Context()})
})
}
func (e *Engine) DELETE(path string, handler func(*CTX)) {
e.mux.HandleFunc(fmt.Sprintf("DELETE %s", path), func(w http.ResponseWriter, r *http.Request) {
rw := &responseWriterWrapper{ResponseWriter: w}
handler(&CTX{W: rw, R: r, E: e, Ctx: r.Context()})
})
}
@@ -171,17 +186,31 @@ func (g *groupMux) USE(middleware Middleware) {
g.middleware = append(g.middleware, middleware)
}
func (g *groupMux) GET(path string, handler func(*CTX, context.Context)) {
func (g *groupMux) GET(path string, handler func(*CTX)) {
g.mux.HandleFunc(fmt.Sprintf("GET %s", path), func(w http.ResponseWriter, r *http.Request) {
rw := &responseWriterWrapper{ResponseWriter: w}
handler(&CTX{W: rw, R: r, E: g.engine}, r.Context())
handler(&CTX{W: rw, R: r, E: g.engine, Ctx: r.Context()})
})
}
func (g *groupMux) POST(path string, handler func(*CTX, context.Context)) {
func (g *groupMux) POST(path string, handler func(*CTX)) {
g.mux.HandleFunc(fmt.Sprintf("POST %s", path), func(w http.ResponseWriter, r *http.Request) {
rw := &responseWriterWrapper{ResponseWriter: w}
handler(&CTX{W: rw, R: r, E: g.engine}, r.Context())
handler(&CTX{W: rw, R: r, E: g.engine, Ctx: r.Context()})
})
}
func (g *groupMux) PUT(path string, handler func(*CTX)) {
g.mux.HandleFunc(fmt.Sprintf("PUT %s", path), func(w http.ResponseWriter, r *http.Request) {
rw := &responseWriterWrapper{ResponseWriter: w}
handler(&CTX{W: rw, R: r, E: g.engine, Ctx: r.Context()})
})
}
func (g *groupMux) DELETE(path string, handler func(*CTX)) {
g.mux.HandleFunc(fmt.Sprintf("DELETE %s", path), func(w http.ResponseWriter, r *http.Request) {
rw := &responseWriterWrapper{ResponseWriter: w}
handler(&CTX{W: rw, R: r, E: g.engine, Ctx: r.Context()})
})
}