feat: if report http server close err as run err
This commit is contained in:
		
							
								
								
									
										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 { | ||||
|   | ||||
							
								
								
									
										4
									
								
								nf.go
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								nf.go
									
									
									
									
									
								
							| @@ -9,6 +9,10 @@ type Map map[string]interface{} | ||||
| type Config struct { | ||||
| 	// Default: 4 * 1024 * 1024 | ||||
| 	BodyLimit int64 `json:"-"` | ||||
|  | ||||
| 	// if report http.ErrServerClosed as run err | ||||
| 	ErrServeClose bool `json:"-"` | ||||
|  | ||||
| 	DisableBanner  bool `json:"-"` | ||||
| 	DisableLogger  bool `json:"-"` | ||||
| 	DisableRecover bool `json:"-"` | ||||
|   | ||||
							
								
								
									
										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}) | ||||
| } | ||||
		Reference in New Issue
	
	Block a user