-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelloworld.go
102 lines (80 loc) · 2.6 KB
/
helloworld.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
package main
import (
"fmt"
"log"
"net/http"
"os"
"database/sql"
_ "github.com/go-sql-driver/mysql"
)
func handler(w http.ResponseWriter, r *http.Request) {
log.Print("Hello world received a request.")
target := os.Getenv("TARGET")
if target == "" {
target = "World"
}
fmt.Fprintf(w, "Hello %s!\n", target)
}
// mustGetEnv is a helper function for getting environment variables.
// Displays a warning if the environment variable is not set.
func mustGetenv(k string) string {
v := os.Getenv(k)
if v == "" {
log.Printf("Warning: %s environment variable not set.\n", k)
}
return v
}
func initSocketConnectionPool() (*sql.DB, error) {
// [START cloud_sql_mysql_databasesql_create_socket]
var (
dbUser = mustGetenv("DB_USER")
dbPwd = mustGetenv("DB_PASS")
instanceConnectionName = mustGetenv("INSTANCE_CONNECTION_NAME")
dbName = mustGetenv("DB_NAME")
)
var dbURI string
dbURI = fmt.Sprintf("%s:%s@unix(/cloudsql/%s)/%s", dbUser, dbPwd, instanceConnectionName, dbName)
// dbPool is the pool of database connections.
dbPool, err := sql.Open("mysql", dbURI)
if err != nil {
return nil, fmt.Errorf("sql.Open: %v", err)
}
// [START_EXCLUDE]
configureConnectionPool(dbPool)
// [END_EXCLUDE]
return dbPool, nil
// [END cloud_sql_mysql_databasesql_create_socket]
}
// configureConnectionPool sets database connection pool properties.
// For more information, see https://golang.org/pkg/database/sql
func configureConnectionPool(dbPool *sql.DB) {
// [START cloud_sql_mysql_databasesql_limit]
// Set maximum number of connections in idle connection pool.
dbPool.SetMaxIdleConns(5)
// Set maximum number of open connections to the database.
dbPool.SetMaxOpenConns(7)
// [END cloud_sql_mysql_databasesql_limit]
// [START cloud_sql_mysql_databasesql_lifetime]
// Set Maximum time (in seconds) that a connection can remain open.
dbPool.SetConnMaxLifetime(1800)
// [END cloud_sql_mysql_databasesql_lifetime]
}
func main() {
log.Print("Hello world sample started.")
db, err := initSocketConnectionPool()
if err != nil {
log.Fatalf("initSocketConnectionPool: unable to connect: %s", err)
}
// Create the votes table if it does not already exist.
if _, err = db.Exec(`CREATE TABLE IF NOT EXISTS votes
( vote_id SERIAL NOT NULL, time_cast timestamp NOT NULL,
candidate CHAR(6) NOT NULL, PRIMARY KEY (vote_id) );`); err != nil {
log.Fatalf("DB.Exec: unable to create table: %s", err)
}
http.HandleFunc("/", handler)
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
}