diff options
| -rw-r--r-- | context.go | 40 | ||||
| -rw-r--r-- | test/main.go | 74 |
2 files changed, 98 insertions, 16 deletions
@@ -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") |
