Compare commits

...

2 Commits

Author SHA1 Message Date
940e86bd8d chore: update go module, readme 2024-08-27 14:39:24 +08:00
5263cba44a chore: expose default logger
chore: add ctx alias method
2024-08-12 16:14:16 +08:00
3 changed files with 101 additions and 59 deletions

7
ctx.go
View File

@ -295,10 +295,17 @@ func (c *Ctx) Status(code int) *Ctx {
return c
}
// Set set response header
func (c *Ctx) Set(key string, value string) {
c.Writer.Header().Set(key, value)
}
// AddHeader add response header
func (c *Ctx) AddHeader(key string, value string) {
c.Writer.Header().Add(key, value)
}
// SetHeader set response header
func (c *Ctx) SetHeader(key string, value string) {
c.Writer.Header().Set(key, value)
}

View File

@ -21,7 +21,7 @@ var (
os.Exit(1)
}
defaultLogger = &logger{
DefaultLogger = &logger{
Mutex: sync.Mutex{},
timeFormat: "2006-01-02T15:04:05",
writer: os.Stdout,
@ -36,32 +36,32 @@ var (
)
func SetTimeFormat(format string) {
defaultLogger.SetTimeFormat(format)
DefaultLogger.SetTimeFormat(format)
}
func SetLogLevel(level LogLevel) {
defaultLogger.SetLogLevel(level)
DefaultLogger.SetLogLevel(level)
}
func Debug(msg string, data ...any) {
defaultLogger.Debug(msg, data...)
DefaultLogger.Debug(msg, data...)
}
func Info(msg string, data ...any) {
defaultLogger.Info(msg, data...)
DefaultLogger.Info(msg, data...)
}
func Warn(msg string, data ...any) {
defaultLogger.Warn(msg, data...)
DefaultLogger.Warn(msg, data...)
}
func Error(msg string, data ...any) {
defaultLogger.Error(msg, data...)
DefaultLogger.Error(msg, data...)
}
func Panic(msg string, data ...any) {
defaultLogger.Panic(msg, data...)
DefaultLogger.Panic(msg, data...)
}
func Fatal(msg string, data ...any) {
defaultLogger.Fatal(msg, data...)
DefaultLogger.Fatal(msg, data...)
}

View File

@ -5,8 +5,9 @@
##### basic usage
- get param
```go
func main() {
```go
func main() {
app := nf.New()
app.Get("/hello/:name", func(c *nf.Ctx) error {
@ -15,12 +16,13 @@ func main() {
})
log.Fatal(app.Run("0.0.0.0:80"))
}
```
}
```
- parse request query
```go
func handleQuery(c *nf.Ctx) error {
```go
func handleQuery(c *nf.Ctx) error {
type Req struct {
Name string `query:"name"`
Addr []string `query:"addr"`
@ -36,12 +38,13 @@ func handleQuery(c *nf.Ctx) error {
}
return c.JSON(nf.Map{"query": req})
}
```
}
```
- parse application/json body
```go
func handlePost(c *nf.Ctx) error {
```go
func handlePost(c *nf.Ctx) error {
type Req struct {
Name string `json:"name"`
Addr []string `json:"addr"`
@ -63,5 +66,37 @@ func handlePost(c *nf.Ctx) error {
}
return c.JSON(nf.Map{"struct": req, "map": reqMap})
}
```
}
```
- pass local value
```go
type User struct {
Id int
Username string
}
func main() {
app := nf.New()
app.Use(auth())
app.Get("/item/list", list)
}
func auth() nf.HandlerFunc {
return func(c *nf.Ctx) error {
c.Locals("user", &User{Id: 1, Username:"user"})
return c.Next()
}
}
func list(c *nf.Ctx) error {
user, ok := c.Locals("user").(*User)
if !ok {
return c.Status(401).SendString("login required")
}
...
}
```