-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
84 lines (68 loc) · 1.32 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
package main
import (
"log"
"time"
"github.com/go-co-op/gocron/v2"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/fiber/v2/middleware/recover"
"github.com/gofiber/template/html/v2"
"github.com/joho/godotenv"
)
func main() {
err := loadEnv()
if err != nil {
log.Fatal("Error loading .env file")
}
err = Init()
if err != nil {
log.Fatal("Error initializing database: ", err)
}
err = startScheduler()
if err != nil {
log.Fatal("Error starting scheduler: ", err)
}
app := startServer()
err = app.Listen(":3000")
if err != nil {
log.Fatal("Error starting server: ", err)
}
}
func loadEnv() error {
err := godotenv.Load()
if err != nil {
return err
}
return nil
}
func startScheduler() error {
s, err := gocron.NewScheduler()
if err != nil {
return err
}
j, err := s.NewJob(
gocron.DurationJob(10*time.Second),
gocron.NewTask(func() {
status, err := sendInvoices()
if err != nil {
log.Println("Error sending invoices: ", err)
}
log.Println(status)
}),
)
if err != nil {
return err
}
log.Println("Job ID: ", j.ID())
s.Start()
return nil
}
func startServer() *fiber.App {
app := fiber.New(fiber.Config{
Views: html.New("./views", ".html"),
})
app.Use(recover.New())
app.Use(logger.New())
setRoutes(app)
return app
}