Skip to content

Instantly share code, notes, and snippets.

@luftreich
Last active August 29, 2015 14:19
Show Gist options
  • Save luftreich/dddbf52fc22ab65d93a9 to your computer and use it in GitHub Desktop.
Save luftreich/dddbf52fc22ab65d93a9 to your computer and use it in GitHub Desktop.
package main
import (
"database/sql"
"encoding/json"
"html/template"
"log"
"math/rand"
"net/http"
"runtime"
_ "github.com/go-sql-driver/mysql"
)
type Message struct {
Message string `json:"message"`
}
type World struct {
Id uint16 `json:"id"`
RandomNumber uint16 `json:"randomNumber"`
}
type Fortune struct {
Id uint16 `json:"id"`
Message string `json:"message"`
}
// TODO: remove ?charset=utf8 from DSN after the next Go-MySQL-Driver release
// https://github.com/go-sql-driver/mysql#unicode-support
const (
// Database
connectionString = "root:@tcp(localhost:3306)/hello_world?charset=utf8"
worldSelect = "SELECT id, randomNumber FROM World WHERE id = ?"
fortuneSelect = "SELECT id, message FROM Fortune;"
//worldRowCount = 10000
worldRowCount = 6
maxConnectionCount = 256
helloWorldString = "Hello, World!"
)
var (
// Templates
tmpl = template.Must(template.ParseFiles("templates/layout.html", "templates/fortune.html"))
// Database
worldStatement *sql.Stmt
updateStatement *sql.Stmt
helloWorldBytes = []byte(helloWorldString)
)
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
db, err := sql.Open("mysql", connectionString)
if err != nil {
log.Fatalf("Error opening database: %v", err)
}
db.SetMaxIdleConns(maxConnectionCount)
worldStatement, err = db.Prepare(worldSelect)
if err != nil {
log.Fatal(err)
}
http.HandleFunc("/", dbHandler)
http.ListenAndServe(":8080", nil)
}
// Test 2: Single database query
func dbHandler(w http.ResponseWriter, r *http.Request) {
var world World
err := worldStatement.QueryRow(rand.Intn(worldRowCount)+1).Scan(&world.Id, &world.RandomNumber)
if err != nil {
log.Fatalf("Error scanning world row: %s", err.Error())
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(&world)
}
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 }
require 'json'
require 'eventmachine'
require 'mysql2/em'
require './pool'
$MAX_RECORD = 6
class Server < EventMachine::Connection
def receive_data(data)
$client.query "SELECT * from World where id = #{rand(6) + 1}" do |result|
send_data JSON.dump(result.to_a)
close_connection_after_writing
end
end
end
EM.run do
EM.next_tick do
$client = Pool.new username: "root", database: "hello_world", size: 20
end
EM.start_server '0.0.0.0', 3001, Server
end
require 'json'
require 'eventmachine'
require 'mysql2/em'
require './pool'
$MAX_RECORD = 6
class Server < EventMachine::Connection
def receive_data(data)
$client.query "SELECT * from World where id = #{rand(6) + 1}" do |result|
send_data JSON.dump(result.to_a)
close_connection_after_writing
en
end
end
EM.run do
EM.next_tick do
$client = Pool.new username: "root", database: "hello_world", size: 20
end
EM.start_server '0.0.0.0', 3001, Server
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment