-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
93 lines (76 loc) · 1.94 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
93
package main
import (
"flag"
"log"
"net/http"
"github.com/murtll/mcserver-pay/pkg/config"
"github.com/murtll/mcserver-pay/pkg/entities"
"github.com/murtll/mcserver-pay/pkg/repository"
"github.com/murtll/mcserver-pay/pkg/routes"
"github.com/murtll/mcserver-pay/pkg/service"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/render"
amqp "github.com/rabbitmq/amqp091-go"
)
var migrate = flag.Bool("migrate", false, "Run migrations.")
func main() {
// init db
db, err := gorm.Open(postgres.Open(config.PostgresString))
if err != nil {
panic(err)
}
flag.Parse()
// init repositories
dr := repository.NewDonateRepository(db)
if *migrate {
err := dr.Migrate()
if err != nil {
panic(err)
}
}
ir, err := repository.NewItemRepository(config.ApiUrl)
if err != nil {
panic(err)
}
// open amqp connection
conn, err := amqp.Dial(config.AmqpUrl)
if err != nil {
panic(err)
}
defer conn.Close()
ch, err := conn.Channel()
if err != nil {
panic(err)
}
defer ch.Close()
mr, err := repository.NewMessageRepository(ch, config.AmqpQueueName)
if err != nil {
panic(err)
}
// init services
ds := service.NewDonateService(dr, ir, mr)
// init main chi router
router := chi.NewRouter()
router.Use(middleware.RequestID)
router.Use(middleware.Logger)
router.Use(middleware.Recoverer)
router.Use(middleware.URLFormat)
router.Use(render.SetContentType(render.ContentTypeJSON))
router.Get(config.HealthPath, func(w http.ResponseWriter, r *http.Request) {
render.JSON(w, r, entities.StatusResponse{
Status: "ok",
Version: config.Version,
})
})
// mount routers
router.Mount("/process", routes.NewPaymentRouter(ds))
router.Mount("/donates", routes.NewDonateRouter(ds))
log.Default().Printf("Starting server v%s on %s", config.Version, config.ListenAddr)
err = http.ListenAndServe(config.ListenAddr, router)
if err != nil {
panic(err)
}
}