-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
117 lines (96 loc) · 2.89 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
// @APIVersion 1.0.0
// @APITitle STORE API
// @APIDescription API for STORE app.
// @BasePath http://host:port/api
package main
import (
"encoding/json"
"os"
"strconv"
"github.com/qor/admin"
"github.com/qor/location"
"github.com/qor/media"
"github.com/storebeacon/backend/paola/auth"
"github.com/storebeacon/backend/paola/database"
"github.com/storebeacon/backend/paola/jsonconfig"
"github.com/storebeacon/backend/paola/server"
adminapp "github.com/storebeacon/backend/app/admin"
"github.com/storebeacon/backend/app/application"
"github.com/storebeacon/backend/app/static"
"github.com/storebeacon/backend/models/migrations"
"github.com/storebeacon/backend/models/admins"
"github.com/storebeacon/backend/app/store"
"github.com/storebeacon/backend/app/beacon"
"github.com/storebeacon/backend/app/aisle"
"github.com/storebeacon/backend/app/product"
)
var (
config = &Configuration{}
)
type Configuration struct {
Server server.Server `json:"server"`
Database database.DatabaseConfig `json:"database"`
AWS product.AWSConfig `json:"aws"`
}
// ParseJSON unmarshals bytes to structs
func (c *Configuration) ParseJSON(b []byte) error {
return json.Unmarshal(b, &c)
}
// LoadComponents parses the JSON file and sets up components
func LoadComponents() *Configuration {
configPath := "config.json"
jsonconfig.Load(configPath, config)
database.Configure(config.Database)
// Configure database rds or local test
host := os.Getenv("RDS_HOSTNAME")
port := os.Getenv("RDS_PORT")
username := os.Getenv("RDS_USERNAME")
password := os.Getenv("RDS_PASSWORD")
db := os.Getenv("RDS_DB_NAME")
if host != "" {
configDatabase := database.DatabaseConfig{}
portInt, _ := strconv.Atoi(port)
configDatabase.Host = host
configDatabase.Port = portInt
configDatabase.User = username
configDatabase.Password = password
configDatabase.Database = db
database.Configure(configDatabase)
}
database.Connect()
media.RegisterCallbacks(database.Conn)
// Update the database
migrations.Migrate(database.Conn)
location.GoogleAPIKey = os.Getenv("GOOGLE_API_KEY")
return config
}
func main() {
LoadComponents()
Admin := admin.New(&admin.AdminConfig{
SiteName: "STORE API",
DB: database.Conn,
Auth: auth.AdminAuth{},
})
app := application.New(&application.Config{
Admin: Admin,
Auth: auth.NewAuth(),
})
// check if there are admins, if not create one
adminUsers, _ := admins.GetAdminUsers()
if len(adminUsers) == 0 {
adm := admins.Admin{
Name: "Admin",
Email: "[email protected]",
Password: "admin123!",
Role: "Admin",
}
database.Conn.Create(&adm)
}
app.Use(adminapp.New(&adminapp.Config{}))
app.Use(static.New(&static.Config{}))
app.Use(store.New(&store.Config{}))
app.Use(aisle.New(&aisle.Config{}))
app.Use(beacon.New(&beacon.Config{}))
app.Use(product.New(&product.Config{AwsConfig:config.AWS}))
server.Run(app.GetMux(), config.Server)
}