-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
71 lines (59 loc) · 1.74 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
package main
import (
"context"
"database/sql"
"fmt"
"log"
"strings"
"com.thebeachmaster/entexample/config"
"com.thebeachmaster/entexample/drivers"
"com.thebeachmaster/entexample/ent"
"com.thebeachmaster/entexample/server"
"entgo.io/ent/dialect"
entsql "entgo.io/ent/dialect/sql"
_ "github.com/jackc/pgx/v4/stdlib"
)
func openDb(databaseUrl string) (*ent.Client, error) {
db, err := sql.Open("pgx", databaseUrl)
if err != nil {
return nil, err
}
drv := entsql.OpenDB(dialect.Postgres, db)
return ent.NewClient(ent.Driver(drv)), nil
}
func main() {
log.Println("Starting Server...")
appConfigPath := "./config/config-local"
cfgFile, err := config.LoadConfig(appConfigPath)
if err != nil {
log.Fatalf("LoadConfig Error: %v", err)
}
cfg, err := config.ParseConfig(cfgFile)
if err != nil {
log.Fatalf("ParseConfig Error: %v", err)
}
// postgresql://user:password@host/database
// TODO: Handle Heroku DB Conn String
connStringBuilder := strings.Builder{}
connStringBuilder.WriteString(fmt.Sprintf("postgresql://%s:%s@%s/%s", cfg.DB.DBUser, cfg.DB.DBPass, cfg.DB.DBHost, cfg.DB.DBName))
dbConnString := connStringBuilder.String()
client, err := openDb(dbConnString)
if err != nil {
log.Fatalf("failed opening connection to postgres: %v", err)
}
defer client.Close()
// Run the auto migration tool.
// TODO: maybe add cli flag
if err := client.Schema.Create(context.Background()); err != nil {
log.Fatalf("failed creating schema resources: %v", err)
}
redisClient, err := drivers.NewRedisDBClient(cfg)
if err != nil {
log.Printf("failed to connect to redis due to: %v\n", err)
}
defer redisClient.Close()
server := server.NewServer(cfg, client, redisClient)
if err = server.Run(); err != nil {
log.Fatal(err)
}
}