Browse Source

Merge pull request #1879 from kostya-sh/go-raw_simpler-deployment

go-raw: simplify deployment
Mike Smith 9 years ago
parent
commit
f28cfe97d1

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

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

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

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

+ 0 - 3
frameworks/Go/go-raw/source_code

@@ -1,6 +1,3 @@
 ./go/src/
 ./go/src/hello
 ./go/src/hello/hello.go
-./go/templates/
-./go/templates/fortune.html
-./go/templates/layout.html

+ 29 - 3
frameworks/Go/go-raw/src/hello/hello.go

@@ -4,6 +4,7 @@ import (
 	"database/sql"
 	"encoding/json"
 	"flag"
+	"fmt"
 	"html/template"
 	"log"
 	"math/rand"
@@ -33,7 +34,28 @@ type Fortune struct {
 }
 
 const (
+	// Content
 	helloWorldString = "Hello, World!"
+	fortuneHTML      = `<!DOCTYPE html>
+<html>
+<head>
+<title>Fortunes</title>
+</head>
+<body>
+<table>
+<tr>
+<th>id</th>
+<th>message</th>
+</tr>
+{{range .}}
+<tr>
+<td>{{.Id}}</td>
+<td>{{.Message}}</td>
+</tr>
+{{end}}
+</table>
+</body>
+</html>`
 
 	// Databases
 	//
@@ -41,7 +63,7 @@ const (
 	// It reduces round trips without prepared statement.
 	//
 	// 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 = ?"
 	worldUpdate      = "UPDATE World SET randomNumber = ? WHERE id = ?"
 	fortuneSelect    = "SELECT id, message FROM Fortune"
@@ -53,7 +75,7 @@ var (
 	helloWorldBytes = []byte(helloWorldString)
 
 	// Templates
-	tmpl = template.Must(template.ParseFiles("templates/layout.html", "templates/fortune.html"))
+	tmpl = template.Must(template.New("fortune.html").Parse(fortuneHTML))
 
 	// Database
 	db                    *sql.DB
@@ -67,7 +89,11 @@ var child = flag.Bool("child", false, "is child proc")
 
 func initDB() {
 	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 {
 		log.Fatalf("Error opening database: %v", err)
 	}

+ 0 - 14
frameworks/Go/go-raw/templates/fortune.html

@@ -1,14 +0,0 @@
-{{define "content"}}
-<table>
-<tr>
-<th>id</th>
-<th>message</th>
-</tr>
-{{range .}}
-<tr>
-<td>{{.Id}}</td>
-<td>{{.Message}}</td>
-</tr>
-{{end}}
-</table>
-{{end}}

+ 0 - 9
frameworks/Go/go-raw/templates/layout.html

@@ -1,9 +0,0 @@
-<!DOCTYPE html>
-<html>
-<head>
-<title>Fortunes</title>
-</head>
-<body>
-{{template "content" .}}
-</body>
-</html>