Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
940e86bd8d | |||
5263cba44a | |||
63f7516667 |
19
ctx.go
19
ctx.go
@ -289,23 +289,30 @@ func (c *Ctx) Status(code int) *Ctx {
|
|||||||
c.lock.Lock()
|
c.lock.Lock()
|
||||||
defer c.lock.Unlock()
|
defer c.lock.Unlock()
|
||||||
|
|
||||||
c.writermem.WriteHeader(code)
|
c.Writer.WriteHeader(code)
|
||||||
c.StatusCode = c.writermem.status
|
c.StatusCode = c.writermem.status
|
||||||
|
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set set response header
|
||||||
func (c *Ctx) Set(key string, value string) {
|
func (c *Ctx) Set(key string, value string) {
|
||||||
c.writermem.Header().Set(key, value)
|
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) {
|
func (c *Ctx) SetHeader(key string, value string) {
|
||||||
c.writermem.Header().Set(key, value)
|
c.Writer.Header().Set(key, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Ctx) SendStatus(code int) error {
|
func (c *Ctx) SendStatus(code int) error {
|
||||||
c.Status(code)
|
c.Status(code)
|
||||||
c.writermem.WriteHeaderNow()
|
c.Writer.WriteHeaderNow()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -323,7 +330,7 @@ func (c *Ctx) Writef(format string, values ...interface{}) (int, error) {
|
|||||||
func (c *Ctx) JSON(data interface{}) error {
|
func (c *Ctx) JSON(data interface{}) error {
|
||||||
c.SetHeader("Content-Type", MIMEApplicationJSON)
|
c.SetHeader("Content-Type", MIMEApplicationJSON)
|
||||||
|
|
||||||
encoder := json.NewEncoder(&c.writermem)
|
encoder := json.NewEncoder(c.Writer)
|
||||||
|
|
||||||
if err := encoder.Encode(data); err != nil {
|
if err := encoder.Encode(data); err != nil {
|
||||||
return err
|
return err
|
||||||
@ -356,5 +363,5 @@ func (c *Ctx) HTML(html string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Ctx) Write(data []byte) (int, error) {
|
func (c *Ctx) Write(data []byte) (int, error) {
|
||||||
return c.writermem.Write(data)
|
return c.Writer.Write(data)
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,7 @@ var (
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
defaultLogger = &logger{
|
DefaultLogger = &logger{
|
||||||
Mutex: sync.Mutex{},
|
Mutex: sync.Mutex{},
|
||||||
timeFormat: "2006-01-02T15:04:05",
|
timeFormat: "2006-01-02T15:04:05",
|
||||||
writer: os.Stdout,
|
writer: os.Stdout,
|
||||||
@ -36,32 +36,32 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func SetTimeFormat(format string) {
|
func SetTimeFormat(format string) {
|
||||||
defaultLogger.SetTimeFormat(format)
|
DefaultLogger.SetTimeFormat(format)
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetLogLevel(level LogLevel) {
|
func SetLogLevel(level LogLevel) {
|
||||||
defaultLogger.SetLogLevel(level)
|
DefaultLogger.SetLogLevel(level)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Debug(msg string, data ...any) {
|
func Debug(msg string, data ...any) {
|
||||||
defaultLogger.Debug(msg, data...)
|
DefaultLogger.Debug(msg, data...)
|
||||||
}
|
}
|
||||||
func Info(msg string, data ...any) {
|
func Info(msg string, data ...any) {
|
||||||
defaultLogger.Info(msg, data...)
|
DefaultLogger.Info(msg, data...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Warn(msg string, data ...any) {
|
func Warn(msg string, data ...any) {
|
||||||
defaultLogger.Warn(msg, data...)
|
DefaultLogger.Warn(msg, data...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Error(msg string, data ...any) {
|
func Error(msg string, data ...any) {
|
||||||
defaultLogger.Error(msg, data...)
|
DefaultLogger.Error(msg, data...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Panic(msg string, data ...any) {
|
func Panic(msg string, data ...any) {
|
||||||
defaultLogger.Panic(msg, data...)
|
DefaultLogger.Panic(msg, data...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Fatal(msg string, data ...any) {
|
func Fatal(msg string, data ...any) {
|
||||||
defaultLogger.Fatal(msg, data...)
|
DefaultLogger.Fatal(msg, data...)
|
||||||
}
|
}
|
||||||
|
59
readme.md
59
readme.md
@ -5,8 +5,9 @@
|
|||||||
##### basic usage
|
##### basic usage
|
||||||
|
|
||||||
- get param
|
- get param
|
||||||
```go
|
|
||||||
func main() {
|
```go
|
||||||
|
func main() {
|
||||||
app := nf.New()
|
app := nf.New()
|
||||||
|
|
||||||
app.Get("/hello/:name", func(c *nf.Ctx) error {
|
app.Get("/hello/:name", func(c *nf.Ctx) error {
|
||||||
@ -15,12 +16,13 @@ func main() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
log.Fatal(app.Run("0.0.0.0:80"))
|
log.Fatal(app.Run("0.0.0.0:80"))
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
- parse request query
|
- parse request query
|
||||||
```go
|
|
||||||
func handleQuery(c *nf.Ctx) error {
|
```go
|
||||||
|
func handleQuery(c *nf.Ctx) error {
|
||||||
type Req struct {
|
type Req struct {
|
||||||
Name string `query:"name"`
|
Name string `query:"name"`
|
||||||
Addr []string `query:"addr"`
|
Addr []string `query:"addr"`
|
||||||
@ -36,12 +38,13 @@ func handleQuery(c *nf.Ctx) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return c.JSON(nf.Map{"query": req})
|
return c.JSON(nf.Map{"query": req})
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
- parse application/json body
|
- parse application/json body
|
||||||
```go
|
|
||||||
func handlePost(c *nf.Ctx) error {
|
```go
|
||||||
|
func handlePost(c *nf.Ctx) error {
|
||||||
type Req struct {
|
type Req struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Addr []string `json:"addr"`
|
Addr []string `json:"addr"`
|
||||||
@ -63,5 +66,37 @@ func handlePost(c *nf.Ctx) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return c.JSON(nf.Map{"struct": req, "map": reqMap})
|
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")
|
||||||
|
}
|
||||||
|
|
||||||
|
...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
Reference in New Issue
Block a user