Browse Source

Merge branch 'fortune'

Patrick Falls 12 years ago
parent
commit
42ad6b2756
4 changed files with 71 additions and 3 deletions
  1. 1 0
      go/benchmark_config
  2. 47 3
      go/src/hello/hello.go
  3. 14 0
      go/templates/fortune.html
  4. 9 0
      go/templates/layout.html

+ 1 - 0
go/benchmark_config

@@ -6,6 +6,7 @@
       "json_url": "/json",
       "db_url": "/db",
       "query_url": "/db?queries=",
+      "fortune_url": "/fortune",
       "port": 8080,
       "sort": 27
     }

+ 47 - 3
go/src/hello/hello.go

@@ -9,6 +9,8 @@ import (
 	"net/http"
 	"runtime"
 	"strconv"
+        "html/template"
+	"sort"
 )
 
 type MessageStruct struct {
@@ -20,10 +22,16 @@ type World struct {
 	RandomNumber uint16 `json:"randomNumber"`
 }
 
+type Fortune struct {
+	Id           uint16 `json:"id"`
+	Message      string `json:"message"`
+}
+
 const (
-	DB_CONN_STR   = "benchmarkdbuser:benchmarkdbpass@tcp(172.16.98.98:3306)/hello_world?charset=utf8"
-	DB_SELECT_SQL = "SELECT id, randomNumber FROM World where id = ?;"
-	DB_ROWS       = 10000
+	DB_CONN_STR           = "benchmarkdbuser:benchmarkdbpass@tcp(localhost:3306)/hello_world?charset=utf8"
+	DB_SELECT_SQL         = "SELECT id, randomNumber FROM World where id = ?;"
+	DB_FORTUNE_SELECT_SQL = "SELECT id, message FROM Fortune;"
+	DB_ROWS               = 10000
 )
 
 var (
@@ -42,6 +50,7 @@ func main() {
 	}
 	http.HandleFunc("/json", jsonHandler)
 	http.HandleFunc("/db", dbHandler)
+	http.HandleFunc("/fortune", fortuneHandler)
 	http.ListenAndServe(":8080", nil)
 }
 
@@ -66,3 +75,38 @@ func dbHandler(w http.ResponseWriter, r *http.Request) {
 	w.Header().Set("Content-Length", strconv.Itoa(len(j)))
 	w.Write(j)
 }
+
+func fortuneHandler(w http.ResponseWriter, r *http.Request) {
+	// the Fortune table contains 12 rows, and we'll add another Fortune ourselves
+	fortunes := make([]Fortune, 13)
+  
+	// Execute the query
+	rows, err := db.Query(DB_FORTUNE_SELECT_SQL)
+	if err != nil {
+		log.Fatalf("Error preparing statement: %s", err)
+	}
+  
+	i := 0
+	// Fetch rows
+	for rows.Next() {
+		// get RawBytes from data
+		err = rows.Scan(&fortunes[i].Id, &fortunes[i].Message)
+		if err != nil {
+			panic(err.Error())
+		}
+		i++
+	}
+        fortunes[i].Message = "Additional fortune added at request time."
+	
+  	sort.Sort(ByMessage{fortunes})
+	var tmpl = template.Must(template.ParseFiles("templates/layout.html", "templates/fortune.html"))
+	if err := tmpl.Execute(w, map[string]interface{} {"fortunes": fortunes}); err != nil {
+		http.Error(w, err.Error(), http.StatusInternalServerError)
+	}
+}
+
+type Fortunes []Fortune
+func (s Fortunes) Len() int      { return len(s) }
+func (s Fortunes) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
+type ByMessage struct{ Fortunes }
+func (s ByMessage) Less(i, j int) bool { return s.Fortunes[i].Message < s.Fortunes[j].Message }

+ 14 - 0
go/templates/fortune.html

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

+ 9 - 0
go/templates/layout.html

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