This commit is contained in:
parent
3adfce18cb
commit
0442fc1e2e
2 changed files with 17 additions and 0 deletions
|
@ -19,6 +19,7 @@ import (
|
|||
// Server is the interface for an HTTP server.
|
||||
type Server interface {
|
||||
Get(path string, handler Handler)
|
||||
Ready() chan struct{}
|
||||
Request(method string, path string, headers []Header, body io.Reader) Response
|
||||
Router() *router.Router[Handler]
|
||||
Run(address string) error
|
||||
|
@ -31,6 +32,7 @@ type server struct {
|
|||
contextPool sync.Pool
|
||||
router *router.Router[Handler]
|
||||
errorHandler func(Context, error)
|
||||
ready chan struct{}
|
||||
}
|
||||
|
||||
// NewServer creates a new HTTP server.
|
||||
|
@ -54,6 +56,7 @@ func NewServer() Server {
|
|||
errorHandler: func(ctx Context, err error) {
|
||||
log.Println(ctx.Request().Path(), err)
|
||||
},
|
||||
ready: make(chan struct{}),
|
||||
}
|
||||
|
||||
s.contextPool.New = func() any { return s.newContext() }
|
||||
|
@ -65,6 +68,11 @@ func (s *server) Get(path string, handler Handler) {
|
|||
s.Router().Add("GET", path, handler)
|
||||
}
|
||||
|
||||
// Ready returns a channel that will be closed once the listener is ready for connection handling.
|
||||
func (s *server) Ready() chan struct{} {
|
||||
return s.ready
|
||||
}
|
||||
|
||||
// Request performs a synthetic request and returns the response.
|
||||
// This function keeps the response in memory so it's slightly slower than a real request.
|
||||
// However it is very useful inside tests where you don't want to spin up a real web server.
|
||||
|
@ -99,6 +107,7 @@ func (s *server) Run(address string) error {
|
|||
|
||||
stop := make(chan os.Signal, 1)
|
||||
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
|
||||
close(s.ready)
|
||||
<-stop
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue