-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
144 lines (122 loc) · 3.25 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"fmt"
"log"
"os"
"strings"
"text/template"
"github.com/gin-gonic/gin"
"github.com/ostafen/clover/v2"
"github.com/ostafen/clover/v2/query"
uuid "github.com/satori/go.uuid"
"github.com/ClubCedille/hackqc2024/pkg/account"
"github.com/ClubCedille/hackqc2024/pkg/data_import"
"github.com/ClubCedille/hackqc2024/pkg/database"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
)
const (
GIN_SESSION_NAME = "gin-session"
GIN_SESSION_SECRET = "gin-session-secret"
TMP_DIR = "tmp"
)
type Request struct {
IP string
DateTime string
}
func main() {
// Init database
db, err := database.InitDatabase()
if err != nil {
log.Fatalf(err.Error())
}
os.Mkdir("tmp", 0777)
// Initial load
err = data_import.UpdateAll(db)
if err != nil {
log.Fatalf(err.Error())
}
// generateSeedData(db)
r := gin.Default()
r.SetFuncMap(template.FuncMap{
"escapeSingleQuotes": func(s string) string {
return strings.ReplaceAll(s, "'", "\\'")
},
})
store := cookie.NewStore([]byte(GIN_SESSION_SECRET))
store.Options(sessions.Options{MaxAge: 60 * 60 * 24}) // expire in a day
r.Use(sessions.Sessions(GIN_SESSION_NAME, store))
r.Use(LoginMiddleware())
r.LoadHTMLGlob("templates/**/*.html")
registerRoutes(r, db)
err = r.Run()
if err != nil {
fmt.Print("Failed to run")
return
}
defer func(db *clover.DB) {
err := db.Close()
if err != nil {
fmt.Print("Failed to close CloverDB during program exit!")
return
}
}(db)
}
func generateSeedData(db *clover.DB) {
// Create an account
err := account.CreateAccount(db, account.Account{
Id: uuid.NewV4().String(),
UserName: "sonoflope",
FirstName: "son",
LastName: "oflope",
Email: "[email protected]",
})
if err != nil {
log.Fatalf(err.Error())
}
accountExists, err := account.AccountExistsById(db, data_import.SYSTEM_USER_GUID)
if err != nil {
log.Fatalf(err.Error())
}
if !accountExists {
err = account.CreateAccount(db, account.Account{
Id: data_import.SYSTEM_USER_GUID,
UserName: "system",
FirstName: "system",
LastName: "system",
Email: "[email protected]",
})
if err != nil {
log.Fatalf(err.Error())
}
}
// Fetch the new account id
docs, err := db.FindFirst(query.NewQuery(database.AccountCollection).Where(query.Field("user_name").Eq("sonoflope")))
if err != nil {
log.Fatalf(err.Error())
}
acc := account.Account{}
docs.Unmarshal(&acc)
// Create test help object
// testHelp, err := db.FindFirst(query.NewQuery(database.HelpCollection).Where(query.Field("map_object.name").Eq("Test help")))
// if err == nil && testHelp == nil {
// err = help.CreateHelp(db, help.Help{
// Id: uuid.NewV4().String(),
// MapObject: mapobject.MapObject{
// AccountId: acc.Id,
// Geometry: mapobject.Geometry{GeomType: "Point", Coordinates: []float64{-73.5673, 45.5017}},
// Name: "Test - Utilisez ma maison",
// Description: "J'ai deux chambres à coucher de libre",
// Category: "Hébergement",
// Tags: []string{"test", "help"},
// },
// ContactInfos: "555 444 3333",
// NeedHelp: true,
// HowToHelp: "N/A",
// HowToUseHelp: "Venez chez moi.",
// })
// }
// if err != nil {
// log.Fatalf(err.Error())
// }
}