2
0

main.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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/admin-api/graph/product/generated"
  9. "gshopper.com/gshopify/admin-product/graphql"
  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. h := handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{
  37. Resolvers: resolver,
  38. }))
  39. srv := server.NewServer(h, opts)
  40. go func() {
  41. if err := srv.ListenAndServe(); err != nil {
  42. log.Println(err)
  43. }
  44. }()
  45. signal.Notify(signals, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
  46. <-signals
  47. ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
  48. defer cancel()
  49. _ = srv.Shutdown(ctx)
  50. log.Println("shutting down")
  51. os.Exit(0)
  52. }