Merge pull request #122 from hasLeland/shutdown-hook
Add hook for controlling shutdown in truss
This commit is contained in:
Коммит
6a5285aec7
|
@ -70,6 +70,11 @@ func generateResponseFile(templFP string, data *gengokit.Data, prevFile io.Reade
|
|||
if genCode, err = h.Render(templFP, data); err != nil {
|
||||
return nil, errors.Wrapf(err, "cannot render template: %s", templFP)
|
||||
}
|
||||
case handler.HookPath:
|
||||
hook := handler.NewHook(prevFile)
|
||||
if genCode, err = hook.Render(templFP, data); err != nil {
|
||||
return nil, errors.Wrapf(err, "cannot render template: %s", templFP)
|
||||
}
|
||||
case middlewares.EndpointsPath:
|
||||
m := middlewares.New()
|
||||
m.LoadEndpoints(prevFile)
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
package handler
|
||||
|
||||
import (
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"github.com/TuneLab/go-truss/gengokit"
|
||||
)
|
||||
|
||||
const HookPath = "NAME-service/handlers/server/hooks.gotemplate"
|
||||
|
||||
// NewHook returns a new HookRender
|
||||
func NewHook(prev io.Reader) gengokit.Renderable {
|
||||
return &HookRender{
|
||||
prev: prev,
|
||||
}
|
||||
}
|
||||
|
||||
type HookRender struct {
|
||||
prev io.Reader
|
||||
}
|
||||
|
||||
// Render will return the existing file if it exists, otherwise it will return
|
||||
// a brand new copy from the template.
|
||||
func (h *HookRender) Render(_ string, _ *gengokit.Data) (io.Reader, error) {
|
||||
if h.prev == nil {
|
||||
return strings.NewReader(hookTempl), nil
|
||||
} else {
|
||||
return h.prev, nil
|
||||
}
|
||||
}
|
|
@ -48,3 +48,23 @@ type {{.PackageName}}Service struct{}
|
|||
{{end}}
|
||||
{{- end}}
|
||||
`
|
||||
const hookTempl = `
|
||||
package handler
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func InterruptHandler(errc chan<- error) {
|
||||
c := make(chan os.Signal, 1)
|
||||
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
|
||||
terminateError := fmt.Errorf("%s", <-c)
|
||||
|
||||
// Place whatever shutdown handling you want here
|
||||
|
||||
errc <- terminateError
|
||||
}
|
||||
`
|
||||
|
|
|
@ -7,8 +7,6 @@ import (
|
|||
"net/http"
|
||||
"net/http/pprof"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
// 3d Party
|
||||
"golang.org/x/net/context"
|
||||
|
@ -83,11 +81,7 @@ func main() {
|
|||
ctx := context.Background()
|
||||
|
||||
// Interrupt handler.
|
||||
go func() {
|
||||
c := make(chan os.Signal, 1)
|
||||
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
|
||||
errc <- fmt.Errorf("%s", <-c)
|
||||
}()
|
||||
go handler.InterruptHandler(errc)
|
||||
|
||||
// Debug listener.
|
||||
go func() {
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
{{/* See go-truss/gengokit/handler/templates.go for code */}}
|
Различия файлов скрыты, потому что одна или несколько строк слишком длинны
Загрузка…
Ссылка в новой задаче