feat: if report http server close err as run err
This commit is contained in:
parent
f938487884
commit
286f010346
7
app.go
7
app.go
@ -48,7 +48,12 @@ func (a *App) run(ln net.Listener) error {
|
||||
fmt.Println(banner + "nf serve at: " + ln.Addr().String() + "\n")
|
||||
}
|
||||
|
||||
return a.server.Serve(ln)
|
||||
err := a.server.Serve(ln)
|
||||
if !errors.Is(err, http.ErrServerClosed) || a.config.ErrServeClose {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *App) Run(address string) error {
|
||||
|
12
nf.go
12
nf.go
@ -8,10 +8,14 @@ type Map map[string]interface{}
|
||||
|
||||
type Config struct {
|
||||
// Default: 4 * 1024 * 1024
|
||||
BodyLimit int64 `json:"-"`
|
||||
DisableBanner bool `json:"-"`
|
||||
DisableLogger bool `json:"-"`
|
||||
DisableRecover bool `json:"-"`
|
||||
BodyLimit int64 `json:"-"`
|
||||
|
||||
// if report http.ErrServerClosed as run err
|
||||
ErrServeClose bool `json:"-"`
|
||||
|
||||
DisableBanner bool `json:"-"`
|
||||
DisableLogger bool `json:"-"`
|
||||
DisableRecover bool `json:"-"`
|
||||
}
|
||||
|
||||
var (
|
||||
|
@ -50,7 +50,7 @@ func handlePost(c *nf.Ctx) error {
|
||||
var (
|
||||
err error
|
||||
req = Req{}
|
||||
reqMap = make(map[string]any)
|
||||
reqMap = make(map[string]any)
|
||||
)
|
||||
|
||||
if err = c.BodyParser(&req); err != nil {
|
||||
|
49
xtest/quit/main.go
Normal file
49
xtest/quit/main.go
Normal file
@ -0,0 +1,49 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/loveuer/nf"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := nf.New()
|
||||
quit := make(chan bool)
|
||||
|
||||
app.Get("/name", handleGet)
|
||||
|
||||
go func() {
|
||||
err := app.Run(":7383")
|
||||
log.Print("run with err=", err)
|
||||
}()
|
||||
|
||||
go func() {
|
||||
time.Sleep(5 * time.Second)
|
||||
err := app.Shutdown(context.TODO())
|
||||
log.Print("quit with err=", err)
|
||||
quit <- true
|
||||
}()
|
||||
|
||||
<-quit
|
||||
|
||||
log.Print("quited")
|
||||
}
|
||||
|
||||
func handleGet(c *nf.Ctx) error {
|
||||
type Req struct {
|
||||
Name string `query:"name"`
|
||||
Addr []string `query:"addr"`
|
||||
}
|
||||
|
||||
var (
|
||||
err error
|
||||
req = Req{}
|
||||
)
|
||||
|
||||
if err = c.QueryParser(&req); err != nil {
|
||||
return nf.NewNFError(400, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(nf.Map{"req_map": req})
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user