完善 exegroup 的文档并提供 http 服务函数;
parent
4f028bc117
commit
bd1fc116ec
@ -0,0 +1,2 @@
|
||||
.idea/
|
||||
.DS_Store
|
@ -0,0 +1,16 @@
|
||||
package exegroup_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.blauwelle.com/go/crate/exegroup"
|
||||
"log"
|
||||
)
|
||||
|
||||
func Example_defaultGroup() {
|
||||
g := exegroup.Default()
|
||||
g.New().WithName("do nothing").WithGo(func(ctx context.Context) error {
|
||||
<-ctx.Done()
|
||||
return ctx.Err()
|
||||
})
|
||||
log.Println("exit:", g.Run(context.Background()))
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package exegroup
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
// HttpListenAndServe 提供 [http.Server] 的启动和停止函数;
|
||||
func HttpListenAndServe(port int, handler http.Handler) (func(ctx context.Context) error, func(ctx context.Context)) {
|
||||
server := &http.Server{Addr: fmt.Sprintf(":%d", port), Handler: handler}
|
||||
inShutdown := &atomic.Bool{}
|
||||
c := make(chan error, 1)
|
||||
goFunc := func(_ context.Context) error {
|
||||
err := server.ListenAndServe()
|
||||
if inShutdown.Load() {
|
||||
err = <-c
|
||||
}
|
||||
return err
|
||||
}
|
||||
stopFunc := func(ctx context.Context) {
|
||||
inShutdown.Store(true)
|
||||
c <- server.Shutdown(ctx)
|
||||
}
|
||||
return goFunc, stopFunc
|
||||
}
|
Loading…
Reference in New Issue