Skip to content

Commit

Permalink
Added WeeDB example Halcyon app. There are sufficient sample apps for…
Browse files Browse the repository at this point in the history
… now. [#23 state:resolved]
  • Loading branch information
mtodd committed May 13, 2008
1 parent 08cf865 commit 3a3108b
Show file tree
Hide file tree
Showing 18 changed files with 507 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/weedb/.gitignore
@@ -0,0 +1,2 @@
config/database.yml
log/*.log
62 changes: 62 additions & 0 deletions examples/weedb/README
@@ -0,0 +1,62 @@
= WeeDB

The Halcyon clone of TinyDB (http://tinydb.org/).


== Introduction

This is a simple


== Installation & Setup

Install the dependencies listed below, set up the database, and run the
migrations.

=== Dependencies

* Halcyon (halcyon >= 0.5.0)
* Sequel (sequel >= 1.5.1)

=== Database

Set up a database, @weedb_development@ for example, and create a user,
@wee_user@ should work fine. Copy the @config/database.sample.yml@ file to
@config/database.yml@ and update the values to match the database name and user
set up.

Now, run the migrations:

$ rake db:migrate

== Usage

=== Start The Server

$ halcyon start -p 4647

=== Interacting With The Server

In Ruby:

$ irb -r lib/client
>> client = WeeDB::Client.new("http://localhost:4647/")
>> client << {'foo' => 'bar'}
=> '1aB2'
>> client['1aB2']
=> {'foo' => 'bar'}

Elsewhere:

$ curl --data '' http://localhost:4647/?foo=bar
{'status':200,'body':'2bC3'}
$ curl http://localhost:4647/2bC3
{'status':200,'body':{'foo':'bar'}}

== License

WeeDB is licensed under the MIT License.

== Contact

Matt Todd <chiology@gmail.com>
51 changes: 51 additions & 0 deletions examples/weedb/Rakefile
@@ -0,0 +1,51 @@
$:.unshift(File.join(File.dirname(__FILE__), 'lib'))
%w(rubygems rake rake/clean rake/rdoctask fileutils pp logger rack/mock halcyon).each{|dep|require dep}

include FileUtils

# Halcyon.root => the root application directory
# Halcyon.app => the application name

desc "Start the application on port 4647"
task :start do
sh "halcyon start -p 4647"
end

desc "Generate RDoc documentation"
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.options << '--line-numbers' << '--inline-source' <<
'--main' << 'README' <<
'--title' << "#{Halcyon.app} Documentation" <<
'--charset' << 'utf-8'
rdoc.rdoc_dir = "doc"
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('app/**/*.rb')
rdoc.rdoc_files.include('lib/**/*.rb')
end

# = Custom Rake Tasks
#
# Add your custom rake tasks here.

desc "Load up the application environment"
task :env do
$log = ''
$logger = Logger.new(StringIO.new($log))
Halcyon.config = {:logger => $logger}
Halcyon::Runner.new
end

namespace(:db) do

desc "Migrate the database to the latest version"
task :migrate => :env do
current_version = Sequel::Migrator.get_current_migration_version(WeeDB::DB)
latest_version = Sequel::Migrator.apply(WeeDB::DB, Halcyon.paths[:lib]/'migrations')
puts "Database successfully migrated to latest version (#{latest_version})." if current_version < latest_version
puts "Migrations finished successfully."
end

end

# = Default Task
task :default => Rake::Task['start']
13 changes: 13 additions & 0 deletions examples/weedb/app/application.rb
@@ -0,0 +1,13 @@
class Application < Halcyon::Controller

# Shortcuts

def read
#
end

def write
#
end

end
1 change: 1 addition & 0 deletions examples/weedb/app/models/record.rb
@@ -0,0 +1 @@
class Record < Sequel::Model; end
49 changes: 49 additions & 0 deletions examples/weedb/app/records.rb
@@ -0,0 +1,49 @@
class Records < Application

# GET /records
def index
raise NotImplemented
end

# GET /records/1
def show
if record = Record[:url => params[:id]]
ok JSON.parse(record[:data])
else
raise NotFound
end
end

# POST /records
# POST /records?key1=value1&key2=value2&...
def create
verify_data_validity!

record = Record.new
record.url = WeeDB.generate_unique_url_key
record.data = (JSON.parse(params[:data]) rescue nil || {}).merge(query_params).to_json

if record.save
ok record.url
else
raise Exception
end
end

# PUT /records/1
def update
raise NotImplemented
end

# DELETE /records/1
def destroy
raise NotImplemented
end

private

def verify_data_validity!
raise BadRequest unless (JSON.parse(params[:data]) rescue nil).is_a?(Hash) or params[:data].nil?
end

end
29 changes: 29 additions & 0 deletions examples/weedb/config/config.yml
@@ -0,0 +1,29 @@
# = Framework
#
# <tt>allow_from</tt>: specifies what connections to accept;
# * <tt>all</tt>: allow connections from all clients
# * <tt>local</tt>: only allow connections from the same host (localhost et al)
# * <tt>halcyon_clients</tt>: only Halcyon clients (tests the User-Agent only)
allow_from: all

# = Logging
#
# Configures the logging client in the framework, including destination,
# level filter, and what logger to use.
#
# <tt>type</tt>: the logger to use (defaults to Ruby's <tt>Logger</tt>)
# * <tt>Logger</tt>
# * <tt>Analogger</tt>
# * <tt>Logging</tt>
# * <tt>Log4r</tt>
# <tt>file</tt>: the log file; leave unset for STDOUT
# <tt>level</tt>: the message filter level (default to <tt>debug</tt>)
# * specific to the client used, often is: debug, info, warn, error, fatal
logging:
type: Logger
# file: # STDOUT
level: debug

# = Application
#
# Your application-specific configuration options here.
23 changes: 23 additions & 0 deletions examples/weedb/config/database.sample.yml
@@ -0,0 +1,23 @@
development:
adapter: mysql
encoding: utf8
database: weedb_development
username: wee_user
password: password
host: localhost

test:
adapter: mysql
encoding: utf8
database: weedb_development
username: wee_user
password: password
host: localhost

production:
adapter: mysql
encoding: utf8
database: weedb_development
username: wee_user
password: password
host: localhost
6 changes: 6 additions & 0 deletions examples/weedb/config/init/database.rb
@@ -0,0 +1,6 @@
# = Database
#
# Load the database configuration for the current environment.

Halcyon.db = Halcyon::Runner.load_config(Halcyon::Runner.config_path('database'))
Halcyon.db = Halcyon.db[(Halcyon.environment || :development).to_sym]
4 changes: 4 additions & 0 deletions examples/weedb/config/init/environment.rb
@@ -0,0 +1,4 @@
# = Environment

Halcyon.configurable_attr :environment
Halcyon.environment = :development unless Halcyon.environment
47 changes: 47 additions & 0 deletions examples/weedb/config/init/hooks.rb
@@ -0,0 +1,47 @@
# = Hooks
#
# Specify actions to run at specific times during the application's execution,
# such as the startup or shutdown events.
#
# Available Hooks
# +startup+
# +shutdown+
#
# All hooks take the current application configuration as its sole block param.
#
# Examples
# Halcyon::Application.startup do |config|
# # Halcyon.db set in config/initialize/database.rb
# ::DB = Sequel(Halcyon.db)
# logger.info "Connected to database"
# end
#
# The +logger+ object is available to log messages on status or otherwise.

# = Startup
#
# Run when the Halcyon::Application object is instanciated, after all
# initializers are loaded.
#
# Ideal for establishing connections to resources like databases.
# Establish configuration options in initializer files, though.
Halcyon::Application.startup do |config|
# Connect to DB
WeeDB::DB = Sequel.connect(Halcyon.db)
WeeDB::DB.logger = Halcyon.logger if $DEBUG
logger.info 'Connected to Database'

# Load models
Dir.glob([Halcyon.paths[:model]/'*.rb']).each do |model|
logger.debug "Load: #{File.basename(model).chomp('.rb').camel_case} Model" if require model
end
end

# = Shutdown
#
# Run <tt>at_exit</tt>. Should run in most cases of termination.
#
# Ideal for closing connections to resources.
Halcyon::Application.shutdown do |config|
# logger.info 'Define shutdown tasks in config/init/hooks.rb'
end
13 changes: 13 additions & 0 deletions examples/weedb/config/init/requires.rb
@@ -0,0 +1,13 @@
# = Required Libraries
#
# Specify required libraries specific to the operation of your application.
#
# Examples
# %(digest/md5 tempfile date).each{|dep|require dep}
#
# RubyGems should already be required by Halcyon, so don't include it.

%w(weedb sequel digest/md5).each{|dep|require dep}

# JSON is also another requirement, but Halcyon already handles the complexity
# of loading the appropriate JSON Gem.
55 changes: 55 additions & 0 deletions examples/weedb/config/init/routes.rb
@@ -0,0 +1,55 @@
# = Routes
#
# Halcyon::Application::Router is the request routing mapper for the merb
# framework.
#
# You can route a specific URL to a controller / action pair:
#
# r.match("/contact").
# to(:controller => "info", :action => "contact")
#
# You can define placeholder parts of the url with the :symbol notation. These
# placeholders will be available in the params hash of your controllers. For example:
#
# r.match("/books/:book_id/:action").
# to(:controller => "books")
#
# Or, use placeholders in the "to" results for more complicated routing, e.g.:
#
# r.match("/admin/:module/:controller/:action/:id").
# to(:controller => ":module/:controller")
#
# You can also use regular expressions, deferred routes, and many other options.
# See merb/specs/merb/router.rb for a fairly complete usage sample.
#
# Stolen directly from generated Merb app. All documentation applies.
# Read more about the Merb router at http://merbivore.com/.
Halcyon::Application.route do |r|

r.resources :records

r.match('/:id', :method => 'get').to(:controller => 'records', :action => 'show')
r.match('/', :method => 'post').to(:controller => 'records', :action => 'create')

# Sample route for the sample functionality in Application.
# Safe to remove!
# r.match('/time').to(:controller => 'application', :action => 'time')

# RESTful routes
# r.resources :posts

# This is the default route for /:controller/:action/:id
# This is fine for most cases. If you're heavily using resource-based
# routes, you may want to comment/remove this line to prevent
# clients from calling your create or destroy actions with a GET
# r.default_routes

# Change this for the default route to be available at /
# r.match('/').to(:controller => 'application', :action => 'index')
# It can often be useful to respond with available functionality if the
# application is a public-facing service.

# Default not-found route
{:action => 'not_found'}

end

0 comments on commit 3a3108b

Please sign in to comment.