Browse Source

go-raw: handle DBHOST env variable expansion in the application

This removes some duplication from the setup shell scripts and makes it easier
to specify database location when running the application outside of TFB
framework.
Konstantin Shaposhnikov 9 years ago
parent
commit
2861878a20

+ 0 - 2
frameworks/Go/go-raw/setup.sh

@@ -1,7 +1,5 @@
 #!/bin/bash
 #!/bin/bash
 
 
-sed -i 's|tcp(.*:3306)|tcp('"${DBHOST}"':3306)|g' src/hello/hello.go
-
 fw_depends go
 fw_depends go
 
 
 go get ./...
 go get ./...

+ 0 - 2
frameworks/Go/go-raw/setup_prefork.sh

@@ -1,7 +1,5 @@
 #!/bin/bash
 #!/bin/bash
 
 
-sed -i 's|tcp(.*:3306)|tcp('"${DBHOST}"':3306)|g' src/hello/hello.go
-
 fw_depends go
 fw_depends go
 
 
 go get ./...
 go get ./...

+ 7 - 2
frameworks/Go/go-raw/src/hello/hello.go

@@ -4,6 +4,7 @@ import (
 	"database/sql"
 	"database/sql"
 	"encoding/json"
 	"encoding/json"
 	"flag"
 	"flag"
+	"fmt"
 	"html/template"
 	"html/template"
 	"log"
 	"log"
 	"math/rand"
 	"math/rand"
@@ -41,7 +42,7 @@ const (
 	// It reduces round trips without prepared statement.
 	// It reduces round trips without prepared statement.
 	//
 	//
 	// We can see difference between prepared statement and interpolation by comparing go-raw and go-raw-interpolate
 	// We can see difference between prepared statement and interpolation by comparing go-raw and go-raw-interpolate
-	connectionString = "benchmarkdbuser:benchmarkdbpass@tcp(localhost:3306)/hello_world?interpolateParams=true"
+	connectionString = "benchmarkdbuser:benchmarkdbpass@tcp(%s:3306)/hello_world?interpolateParams=true"
 	worldSelect      = "SELECT id, randomNumber FROM World WHERE id = ?"
 	worldSelect      = "SELECT id, randomNumber FROM World WHERE id = ?"
 	worldUpdate      = "UPDATE World SET randomNumber = ? WHERE id = ?"
 	worldUpdate      = "UPDATE World SET randomNumber = ? WHERE id = ?"
 	fortuneSelect    = "SELECT id, message FROM Fortune"
 	fortuneSelect    = "SELECT id, message FROM Fortune"
@@ -67,7 +68,11 @@ var child = flag.Bool("child", false, "is child proc")
 
 
 func initDB() {
 func initDB() {
 	var err error
 	var err error
-	db, err = sql.Open("mysql", connectionString)
+	var dbhost = os.Getenv("DBHOST")
+	if dbhost == "" {
+		dbhost = "localhost"
+	}
+	db, err = sql.Open("mysql", fmt.Sprintf(connectionString, dbhost))
 	if err != nil {
 	if err != nil {
 		log.Fatalf("Error opening database: %v", err)
 		log.Fatalf("Error opening database: %v", err)
 	}
 	}