-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
63 lines (52 loc) · 1.33 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 (
"fmt"
"log"
"os"
"strings"
"time"
"github.com/golang-migrate/migrate/v4"
_ "github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
"github.com/joho/godotenv"
_ "github.com/lib/pq"
"github.com/smart7even/golang-do/internal/app"
)
func init() {
if os.Getenv("ENV") != "production" {
err := godotenv.Load()
if err != nil {
log.Printf("Can't load .env file")
}
}
}
func main() {
dbConnectionString := os.Getenv("DB_CONNECTION_STRING")
httpAddress := os.Getenv("HTTP_ADRESS")
grpcAdress := os.Getenv("GRPC_ADRESS")
secret := os.Getenv("SECRET")
if secret == "" {
log.Fatal("SECRET is not set")
}
time.Sleep(time.Second * 2)
m, err := migrate.New(
"file://migrations",
fmt.Sprintf("postgres://%s", dbConnectionString))
if err != nil {
log.Fatalf("Unable to prepare migrations: %v", err)
}
log.Printf("Trying to apply migrations")
err = m.Up()
if err != nil {
if strings.Contains(err.Error(), "connect: connection refused") {
log.Fatalf("Connection refused: %v", err)
} else if err == migrate.ErrNoChange {
log.Printf("Database already up-to-date")
} else {
log.Fatalf("Unable to make migrations: %v", err)
}
} else {
log.Printf("Migrations to db applied")
}
app.Run(dbConnectionString, httpAddress, grpcAdress, secret)
}