summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEltrac <hi@guhub.cn>2025-11-27 11:13:47 +0800
committerEltrac <hi@guhub.cn>2025-11-27 11:13:47 +0800
commit2447724fe676bff78e31e0c245b65e1f1608010e (patch)
tree6246a7b6a8aacf6d3c2750dc52cdfb8549903e5f
parent5e49bd23b5d9622db58f0aab129969ffc27bb935 (diff)
feat: add context methods that write responses fast
-rw-r--r--context.go40
-rw-r--r--test/main.go74
2 files changed, 98 insertions, 16 deletions
diff --git a/context.go b/context.go
index 628cb4f..98f52c7 100644
--- a/context.go
+++ b/context.go
@@ -1,7 +1,9 @@
package growel
import (
+ "encoding/json"
"net/http"
+ "strconv"
)
type Context struct {
@@ -10,4 +12,42 @@ type Context struct {
Params map[string]string
}
+func (c *Context) Plain(code int, data string) {
+ c.W.Header().Set("Content-Type", "text/plain; charset=utf-8")
+ c.W.WriteHeader(code)
+ c.W.Write([]byte(data))
+}
+
+func (c *Context) JSON(code int, data any) {
+ c.W.Header().Set("Content-Type", "application/json; charset=utf-8")
+ c.W.WriteHeader(code)
+
+ enc := json.NewEncoder(c.W)
+ _ = enc.Encode(data)
+}
+
+func (c *Context) Error(code int, msg string) {
+ c.JSON(code, map[string]string{
+ "error": http.StatusText(code),
+ "message": msg,
+ "status": strconv.Itoa(code),
+ })
+}
+
+func (c *Context) NotFound(msg string) {
+ c.Error(http.StatusNotFound, msg)
+}
+
+func (c *Context) BadRequest(msg string) {
+ c.Error(http.StatusBadRequest, msg)
+}
+
+func (c *Context) Unauthorized(msg string) {
+ c.Error(http.StatusUnauthorized, msg)
+}
+
+func (c *Context) InternalError(msg string) {
+ c.Error(http.StatusInternalServerError, msg)
+}
+
type Handler func(c *Context)
diff --git a/test/main.go b/test/main.go
index 553456d..1f6c36b 100644
--- a/test/main.go
+++ b/test/main.go
@@ -1,36 +1,78 @@
package main
import (
- "fmt"
-
"github.com/bigcoke233/growel/v2"
+ "strconv"
)
+type User struct {
+ ID int `json:"id"`
+ Name string `json:"name"`
+ Group string `json:"group"`
+}
+
+var Users = []User{
+ {ID: 1, Name: "Alice Chen", Group: "admin"},
+ {ID: 2, Name: "Bob Wu", Group: "admin"},
+
+ {ID: 3, Name: "Cynthia Lin", Group: "staff"},
+ {ID: 4, Name: "Daniel Zhao", Group: "staff"},
+ {ID: 5, Name: "Emma Huang", Group: "staff"},
+
+ {ID: 6, Name: "Felix Sun", Group: "editor"},
+ {ID: 7, Name: "Grace Liu", Group: "editor"},
+
+ {ID: 8, Name: "Henry Qiu", Group: "guest"},
+ {ID: 9, Name: "Ivy Xu", Group: "guest"},
+ {ID: 10, Name: "Jack Ma", Group: "guest"},
+
+ {ID: 11, Name: "Karen He", Group: "beta"},
+ {ID: 12, Name: "Leo Fang", Group: "beta"},
+
+ {ID: 13, Name: "Mia Tang", Group: "test"},
+ {ID: 14, Name: "Noah Jin", Group: "test"},
+ {ID: 15, Name: "Olivia Ding", Group: "test"},
+
+ {ID: 16, Name: "Peter Yan", Group: "ops"},
+ {ID: 17, Name: "Queena Zhou", Group: "ops"},
+ {ID: 18, Name: "Ryan Li", Group: "ops"},
+
+ {ID: 19, Name: "Sophia Wang", Group: "support"},
+ {ID: 20, Name: "Tony Du", Group: "support"},
+}
+
func main() {
api := growel.New()
- api.GET("/hello", func(c *growel.Context) {
- c.W.Write([]byte("Hello, World!"))
- })
-
api.GET("/", func(c *growel.Context) {
- c.W.Write([]byte("This is home."))
+ c.Plain(200, "This is home.")
})
- api.GET("/test", func(c *growel.Context) {
- c.W.Write([]byte("testing"))
+ api.GET("/hello", func(c *growel.Context) {
+ c.JSON(200, map[string]string{
+ "message": "Hello Growel!",
+ })
})
- api.GET("/test/hello", func(c *growel.Context) {
- c.W.Write([]byte("testing hello"))
+ api.GET("/user/", func(c *growel.Context) {
+ c.JSON(200, Users)
})
- api.GET("/test/:uid", func(c *growel.Context) {
- c.W.Write([]byte(fmt.Sprintf("Testing from %s", c.Params["uid"])))
- })
+ api.GET("/user/:uid", func(c *growel.Context) {
+ uid, err := strconv.Atoi(c.Params["uid"])
+ if err != nil {
+ c.BadRequest("Invalid user ID")
+ return
+ }
+
+ for _, user := range Users {
+ if user.ID == uid {
+ c.JSON(200, user)
+ return
+ }
+ }
- api.GET("/test/:uid/info/:sid", func(c *growel.Context) {
- c.W.Write([]byte(fmt.Sprintf("Testing from %s info %s", c.Params["uid"], c.Params["sid"])))
+ c.NotFound("User not found")
})
api.Start(":8080")