-
Notifications
You must be signed in to change notification settings - Fork 138
/
main.go
42 lines (35 loc) · 898 Bytes
/
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
package main
import (
"database/sql"
"fmt"
"net/http"
"os"
"github.com/circleci/cci-demo-docker/service"
_ "github.com/mattes/migrate/driver/postgres"
"github.com/mattes/migrate/migrate"
)
func main() {
db := SetupDB()
server := service.NewServer(db)
http.HandleFunc("/", server.ServeHTTP)
http.ListenAndServe(":8080", nil)
}
func SetupDB() *service.Database {
databaseUrl := os.Getenv("CONTACTS_DB_URL")
if databaseUrl == "" {
panic("CONTACTS_DB_URL must be set!")
}
sqlFiles := "./db/migrations"
if sqlFilesEnv := os.Getenv("CONTACTS_DB_MIGRATIONS"); sqlFilesEnv != "" {
sqlFiles = sqlFilesEnv
}
allErrors, ok := migrate.ResetSync(databaseUrl, sqlFiles)
if !ok {
panic(fmt.Sprintf("%+v", allErrors))
}
db, err := sql.Open("postgres", databaseUrl)
if err != nil {
panic(fmt.Sprintf("Unable to open DB connection: %+v", err))
}
return &service.Database{db}
}