-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·62 lines (53 loc) · 1.63 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
package main
import (
"database/sql"
"fmt"
"os"
"strconv"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/rds/rdsutils"
_ "github.com/lib/pq"
)
func getenv(key, fallback string) string {
value := os.Getenv(key)
if len(value) == 0 {
return fallback
}
return value
}
func main() {
var dbName string = getenv("DB", "empty")
var dbUser string = getenv("USER", "empty")
var dbHost string = getenv("HOST", "empty")
var port string = getenv("PORT", "5432")
var dbPort,_ = strconv.Atoi(port)
var region string = getenv("REGION", "eu-central-1")
var dbEndpoint string = fmt.Sprintf("%s:%d", dbHost, dbPort)
opts := session.Options{Config: aws.Config{
CredentialsChainVerboseErrors: aws.Bool(true),
Region: aws.String(region),
MaxRetries: aws.Int(3),
}}
sess := session.Must(session.NewSessionWithOptions(opts))
authenticationToken, err := rdsutils.BuildAuthToken(dbEndpoint, region, dbUser, sess.Config.Credentials)
if err != nil {
panic("failed to create authentication token: " + err.Error())
}
dsn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s",
dbHost, dbPort, dbUser, authenticationToken, dbName,
)
fmt.Println("DEBUG: ", dsn)
db, err := sql.Open("postgres", dsn)
if err != nil {
panic(err)
} else {
fmt.Println("Connected")
}
err = db.Ping()
if err != nil {
panic(err)
} else {
fmt.Println("Ping response")
}
}