-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
68 lines (59 loc) · 2.27 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
package main
import (
"github.com/joho/godotenv"
"log"
"mahresources/application_context"
"mahresources/constants"
"mahresources/models"
"mahresources/models/util"
"mahresources/server"
)
func main() {
// you may have no .env, it's okay
_ = godotenv.Load(".env")
context, db, mainFs := application_context.CreateContext()
if err := db.AutoMigrate(
&models.Query{},
&models.Resource{},
&models.Note{},
&models.Tag{},
&models.Group{},
&models.Category{},
&models.NoteType{},
&models.Preview{},
&models.GroupRelation{},
&models.GroupRelationType{},
&models.ImageHash{},
); err != nil {
log.Fatalf("failed to migrate: %v", err)
}
util.AddInitialData(db)
indexQueries := [...]string{
"CREATE INDEX IF NOT EXISTS idx__resource_notes__note_id ON resource_notes(note_id)",
"CREATE INDEX IF NOT EXISTS idx__resource_notes__resource_id ON resource_notes(resource_id)",
"CREATE INDEX IF NOT EXISTS idx__groups_related_resources__resource_id ON groups_related_resources(resource_id)",
"CREATE INDEX IF NOT EXISTS idx__groups_related_resources__resource_id___hash ON groups_related_resources USING HASH (resource_id);",
"CREATE INDEX IF NOT EXISTS idx__groups_related_resources__group_id ON groups_related_resources(group_id)",
}
indexQueriesSqlite := [...]string{
"CREATE INDEX IF NOT EXISTS idx__resource_notes__note_id ON resource_notes(note_id)",
"CREATE INDEX IF NOT EXISTS idx__resource_notes__resource_id ON resource_notes(resource_id)",
"CREATE INDEX IF NOT EXISTS idx__groups_related_resources__resource_id ON groups_related_resources(resource_id)",
"CREATE INDEX IF NOT EXISTS idx__groups_related_resources__resource_id___hash ON groups_related_resources(resource_id);",
"CREATE INDEX IF NOT EXISTS idx__groups_related_resources__group_id ON groups_related_resources(group_id)",
}
if context.Config.DbType == constants.DbTypePosgres {
for _, query := range indexQueries {
if err := db.Exec(query).Error; err != nil {
log.Fatalf("Error when creating index: %v", err)
}
}
} else {
for _, query := range indexQueriesSqlite {
if err := db.Exec(query).Error; err != nil {
log.Fatalf("Error when creating index: %v", err)
}
}
}
log.Fatal(server.CreateServer(context, mainFs, context.Config.AltFileSystems).ListenAndServe())
}