/
cloudsql.go
110 lines (90 loc) · 2.8 KB
/
cloudsql.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2018 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
// [START gae_cloudsql]
// Sample cloudsql demonstrates connection to a Cloud SQL instance from App Engine standard.
package main
import (
"bytes"
"database/sql"
"fmt"
"log"
"net/http"
"os"
// MySQL library, comment out to use PostgreSQL.
_ "github.com/go-sql-driver/mysql"
// PostgreSQL Library, uncomment to use.
// _ "github.com/lib/pq"
)
var db *sql.DB
func main() {
db = DB()
http.HandleFunc("/", indexHandler)
port := os.Getenv("PORT")
if port == "" {
port = "8080"
log.Printf("Defaulting to port %s", port)
}
log.Printf("Listening on port %s", port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
}
// DB gets a connection to the database.
// This can panic for malformed database connection strings, invalid credentials, or non-existance database instance.
func DB() *sql.DB {
var (
connectionName = mustGetenv("CLOUDSQL_CONNECTION_NAME")
user = mustGetenv("CLOUDSQL_USER")
password = os.Getenv("CLOUDSQL_PASSWORD") // NOTE: password may be empty
socket = os.Getenv("CLOUDSQL_SOCKET_PREFIX")
)
// /cloudsql is used on App Engine.
if socket == "" {
socket = "/cloudsql"
}
// MySQL Connection, comment out to use PostgreSQL.
// connection string format: USER:PASSWORD@unix(/cloudsql/)PROJECT_ID:REGION_ID:INSTANCE_ID/[DB_NAME]
dbURI := fmt.Sprintf("%s:%s@unix(%s/%s)/", user, password, socket, connectionName)
conn, err := sql.Open("mysql", dbURI)
// PostgreSQL Connection, uncomment to use.
// connection string format: user=USER password=PASSWORD host=/cloudsql/PROJECT_ID:REGION_ID:INSTANCE_ID/[ dbname=DB_NAME]
// dbURI := fmt.Sprintf("user=%s password=%s host=/cloudsql/%s dbname=%s", user, password, connectionName)
// conn, err := sql.Open("postgres", dbURI)
if err != nil {
panic(fmt.Sprintf("DB: %v", err))
}
return conn
}
// indexHandler responds to requests with our list of available databases.
func indexHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
w.Header().Set("Content-Type", "text/plain")
rows, err := db.Query("SHOW DATABASES")
if err != nil {
log.Printf("Could not query db: %v", err)
http.Error(w, "Internal Error", 500)
return
}
defer rows.Close()
buf := bytes.NewBufferString("Databases:\n")
for rows.Next() {
var dbName string
if err := rows.Scan(&dbName); err != nil {
log.Printf("Could not scan result: %v", err)
http.Error(w, "Internal Error", 500)
return
}
fmt.Fprintf(buf, "- %s\n", dbName)
}
w.Write(buf.Bytes())
}
func mustGetenv(k string) string {
v := os.Getenv(k)
if v == "" {
log.Panicf("%s environment variable not set.", k)
}
return v
}
// [END gae_cloudsql]