2
0

server.go 963 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package main
  2. import (
  3. "context"
  4. "github.com/99designs/gqlgen/graphql/handler"
  5. "github.com/gshopify/service-wrapper/server"
  6. "gshopper.com/gshopify/customer/graphql"
  7. "gshopper.com/gshopify/customer/graphql/generated"
  8. "log"
  9. "os"
  10. "os/signal"
  11. "syscall"
  12. "time"
  13. )
  14. var (
  15. signals = make(chan os.Signal, 1)
  16. )
  17. func serve() error {
  18. opts, err := server.NewDefaultOpts(*fPort, *fTimeout)
  19. if err != nil {
  20. return err
  21. }
  22. resolver, err := graphql.NewResolver()
  23. if err != nil {
  24. return err
  25. }
  26. srv := server.NewServer(
  27. handler.NewDefaultServer(
  28. generated.NewExecutableSchema(generated.Config{Resolvers: resolver})),
  29. opts)
  30. go func() {
  31. if err := srv.ListenAndServe(); err != nil {
  32. log.Println(err)
  33. }
  34. }()
  35. signal.Notify(signals, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
  36. <-signals
  37. ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
  38. defer cancel()
  39. _ = srv.Shutdown(ctx)
  40. log.Println("shutting down")
  41. return nil
  42. }