-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
81 lines (69 loc) · 2.54 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"net/http"
"os"
"github.com/dileepaj/tracified-gateway/api/routes"
"github.com/dileepaj/tracified-gateway/commons"
"github.com/dileepaj/tracified-gateway/configs"
"github.com/dileepaj/tracified-gateway/services"
notificationhandler "github.com/dileepaj/tracified-gateway/services/notificationHandler.go"
"github.com/dileepaj/tracified-gateway/services/rabbitmq"
"github.com/dileepaj/tracified-gateway/utilities"
"github.com/go-openapi/runtime/middleware"
"github.com/gorilla/handlers"
"github.com/robfig/cron/v3"
)
func getPort() string {
p := os.Getenv("GATEWAY_PORT")
if p != "" {
return ":" + p
}
return ":8000"
}
func main() {
// godotenv package
envName := commons.GoDotEnvVariable("ENVIRONMENT")
// getEnvironment()
port := getPort()
headersOk := handlers.AllowedHeaders([]string{"X-Requested-With", "Content-Type", "Authorization", "Token"})
originsOk := handlers.AllowedOrigins([]string{"*"})
methodsOk := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "OPTIONS"})
// Register services, this will be used to schedule workers
// Added here to avoid circular import, refactor if there is a better way
configs.QueueBackLinks.Method = services.SubmitBacklinksDataToStellar
configs.QueueTransaction.Method = services.SubmitUserDataToStellar
// Initialize the cron scheduler with Delay a job's execution if the previous run hasn't completed yet
c := cron.New(
cron.WithChain(
cron.SkipIfStillRunning(cron.DefaultLogger),
cron.Recover(cron.DefaultLogger),
),
)
c.AddFunc("@every 30m", func() {
notificationhandler.CheckStellarAccountBalance(commons.GoDotEnvVariable("NFTSTELLARISSUERPUBLICKEYK"))
notificationhandler.CheckStellarAccountBalance(commons.GoDotEnvVariable("SPONSORERPK"))
})
c.AddFunc("@every 12h", func() {
services.CheckTestimonialStatus()
services.CheckOrganizationStatus()
})
c.AddFunc("@every 30s", func() {
services.QueueScheduleWorkers()
})
c.Start()
router := routes.NewRouter()
// rabbit mq server
go rabbitmq.ReceiverRmq()
go rabbitmq.ReleaseLock()
// serve swagger documentation
opts := middleware.SwaggerUIOpts{SpecURL: "/swagger.yaml"}
sh := middleware.SwaggerUI(opts, nil)
router.Handle("/docs", sh)
router.Handle("/swagger.yaml", http.FileServer(http.Dir("./")))
// initial log file when server starts
utilities.CreateLogFile()
// create logger
logger := utilities.NewCustomLogger()
logger.LogWriter("Gateway Started @port "+port+" with "+envName+" environment", 1)
http.ListenAndServe(port, handlers.CORS(originsOk, headersOk, methodsOk)(router))
}