-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
119 lines (104 loc) · 2.48 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"encoding/json"
"fmt"
"os"
"github.com/gin-contrib/pprof"
"github.com/google/gops/agent"
"github.com/ossan-dev/coworkingapp/handlers"
"github.com/ossan-dev/coworkingapp/middlewares"
"github.com/ossan-dev/coworkingapp/models"
"github.com/ossan-dev/coworkingapp/utils"
"github.com/gin-gonic/gin"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
func main() {
// gops agent
if err := agent.Listen(agent.Options{}); err != nil {
panic(err)
}
// flight recorder
fr := handlers.NewFlightRecorder()
if err := fr.FlightRecorderTracer.Start(); err != nil {
panic(err)
}
fr.FlightRecorderTracer.SetSize(4096)
defer func() {
if err := fr.FlightRecorderTracer.Stop(); err != nil {
panic(err)
}
}()
// app Main code
var config models.CoworkingConfig
data, err := os.ReadFile("config/config.json")
if err != nil {
panic(err)
}
if err := json.Unmarshal(data, &config); err != nil {
panic(err)
}
if len(config.SecretKey) != 32 {
panic(fmt.Errorf("config.SecretKey must have 32 bytes"))
}
pgConfig := postgres.Config{
DSN: config.Dsn,
}
db, err := gorm.Open(postgres.Dialector{
Config: &pgConfig,
})
if err != nil {
panic(err)
}
user := models.User{}
room := models.Room{}
photo := models.Photo{}
booking := models.Booking{}
utils.PrintMemStats()
err = db.AutoMigrate(&user, &room, &photo, &booking)
if err != nil {
panic(err)
}
utils.PrintMemStats()
seedData(db)
r := gin.Default()
r.Use(middlewares.EarlyExitOnPreflightRequests())
r.Use(middlewares.SetCorsPolicy(config.AllowedOrigins))
r.Use(middlewares.SetRequestValues(*db, config))
handlers.SetupRoutes(r, fr)
pprof.Register(r)
if err := r.Run(":8080"); err != nil {
panic(err)
}
}
func seedData(db *gorm.DB) {
sqldb, err := db.DB()
if err != nil {
panic(err)
}
_, err = sqldb.Exec(`DELETE FROM public.bookings`)
if err != nil {
panic(err)
}
_, err = sqldb.Exec(`DELETE FROM public.users`)
if err != nil {
panic(err)
}
_, err = sqldb.Exec(`DELETE FROM public.photos`)
if err != nil {
panic(err)
}
_, err = sqldb.Exec(`DELETE FROM public.rooms`)
if err != nil {
panic(err)
}
user := models.User{}
if err := models.ParseModelWithUnmarshal(&user, "user.json"); err != nil {
panic(err)
}
db.Create(&user)
rooms := make([]models.Room, 0, 3)
if err := models.ParseModelWithUnmarshal(&rooms, "rooms.json"); err != nil {
}
db.Create(&rooms)
}