-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
92 lines (69 loc) · 1.9 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
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"fmt"
"log"
"os"
"github.com/SanjaySinghRajpoot/realNotification/config"
"github.com/SanjaySinghRajpoot/realNotification/middleware"
"github.com/SanjaySinghRajpoot/realNotification/routes"
"github.com/SanjaySinghRajpoot/realNotification/utils"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
"gopkg.in/robfig/cron.v2"
)
// @title Real Notification Service
// @version 1.0
// @description A Notification Service in Go using Gin framework
// @host localhost:8081
// @BasePath /
func main() {
// Connect to the database
config.Connect()
// start the CRON JOB
CRONjob()
// Gin router
r := gin.Default()
// Prometheus middleware
p := middleware.NewPrometheus("gin")
p.Use(r)
// Home Page endpoint
r.GET("/", utils.HomepageHandler)
password := "12345678"
// Redis Cache Setup
utils.RedisClient = utils.SetUpRedis(password)
var err error
utils.KafkaProducer, err = utils.InitializeProducer()
if err != nil {
fmt.Printf("Failed to create producer: %s\n", err.Error())
return
}
// adding rate limiter for all the routes
// r.Use(middleware.RateLimiter)
// User Router for Notification Route
routes.UserRoute(r)
// setting up the SWAAGGER URL
url := ginSwagger.URL("http://localhost:8081/swagger/doc.json")
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, url))
r.Run(":8081")
}
func CRONjob() {
// start the cron job
cronJob := cron.New()
// Cron Job that will check for state=false for Notifications
cronJob.AddFunc("@every 10s", func() {
utils.CheckForNotificationState()
})
cronJob.Start()
}
// use godot package to load/read the .env file and
// return the value of the key
func EnvVariable(key string) string {
// load .env file
err := godotenv.Load(".env")
if err != nil {
log.Fatalf("Error loading .env file")
}
return os.Getenv(key)
}