Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
convert code blocks to fenced code for syntax highlighting, removed s…
…ome whitespace
  • Loading branch information
Zachary Scott committed Mar 17, 2013
1 parent ebfbf9a commit 35c519e
Show file tree
Hide file tree
Showing 31 changed files with 1,578 additions and 1,246 deletions.
156 changes: 84 additions & 72 deletions databases/mongo.md
Expand Up @@ -15,102 +15,114 @@ available.
The first step is in connecting your application to an instance of Mongo is
to create a connection. You can do this in your _configure_ block:

require 'rubygems'
require 'sinatra'
require 'mongo'
require 'json' # required for .to_json

configure do
conn = Mongo::Connection.new("localhost", 27017)
set :mongo_connection, conn
set :mongo_db, conn.db('test')
end
```ruby
require 'rubygems'
require 'sinatra'
require 'mongo'
require 'json' # required for .to_json

configure do
conn = Mongo::Connection.new("localhost", 27017)
set :mongo_connection, conn
set :mongo_db, conn.db('test')
end
```

With a connection "in hand" you can connect to any database and collection in
your Mongo instance.

get '/collections/?' do
settings.mongo_db.collection_names
end

helpers do
# a helper method to turn a string ID
# representation into a BSON::ObjectId
def object_id val
BSON::ObjectId.from_string(val)
end
def document_by_id id
id = object_id(id) if String === id
settings.mongo_db['test'].
find_one(:_id => id).to_json
end
end
```ruby
get '/collections/?' do
settings.mongo_db.collection_names
end

helpers do
# a helper method to turn a string ID
# representation into a BSON::ObjectId
def object_id val
BSON::ObjectId.from_string(val)
end

def document_by_id id
id = object_id(id) if String === id
settings.mongo_db['test'].
find_one(:_id => id).to_json
end
end
```
<span style='font-size: smaller'>([top](#top))</span>

<a name='find' />
###Finding Records###

# list all documents in the test collection
get '/documents/?' do
content_type :json
settings.mongo_db['test'].find.to_a.to_json
end

# find a document by its ID
get '/document/:id/?' do
content_type :json
document_by_id(params[:id]).to_json
end
```ruby
# list all documents in the test collection
get '/documents/?' do
content_type :json
settings.mongo_db['test'].find.to_a.to_json
end

# find a document by its ID
get '/document/:id/?' do
content_type :json
document_by_id(params[:id]).to_json
end
```
<span style='font-size: smaller'>([top](#top))</span>

<a name='insert' />
###Inserting Records###

# insert a new document from the request parameters,
# then return the full document
post '/new_document/?' do
content_type :json
new_id = settings.mongo_db['test'].insert params
document_by_id(new_id).to_json
end
```ruby
# insert a new document from the request parameters,
# then return the full document
post '/new_document/?' do
content_type :json
new_id = settings.mongo_db['test'].insert params
document_by_id(new_id).to_json
end
```
<span style='font-size: smaller'>([top](#top))</span>

<a name='update' />
###Updating Records###

# update the document specified by :id, setting its
# contents to params, then return the full document
put '/update/:id/?' do
content_type :json
id = object_id(params[:id])
settings.mongo_db['test'].update(:_id => id, params)
document_by_id(id).to_json
end

# update the document specified by :id, setting just its
# name attribute to params[:name], then return the full
# document
put '/update_name/:id/?' do
content_type :json
id = object_id(params[:id])
name = params[:name]
settings.mongo_db['test'].
update(:_id => id, {"$set" => {:name => name}})
document_by_id(id).to_json
end
```ruby
# update the document specified by :id, setting its
# contents to params, then return the full document
put '/update/:id/?' do
content_type :json
id = object_id(params[:id])
settings.mongo_db['test'].update(:_id => id, params)
document_by_id(id).to_json
end

# update the document specified by :id, setting just its
# name attribute to params[:name], then return the full
# document
put '/update_name/:id/?' do
content_type :json
id = object_id(params[:id])
name = params[:name]
settings.mongo_db['test'].
update(:_id => id, {"$set" => {:name => name}})
document_by_id(id).to_json
end
```
<span style='font-size: smaller'>([top](#top))</span>

<a name='delete' />
###Deleting Records###

# delete the specified document and return success
delete '/remove/:id' do
content_type :json
settings.mongo_db['test'].
remove(:_id => object_id(params[:id]))
{:success => true}.to_json
end
```ruby
# delete the specified document and return success
delete '/remove/:id' do
content_type :json
settings.mongo_db['test'].
remove(:_id => object_id(params[:id]))
{:success => true}.to_json
end
```
<span style='font-size: smaller'>([top](#top))</span>

For more information on using the Ruby driver without an ORM take a look at [MongoDB's tutorial][rubydrivertutorial].
Expand Down
56 changes: 35 additions & 21 deletions deployment/apache_with_passenger.md
Expand Up @@ -19,11 +19,15 @@ You have a number of options when [installing phusion
passenger](http://modrails.com/documentation/Users%20guide%20Apache.html#_installing_upgrading_and_uninstalling_phusion_passenger),
however the gem is likely the easiest way to get started.

gem install passenger
```bash
gem install passenger
```

Once you've got that installed you can build the passenger apache module.

passenger-install-apache2-module
```bash
passenger-install-apache2-module
```

Follow the instructions given by the installer.

Expand All @@ -37,9 +41,11 @@ the `tmp` and `public` sub-directories of your application.
In order to fit these prerequisites, simply make sure you have the following
setup:

mkdir public
mkdir tmp
config.ru
```bash
mkdir public
mkdir tmp
config.ru
```

The public directory is for serving static files and tmp directory is for the
`restart.txt` application restart mechanism. `config.ru` is where you will
Expand All @@ -50,25 +56,29 @@ place your rackup configuration.
Once you have these directories in place, you can setup your applications
rackup file, `config.ru`.

require 'rubygems'
require 'sinatra'
require File.expand_path '../app.rb', __FILE__
```ruby
require 'rubygems'
require 'sinatra'
require File.expand_path '../app.rb', __FILE__

run Sinatra::Application
run Sinatra::Application
```

**Virtual Host**

Next thing you'll have to do is setup the [Apache Virtual
Host](http://httpd.apache.org/docs/2.2/vhosts/) for your app.

<VirtualHost *:80>
ServerName www.yourapplication.com
DocumentRoot /path/to/app/public
<Directory /path/to/app/public>
Allow from all
Options -MultiViews
</Directory>
</VirtualHost>
```bash
<VirtualHost *:80>
ServerName www.yourapplication.com
DocumentRoot /path/to/app/public
<Directory /path/to/app/public>
Allow from all
Options -MultiViews
</Directory>
</VirtualHost>
```

That should just about do it for your basic apache and passenger configuration.
For more specific information please visit the [official modrails
Expand All @@ -81,17 +91,21 @@ Apache](http://httpd.apache.org/docs/2.2/stopping.html).

On most debian-based systems you should be able to:

sudo apache2ctl stop
# then
sudo apache2ctl start
```bash
sudo apache2ctl stop
# then
sudo apache2ctl start
```

To restart Apache. Check the link above for more detailed information.

In order to [restart the Passenger
application](http://www.modrails.com/documentation/Users%20guide%20Apache.html#_redeploying_restarting_the_ruby_on_rails_application),
all you need to do is run this simple command for your application root:

touch tmp/restart.txt
```bash
touch tmp/restart.txt
```

You should be up and running now with [Phusion Passenger](http://modrails.com/)
and [Apache](http://httpd.apache.org/), if you run into any problems please
Expand Down
88 changes: 50 additions & 38 deletions deployment/dreamhost_via_passenger.md
Expand Up @@ -4,41 +4,51 @@ Dreamhost Deployment via Passenger
You can deploy your Sinatra apps to Dreamhost, a shared web hosting service,
via Passenger with relative ease. Here's how.

1. Setting up the account in the Dreamhost interface

Domains -> Manage Domains -> Edit (web hosting column)
Enable 'Ruby on Rails Passenger (mod_rails)'
Add the public directory to the web directory box. So if you were using 'rails.com', it would change to 'rails.com/public'
Save your changes

2. Creating the directory structure

domain.com/
domain.com/tmp
domain.com/public
# a vendored version of sinatra - not necessary if you use the gem
domain.com/sinatra

3. Here is an example config.ru file that does two things. First, it requires
your main app file, whatever it's called. In the example, it will look for
`myapp.rb`. Second, run your application. If you're subclassing, use the
subclass's name, otherwise use Sinatra::Application.

require File.expand_path '../myapp.rb', __FILE__

run Sinatra::Application

4. A very simple Sinatra application

# this is myapp.rb referred to above
require 'sinatra'
get '/' do
"Worked on dreamhost"
end
get '/foo/:bar' do
"You asked for foo/#{params[:bar]}"
end
### Setting up the account in the Dreamhost interface

```
Domains -> Manage Domains -> Edit (web hosting column)
Enable 'Ruby on Rails Passenger (mod_rails)'
Add the public directory to the web directory box. So if you were using 'rails.com', it would change to 'rails.com/public'
Save your changes
```

### Creating the directory structure

```
domain.com/
domain.com/tmp
domain.com/public
# a vendored version of sinatra - not necessary if you use the gem
domain.com/sinatra
```

### Rack integration

Here is an example config.ru file that does two things. First, it requires
your main app file, whatever it's called. In the example, it will look for
`myapp.rb`. Second, run your application. If you're subclassing, use the
subclass's name, otherwise use Sinatra::Application.

```ruby
require File.expand_path '../myapp.rb', __FILE__

run Sinatra::Application
```

### A very simple Sinatra application

```ruby
# this is myapp.rb referred to above
require 'sinatra'
get '/' do
"Worked on dreamhost"
end

get '/foo/:bar' do
"You asked for foo/#{params[:bar]}"
end
```

And that's all there is to it! Once it's all setup, point your browser at your
domain, and you should see a 'Worked on Dreamhost' page. To restart the
Expand All @@ -54,7 +64,9 @@ already activated rack-0.4.0". This happens because DreamHost has version 0.4.0
installed, when recent versions of Sinatra require more recent versions of Rack.
The solution is to explicitly require the rack and sinatra gems in your
config.ru. Add the following two lines to the start of your config.ru file:

require '/home/USERNAME/.gem/ruby/1.8/gems/rack-VERSION-OF-RACK-GEM-YOU-HAVE-INSTALLELD/lib/rack.rb'
require '/home/USERNAME/.gem/ruby/1.8/gems/sinatra-VERSION-OF-SINATRA-GEM-YOU-HAVE-INSTALLELD/lib/sinatra.rb'

```ruby
require '/home/USERNAME/.gem/ruby/1.8/gems/rack-VERSION-OF-RACK-GEM-YOU-HAVE-INSTALLELD/lib/rack.rb'
require '/home/USERNAME/.gem/ruby/1.8/gems/sinatra-VERSION-OF-SINATRA-GEM-YOU-HAVE-INSTALLELD/lib/sinatra.rb'
```

0 comments on commit 35c519e

Please sign in to comment.