-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
63 lines (50 loc) · 1.19 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
package main
import (
"log"
"github.com/carantes/superheroes-api/bundles/superheroesbundle"
"github.com/carantes/superheroes-api/core"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
"github.com/joho/godotenv"
)
func main() {
// Load envs
cfg := loadConfig()
// Init DB
db, err := initDB(cfg.DBType, cfg.DBConnection)
if err != nil {
log.Fatal(err)
}
defer db.Close()
// Init Bundles
bundles := initBundles(db)
// Configure Server with prefix and routes
srv := core.NewServer(bundles, core.ServerOpts{APIPrefix: cfg.APIPrefix})
// Start
log.Fatal(srv.Start(cfg.APIAddr))
}
func initBundles(db *gorm.DB) []core.Bundle {
log.Println("Loading bundles")
return []core.Bundle{
superheroesbundle.NewSuperheroesBundle(db),
}
}
func loadConfig() *core.Config {
log.Println("Loading configs from .env")
err := godotenv.Load()
if err != nil {
log.Fatal(".env file not found")
}
return core.GetConfig()
}
func initDB(dbType string, dbConn string) (*gorm.DB, error) {
db, err := gorm.Open(dbType, dbConn)
if err != nil {
return &gorm.DB{}, err
}
db.AutoMigrate(
&superheroesbundle.Superhero{},
&superheroesbundle.SuperheroGroup{},
)
return db, nil
}