Skip to content

Instantly share code, notes, and snippets.

@igrigorik
Forked from joaodrp/Bench
Created January 7, 2012 18:49
Show Gist options
  • Save igrigorik/1575619 to your computer and use it in GitHub Desktop.
Save igrigorik/1575619 to your computer and use it in GitHub Desktop.
# ruby getimage.rb -e production -c config/getimage.rb
# require "em-synchrony"
# require "em-synchrony/mysql2"
# require 'json'
# require 'uri'
#
# module SomeModule
# module MyExceptions
# class NotFound < StandardError; end
# end
# end
#
# module MyModule
# class EmMySQL
# include SomeModule::MyExceptions
#
# def self.connect(opts = {})
# uri = URI.parse(opts[:uri])
# opts[:host] = uri.host=='' ? '127.0.0.1' : uri.host
# opts[:port] = uri.port=='' ? 3306 : uri.port
# opts[:db] = uri.path=='' ? 'my_db' : uri.path.gsub('/', '')
# opts[:user] = uri.user=='' ? 'my_db' : uri.user
# opts[:password] = uri.password=='' ? 'passwd' : uri.password
# self.new opts
# end
#
# def initialize(opts)
# @host = opts[:host]
# @port = opts[:port]
# @db = opts[:db]
# @user = opts[:user]
# @password = opts[:password]
#
# EventMachine.synchrony do
# conn = connection
# conn.query %[
# CREATE TABLE IF NOT EXISTS `widgets`.`images` (
# `_id` VARCHAR(36) NOT NULL ,
# `name` VARCHAR(45) NOT NULL ,
# `access` VARCHAR(45) NOT NULL ,
# `type` VARCHAR(45) NULL ,
# `format` VARCHAR(45) NULL ,
# `owner` VARCHAR(45) NULL ,
# `status` VARCHAR(45) NULL ,
# `size` INT NULL ,
# `created_at` DATETIME NULL ,
# `accessed_at` DATETIME NULL ,
# `access_count` INT NULL DEFAULT 0 ,
# PRIMARY KEY (`_id`) )
# ENGINE = InnoDB;
# ]
# conn.close
# EventMachine.stop
# end
# end
#
# def connection
# Mysql2::EM::Client.new(host: @host, port: @port, database: @db,
# username: @user, password: @password)
# end
#
# def get_image(id)
# conn = connection
# meta = conn.query("SELECT * FROM images WHERE _id='#{id}'", symbolize_keys: true).first
# raise NotFound, "No image found with id '#{id}'." if meta.nil?
#
# set_protected_get(id, conn)
# conn.close
# meta
# end
#
# def set_protected_get(id, conn)
# conn.query "UPDATE images SET accessed_at='#{Time.now}', access_count=access_count+1 WHERE _id='#{id}'"
# end
# end
# end
#
# config['db'] = EventMachine::Synchrony::ConnectionPool.new(size: 1) do
# MyModule::EmMySQL.connect uri: 'mysql://127.0.0.1:3306/visor'
# end
#
#
# require 'mysql2'
require 'em-synchrony/mysql2'
p 'running configure'
config['db'] = EventMachine::Synchrony::ConnectionPool.new(size: 10) do
::Mysql2::EM::Client.new(:hostname => 'localhost', :username => 'root',
# :password => 'password', :socket => nil,
:database => 'widgets',
:reconnect => true)
end
require 'goliath'
require 'json'
module SomeModule
module MyExceptions
class NotFound < StandardError; end
end
end
module MyModule
class GetImage < Goliath::API
include SomeModule::MyExceptions
def response(env)
begin
id = params[:id]
image = db.query("SELECT * FROM images WHERE _id='#{id}'", symbolize_keys: true).first
raise NotFound, "No image found with id '#{id}'." if image.nil?
db.query "UPDATE images SET accessed_at='#{Time.now}', access_count=access_count+1 WHERE _id='#{id}'"
[200, {}, {image: image}.to_json]
rescue NotFound => e
[404, {}, {code: 404, message: e.message}.to_json]
end
end
end
class GoliathServer < Goliath::API
use Goliath::Rack::Heartbeat
use Goliath::Rack::Params
use Goliath::Rack::Render
use Goliath::Rack::DefaultMimeType
use Goliath::Rack::Validation::RequestMethod, %w(GET POST PUT DELETE)
get '/images/:id', GetImage
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment