2
0

main.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package main
  2. import (
  3. "context"
  4. "github.com/99designs/gqlgen/graphql/handler"
  5. "github.com/gshopify/service-wrapper/config"
  6. "github.com/gshopify/service-wrapper/server"
  7. "github.com/spf13/pflag"
  8. "gshopper.com/gshopify/shop/graphql"
  9. "gshopper.com/gshopify/shop/graphql/generated"
  10. "log"
  11. "os"
  12. "os/signal"
  13. "syscall"
  14. "time"
  15. )
  16. var (
  17. signals = make(chan os.Signal, 1)
  18. fPort = pflag.IntP("port", "p", 80, "exposing port")
  19. fTimeout = pflag.DurationP("timeout", "t", 15*time.Second, "timeout duration")
  20. fDebug = pflag.Bool("debug", false, "debug mode")
  21. )
  22. func init() {
  23. pflag.Parse()
  24. config.Instance()
  25. config.PrintBanner()
  26. }
  27. func main() {
  28. opts, err := server.NewDefaultOpts(*fPort, *fTimeout)
  29. if err != nil {
  30. panic(err)
  31. }
  32. resolver, err := graphql.New(context.Background(), *fDebug)
  33. if err != nil {
  34. panic(err)
  35. }
  36. srv := server.NewServer(handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{
  37. Resolvers: resolver,
  38. })), opts)
  39. go func() {
  40. if err := srv.ListenAndServe(); err != nil {
  41. log.Println(err)
  42. }
  43. }()
  44. signal.Notify(signals, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
  45. <-signals
  46. ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
  47. defer cancel()
  48. _ = srv.Shutdown(ctx)
  49. log.Println("shutting down")
  50. os.Exit(0)
  51. }