-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
81 lines (64 loc) · 1.63 KB
/
main.go
File metadata and controls
81 lines (64 loc) · 1.63 KB
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
package main
import(
"net/http"
"github.com/saboel/Rate-Fuel-App/handlers"
//"github.com/saboel/Rate-Fuel-App/clients"
"github.com/saboel/Rate-Fuel-App/db"
_ "github.com/jackc/pgx/v4/stdlib"
"github.com/golang-migrate/migrate/v4/source/iofs"
"github.com/golang-migrate/migrate/v4/database/postgres"
"github.com/golang-migrate/migrate/v4"
"database/sql"
"os"
"log"
"embed"
)
// start of our server
func main() {
// For example: POSTGRES_URL="postgres://postgres:mysecretpassword@localhost:5432/postgres"
database, err := sql.Open("pgx", os.Getenv("POSTGRES_URL"))
if err != nil {
log.Fatal("oops, db connection failed", err)
}
err = validateSchema(database)
if err != nil {
log.Fatal("oops, db migration failed", err)
}
h := &handlers.Handlers{
DB: db.New(database),
}
http.Handle("/", h)
log.Println("Serving on localhost:8080")
err = http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal("oops, server failed to start", err)
}
}
//go:embed db/migrations/*.sql
var fs embed.FS
// Migrate migrates the Postgres schema to the current version.
func validateSchema(db *sql.DB) (retErr error) {
sourceInstance, err := iofs.New(fs, "db/migrations")
if err != nil {
return err
}
defer func() {
err := sourceInstance.Close()
if retErr == nil {
retErr = err
}
}()
driverInstance, err := postgres.WithInstance(db, new(postgres.Config))
if err != nil {
return err
}
m, err := migrate.NewWithInstance("iofs", sourceInstance, "postgres", driverInstance)
if err != nil {
return err
}
err = m.Up() // current version
if err != nil && err != migrate.ErrNoChange {
return err
}
return nil
}