|
| 1 | +package runner |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "delta-eats/graph/generated" |
| 6 | + "delta-eats/internal/config" |
| 7 | + "delta-eats/internal/middleware" |
| 8 | + "delta-eats/internal/setup" |
| 9 | + "encoding/json" |
| 10 | + "log" |
| 11 | + "net/http" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/99designs/gqlgen/graphql/handler" |
| 15 | + "github.com/99designs/gqlgen/graphql/playground" |
| 16 | + "github.com/go-co-op/gocron" |
| 17 | + "github.com/gorilla/mux" |
| 18 | + "github.com/nextap-solutions/goNextService" |
| 19 | + "github.com/nextap-solutions/goNextService/components" |
| 20 | + "github.com/rs/cors" |
| 21 | + "go.uber.org/zap" |
| 22 | +) |
| 23 | + |
| 24 | +type MenurooServerComponents struct { |
| 25 | + httpServer goNextService.Component |
| 26 | + cron goNextService.Component |
| 27 | + cleanup goNextService.Component |
| 28 | +} |
| 29 | + |
| 30 | +func Serve() error { |
| 31 | + configuration := config.LoadConfig() |
| 32 | + menurooApi, err := setupService(configuration) |
| 33 | + if err != nil { |
| 34 | + return err |
| 35 | + } |
| 36 | + app := goNextService.NewApplications(menurooApi.httpServer) |
| 37 | + return app.Run() |
| 38 | +} |
| 39 | + |
| 40 | +func setupService(configuration *config.Config) (*MenurooServerComponents, error) { |
| 41 | + logger := setup.InitLogger(*configuration) |
| 42 | + s, _ := json.MarshalIndent(configuration, "", "\t") |
| 43 | + zap.L().Info("Logger initialized successfully") |
| 44 | + logger.Info(string(s)) |
| 45 | + dbConn, err := setup.SetupDb(configuration) |
| 46 | + if err != nil { |
| 47 | + log.Println("Error while connecting to database") |
| 48 | + return nil, err |
| 49 | + } |
| 50 | + |
| 51 | + resolver, err := setup.NewResolver(dbConn, *configuration) |
| 52 | + if err != nil { |
| 53 | + return nil, err |
| 54 | + } |
| 55 | + srv := handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{ |
| 56 | + Resolvers: resolver, |
| 57 | + })) |
| 58 | + |
| 59 | + router := mux.NewRouter() |
| 60 | + router.Use(middleware.Authentication(resolver.AuthUsecase)) |
| 61 | + router.Handle("/", playground.Handler("GraphQL playground", "/query")) |
| 62 | + router.Handle("/query", srv) |
| 63 | + corsMiddleware := cors.New(cors.Options{ |
| 64 | + AllowedOrigins: []string{"*"}, |
| 65 | + AllowedHeaders: []string{"*"}, |
| 66 | + AllowCredentials: true, |
| 67 | + }) |
| 68 | + |
| 69 | + handler := corsMiddleware.Handler(router) |
| 70 | + api := http.Server{ |
| 71 | + Addr: "0.0.0.0:" + configuration.Server.Port, |
| 72 | + ReadTimeout: configuration.Server.ReadTimeout, |
| 73 | + WriteTimeout: configuration.Server.WriteTimeout, |
| 74 | + Handler: handler, |
| 75 | + } |
| 76 | + httpComponent := components.NewHttpComponent(handler, components.WithHttpServer(&api)) |
| 77 | + |
| 78 | + scheduler := gocron.NewScheduler(time.UTC) |
| 79 | + _, err = scheduler.Cron(configuration.FioConfig.RefreshJob). |
| 80 | + Tag("BankRefresh"). |
| 81 | + SingletonMode(). |
| 82 | + Do(resolver.MailUsecase.ParseAndStorePayments) |
| 83 | + if err != nil { |
| 84 | + return nil, err |
| 85 | + } |
| 86 | + |
| 87 | + return &MenurooServerComponents{ |
| 88 | + httpServer: httpComponent, |
| 89 | + cron: components.NewCronComponent(scheduler), |
| 90 | + cleanup: components.NewLifecycleComponent(nil, func(ctx context.Context) error { |
| 91 | + select {} |
| 92 | + }, []components.LifeCycleFunc{ |
| 93 | + func(ctx context.Context) error { |
| 94 | + return nil |
| 95 | + }, |
| 96 | + }), |
| 97 | + }, nil |
| 98 | +} |
0 commit comments