refactor Context to CTX and add context parameter to complains custom contexts from styleguide
This commit is contained in:
parent
8a0e99651c
commit
4c8c6121b1
@ -10,7 +10,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (c *Context) BindJSON(v any) error {
|
func (c *CTX) BindJSON(v any) error {
|
||||||
if c.R.Header.Get("Content-Type") != "application/json" {
|
if c.R.Header.Get("Content-Type") != "application/json" {
|
||||||
return http.ErrNotSupported
|
return http.ErrNotSupported
|
||||||
}
|
}
|
||||||
@ -18,7 +18,7 @@ func (c *Context) BindJSON(v any) error {
|
|||||||
return decoder.Decode(v)
|
return decoder.Decode(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) BindForm(v interface{}) error {
|
func (c *CTX) BindForm(v interface{}) error {
|
||||||
if err := c.R.ParseForm(); err != nil {
|
if err := c.R.ParseForm(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@ -55,7 +55,7 @@ func Test_BindJSON(t *testing.T) {
|
|||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
rr := httptest.NewRecorder()
|
rr := httptest.NewRecorder()
|
||||||
|
|
||||||
c := &Context{
|
c := &CTX{
|
||||||
W: rr,
|
W: rr,
|
||||||
R: req,
|
R: req,
|
||||||
}
|
}
|
||||||
@ -89,7 +89,7 @@ func Test_BindForm(t *testing.T) {
|
|||||||
|
|
||||||
rr := httptest.NewRecorder()
|
rr := httptest.NewRecorder()
|
||||||
|
|
||||||
c := &Context{
|
c := &CTX{
|
||||||
W: rr,
|
W: rr,
|
||||||
R: req,
|
R: req,
|
||||||
}
|
}
|
||||||
|
|||||||
23
ron.go
23
ron.go
@ -19,8 +19,7 @@ type (
|
|||||||
|
|
||||||
Middleware func(http.Handler) http.Handler
|
Middleware func(http.Handler) http.Handler
|
||||||
|
|
||||||
Context struct {
|
CTX struct {
|
||||||
C context.Context
|
|
||||||
W http.ResponseWriter
|
W http.ResponseWriter
|
||||||
R *http.Request
|
R *http.Request
|
||||||
E *Engine
|
E *Engine
|
||||||
@ -105,15 +104,15 @@ func (e *Engine) USE(middleware Middleware) {
|
|||||||
e.middleware = append(e.middleware, middleware)
|
e.middleware = append(e.middleware, middleware)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Engine) GET(path string, handler func(*Context)) {
|
func (e *Engine) GET(path string, handler func(*CTX, context.Context)) {
|
||||||
e.mux.HandleFunc(fmt.Sprintf("GET %s", path), func(w http.ResponseWriter, r *http.Request) {
|
e.mux.HandleFunc(fmt.Sprintf("GET %s", path), func(w http.ResponseWriter, r *http.Request) {
|
||||||
handler(&Context{W: w, R: r, E: e})
|
handler(&CTX{W: w, R: r, E: e}, r.Context())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Engine) POST(path string, handler func(*Context)) {
|
func (e *Engine) POST(path string, handler func(*CTX, context.Context)) {
|
||||||
e.mux.HandleFunc(fmt.Sprintf("POST %s", path), func(w http.ResponseWriter, r *http.Request) {
|
e.mux.HandleFunc(fmt.Sprintf("POST %s", path), func(w http.ResponseWriter, r *http.Request) {
|
||||||
handler(&Context{W: w, R: r, E: e})
|
handler(&CTX{W: w, R: r, E: e}, r.Context())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -135,15 +134,15 @@ func (g *groupMux) USE(middleware Middleware) {
|
|||||||
g.middleware = append(g.middleware, middleware)
|
g.middleware = append(g.middleware, middleware)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *groupMux) GET(path string, handler func(*Context)) {
|
func (g *groupMux) GET(path string, handler func(*CTX, context.Context)) {
|
||||||
g.mux.HandleFunc(fmt.Sprintf("GET %s", path), func(w http.ResponseWriter, r *http.Request) {
|
g.mux.HandleFunc(fmt.Sprintf("GET %s", path), func(w http.ResponseWriter, r *http.Request) {
|
||||||
handler(&Context{W: w, R: r, E: g.engine})
|
handler(&CTX{W: w, R: r, E: g.engine}, r.Context())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *groupMux) POST(path string, handler func(*Context)) {
|
func (g *groupMux) POST(path string, handler func(*CTX, context.Context)) {
|
||||||
g.mux.HandleFunc(fmt.Sprintf("POST %s", path), func(w http.ResponseWriter, r *http.Request) {
|
g.mux.HandleFunc(fmt.Sprintf("POST %s", path), func(w http.ResponseWriter, r *http.Request) {
|
||||||
handler(&Context{W: w, R: r, E: g.engine})
|
handler(&CTX{W: w, R: r, E: g.engine}, r.Context())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -179,7 +178,7 @@ func (e *Engine) Static(path, dir string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) JSON(code int, data any) {
|
func (c *CTX) JSON(code int, data any) {
|
||||||
c.W.Header().Set("Content-Type", "application/json")
|
c.W.Header().Set("Content-Type", "application/json")
|
||||||
encoder := json.NewEncoder(c.W)
|
encoder := json.NewEncoder(c.W)
|
||||||
if err := encoder.Encode(data); err != nil {
|
if err := encoder.Encode(data); err != nil {
|
||||||
@ -189,7 +188,7 @@ func (c *Context) JSON(code int, data any) {
|
|||||||
c.W.WriteHeader(code)
|
c.W.WriteHeader(code)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) HTML(code int, name string, td *TemplateData) {
|
func (c *CTX) HTML(code int, name string, td *TemplateData) {
|
||||||
c.W.Header().Set("Content-Type", "text/html; charset=utf-8")
|
c.W.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||||
err := c.E.Render.Template(c.W, name, td)
|
err := c.E.Render.Template(c.W, name, td)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
25
ron_test.go
25
ron_test.go
@ -1,6 +1,7 @@
|
|||||||
package ron
|
package ron
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -82,7 +83,7 @@ func Test_applyEngineConfig(t *testing.T) {
|
|||||||
func Test_ServeHTTP(t *testing.T) {
|
func Test_ServeHTTP(t *testing.T) {
|
||||||
e := New()
|
e := New()
|
||||||
api := e.GROUP("/api")
|
api := e.GROUP("/api")
|
||||||
api.GET("/index", func(c *Context) {
|
api.GET("/index", func(c *CTX, ctx context.Context) {
|
||||||
c.W.WriteHeader(http.StatusOK)
|
c.W.WriteHeader(http.StatusOK)
|
||||||
c.W.Write([]byte("GET API"))
|
c.W.Write([]byte("GET API"))
|
||||||
})
|
})
|
||||||
@ -170,19 +171,19 @@ func Test_GET(t *testing.T) {
|
|||||||
{"resource with param", "GET", "/api/v1/resource/1", http.StatusOK, "GET Resource"},
|
{"resource with param", "GET", "/api/v1/resource/1", http.StatusOK, "GET Resource"},
|
||||||
}
|
}
|
||||||
|
|
||||||
e.GET("/", func(c *Context) {
|
e.GET("/", func(c *CTX, ctx context.Context) {
|
||||||
c.W.WriteHeader(http.StatusOK)
|
c.W.WriteHeader(http.StatusOK)
|
||||||
c.W.Write([]byte("GET Root"))
|
c.W.Write([]byte("GET Root"))
|
||||||
})
|
})
|
||||||
e.GET("/api", func(c *Context) {
|
e.GET("/api", func(c *CTX, ctx context.Context) {
|
||||||
c.W.WriteHeader(http.StatusOK)
|
c.W.WriteHeader(http.StatusOK)
|
||||||
c.W.Write([]byte("GET API"))
|
c.W.Write([]byte("GET API"))
|
||||||
})
|
})
|
||||||
e.GET("/api/v1", func(c *Context) {
|
e.GET("/api/v1", func(c *CTX, ctx context.Context) {
|
||||||
c.W.WriteHeader(http.StatusOK)
|
c.W.WriteHeader(http.StatusOK)
|
||||||
c.W.Write([]byte("GET API v1"))
|
c.W.Write([]byte("GET API v1"))
|
||||||
})
|
})
|
||||||
e.GET("/api/v1/resource/{id}", func(c *Context) {
|
e.GET("/api/v1/resource/{id}", func(c *CTX, ctx context.Context) {
|
||||||
c.W.WriteHeader(http.StatusOK)
|
c.W.WriteHeader(http.StatusOK)
|
||||||
c.W.Write([]byte("GET Resource"))
|
c.W.Write([]byte("GET Resource"))
|
||||||
})
|
})
|
||||||
@ -206,7 +207,7 @@ func Test_GET(t *testing.T) {
|
|||||||
|
|
||||||
func Test_POST(t *testing.T) {
|
func Test_POST(t *testing.T) {
|
||||||
e := New()
|
e := New()
|
||||||
e.POST("/", func(c *Context) {
|
e.POST("/", func(c *CTX, ctx context.Context) {
|
||||||
c.W.WriteHeader(http.StatusOK)
|
c.W.WriteHeader(http.StatusOK)
|
||||||
c.W.Write([]byte("POST"))
|
c.W.Write([]byte("POST"))
|
||||||
})
|
})
|
||||||
@ -227,7 +228,7 @@ func Test_POST(t *testing.T) {
|
|||||||
func Test_GROUP(t *testing.T) {
|
func Test_GROUP(t *testing.T) {
|
||||||
e := New()
|
e := New()
|
||||||
api := e.GROUP("/api")
|
api := e.GROUP("/api")
|
||||||
api.GET("/index", func(c *Context) {
|
api.GET("/index", func(c *CTX, ctx context.Context) {
|
||||||
c.W.WriteHeader(http.StatusOK)
|
c.W.WriteHeader(http.StatusOK)
|
||||||
c.W.Write([]byte("GET API"))
|
c.W.Write([]byte("GET API"))
|
||||||
})
|
})
|
||||||
@ -247,7 +248,7 @@ func Test_GROUP(t *testing.T) {
|
|||||||
|
|
||||||
func Test_GROUPWithMiddleware(t *testing.T) {
|
func Test_GROUPWithMiddleware(t *testing.T) {
|
||||||
e := New()
|
e := New()
|
||||||
e.GET("/index", func(c *Context) {
|
e.GET("/index", func(c *CTX, ctx context.Context) {
|
||||||
c.W.WriteHeader(http.StatusOK)
|
c.W.WriteHeader(http.StatusOK)
|
||||||
c.W.Write([]byte("GET Root"))
|
c.W.Write([]byte("GET Root"))
|
||||||
})
|
})
|
||||||
@ -265,7 +266,7 @@ func Test_GROUPWithMiddleware(t *testing.T) {
|
|||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
api.GET("/index", func(c *Context) {
|
api.GET("/index", func(c *CTX, ctx context.Context) {
|
||||||
c.W.WriteHeader(http.StatusOK)
|
c.W.WriteHeader(http.StatusOK)
|
||||||
c.W.Write([]byte("GET API"))
|
c.W.Write([]byte("GET API"))
|
||||||
})
|
})
|
||||||
@ -286,7 +287,7 @@ func Test_GROUPWithMiddleware(t *testing.T) {
|
|||||||
func Test_GROUPPOST(t *testing.T) {
|
func Test_GROUPPOST(t *testing.T) {
|
||||||
e := New()
|
e := New()
|
||||||
api := e.GROUP("/api")
|
api := e.GROUP("/api")
|
||||||
api.POST("/index", func(c *Context) {
|
api.POST("/index", func(c *CTX, ctx context.Context) {
|
||||||
c.W.WriteHeader(http.StatusOK)
|
c.W.WriteHeader(http.StatusOK)
|
||||||
c.W.Write([]byte("POST API"))
|
c.W.Write([]byte("POST API"))
|
||||||
})
|
})
|
||||||
@ -403,7 +404,7 @@ func Test_JSON(t *testing.T) {
|
|||||||
t.Run(name, func(t *testing.T) {
|
t.Run(name, func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
rr := httptest.NewRecorder()
|
rr := httptest.NewRecorder()
|
||||||
c := &Context{
|
c := &CTX{
|
||||||
W: rr,
|
W: rr,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -447,7 +448,7 @@ func Test_HTML(t *testing.T) {
|
|||||||
t.Run(name, func(t *testing.T) {
|
t.Run(name, func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
rr := httptest.NewRecorder()
|
rr := httptest.NewRecorder()
|
||||||
c := &Context{
|
c := &CTX{
|
||||||
W: rr,
|
W: rr,
|
||||||
E: &Engine{
|
E: &Engine{
|
||||||
Render: NewHTMLRender(),
|
Render: NewHTMLRender(),
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user