Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS 

Basic WEBrick setup for local web server

Put the following functions and aliases into your ~/.bash_login (or alternatives).


unset -f webrick_local

function webrick_local() { 

   declare root_dir="${HOME}/Desktop/www"

   /bin/mkdir -p "${root_dir}/WEBrickLog"
   /bin/chmod 0754 "${root_dir}" "${root_dir}/WEBrickLog"

   /usr/bin/touch "${root_dir}/WEBrickLog/webrick.pid" "${root_dir}/WEBrickLog/webrick_ruby.pid"
   /bin/chmod 0644 "${root_dir}/WEBrickLog/webrick.pid" "${root_dir}/WEBrickLog/webrick_ruby.pid"

#   /usr/bin/touch "${root_dir}/index.html"
#   /bin/chmod 0754 "${root_dir}/index.html"


/usr/local/bin/ruby <<-HEREDOC

   require 'webrick'
   require 'webrick/accesslog'
   include WEBrick

   require 'thread'
   require 'pp'

   root_dir = "${root_dir}"
   http_dir = File.expand_path(root_dir)

   File.open(http_dir + "/WEBrickLog/webrick_ruby.pid", "w") do |f|
      f.puts(Process.pid)
   end

   # cf. http://microjet.ath.cx/webrickguide/html/Logging.html
   webrick_log_file = File.expand_path(http_dir + "/WEBrickLog/webrick.log")
   #webrick_log_file = '/dev/null'  # disable logging
   webrick_logger = WEBrick::Log.new(webrick_log_file, WEBrick::Log::DEBUG)

   access_log_stream = webrick_logger
   access_log = [[ access_log_stream, WEBrick::AccessLog::COMBINED_LOG_FORMAT ]]

   system_mime_table = WEBrick::HTTPUtils::load_mime_types('/private/etc/httpd/mime.types.default')
   system_mime_table.store('rhtml', 'text/html')   # add a mime type for .rhtml files
   system_mime_table.store('php', 'text/html')
   system_mime_table.store('rb', 'text/plain')
   system_mime_table.store('pid', 'text/plain')
   #pp system_mime_table.sort_by { |k,v| k }

   server = WEBrick::HTTPServer.new(
     :BindAddress     =>    "localhost",
     :Port            =>    9090,
     :DocumentRoot    =>    http_dir,
     :FancyIndexing   =>    true,
     :MimeTypes       =>    system_mime_table,
     :Logger          =>    webrick_logger,
     :AccessLog       =>    access_log
   )

   server.config.store(:DirectoryIndex, server.config[:DirectoryIndex] << "default.htm")
   #pp server.config

   # cf. http://snippets.dzone.com/posts/show/5208
   class TimeServlet < HTTPServlet::AbstractServlet

      def do_GET(req, res)
         res['Content-Type'] = 'text/html'
         res.status = 200
         res.body = "<html>Time: #{Time.now.to_s}</html>" + "\n"
      end

      # cf. http://www.hiveminds.co.uk/node/244, published under the
      # GNU Free Documentation License, http://www.gnu.org/copyleft/fdl.html

      @@instance = nil
      @@instance_creation_mutex = Mutex.new

      def self.get_instance(config, *options)
         #pp @@instance
         @@instance_creation_mutex.synchronize { 
            @@instance = @@instance || self.new(config, *options) }
      end

   end


   # cf. http://ttripp.blogspot.com/2007/01/fun-with-http.html
   class PostDumper < WEBrick::HTTPServlet::AbstractServlet
  
      # Reload file for each request, instantly
      # updating the server with code changes 
      # without needing a restart.

=begin
      def PostDumper.get_instance( config, *options )
         load __FILE__
         PostDumper.new config, *options
      end
=end

      # cf. http://www.hiveminds.co.uk/node/244, published under the
      # GNU Free Documentation License, http://www.gnu.org/copyleft/fdl.html

      @@instance = nil
      @@instance_creation_mutex = Mutex.new

      def self.get_instance(config, *options)
         #pp @@instance
         @@instance_creation_mutex.synchronize { 
            @@instance = @@instance || self.new(config, *options) }
      end

      def do_GET( request, response )
         response.status = 200
         response['Content-Type'] = "text/plain"
         response.body = dump_request( request )
      end
  
      def do_POST( request, response )
         response.status = 200
         response['Content-Type'] = "text/plain"
         response.body = dump_request( request )
         response.body << request.body
      end
  
      def dump_request( request )
         request.request_line << "\r\n" <<
         request.raw_header.join( "" ) << "\r\n"
      end
   end

   server.mount("/dump", PostDumper)
   server.mount("/time", TimeServlet, {:FancyIndexing=>true})

   # handle signals
   %w(INT).each do |signal|
      trap(signal) { server.shutdown }
   end

   server.start

HEREDOC

return 0

}

export -f webrick_local

# start WEBrick alias
alias webrick='swr'

# start WEBrick
unset -f swr
function swr() {   
  declare webrick_pid_file="${HOME}/Desktop/www/WEBrickLog/webrick.pid"
  if [[ -e "${webrick_pid_file}" ]]; then echo "WEBrick already started!"; return 1; fi
  /bin/bash -c "webrick_local" &
  declare WEBRICKPID=$!
  mkdir -p "${HOME}/Desktop/www/WEBrickLog"
  echo $WEBRICKPID > "${HOME}/Desktop/www/WEBrickLog/webrick.pid"
  return 0
}
export -f swr

# quit WEBrick
unset -f qwr
function qwr() { 
   declare webrick_pid_file="${HOME}/Desktop/www/WEBrickLog/webrick.pid"
   declare webrick_ruby_pid_file="${HOME}/Desktop/www/WEBrickLog/webrick_ruby.pid"
   if [[ ! -e "${webrick_pid_file}" ]]; then echo "no such file: ${webrick_pid_file}"; return 1; fi
   declare PIDW=$(cat "${webrick_pid_file}" 2>/dev/null)
   declare PIDR=$(cat "${webrick_ruby_pid_file}" 2>/dev/null)
   kill -INT $PIDW 2>/dev/null || echo "no such PIDW: ${PIDW}"
   kill -INT $PIDR 2>/dev/null || echo "no such PIDR: ${PIDR}"
   rm -f "${webrick_pid_file}" "${webrick_ruby_pid_file}"
   return 0
}
export -f qwr

# quit WEBrick; cf. Job Control Commands, http://tldp.org/LDP/abs/html/x8816.html
###alias qwr='echo "quit WEBrick with ctrl-c ..."; fg %webrick 2>/dev/null'

alias openwww='/usr/bin/open http://localhost:9090'
alias wrlog='/usr/bin/open http://localhost:9090/WEBrickLog/webrick.log'


#----------------------------------------


source ~/.bash_login

webrick

openwww
wrlog
open http://localhost:9090 
open http://localhost:9090/WEBrickLog
open http://localhost:9090/WEBrickLog/webrick.log

open http://localhost:9090/time
open http://localhost:9090/dump

wrlog
qwr


Further information:

- WEBrick
- WEBrick RDoc
- What is WEBrick?
- Guide to using WEBrick for Rails development
- Dynamic WEBrick Servers in Ruby
- Gnome's Guide to WEBrick
- Running Servlets with WEBrick
- rsphandler
- URL rewriting with WEBrick

One-line web server in Ruby


# From: http://www.ntecs.de/blog/articles/2008/02/09/the-worlds-smallest-webserver
# Author: Michael Neumann
# ... point your browser to http://localhost:3125/etc/motd

ruby -rsocket -e 's=TCPServer.new(5**5);loop{_=s.accept;_<<"HTTP/1.0 200 OK\r\n\r\n#{File.read(_.gets.split[1])rescue nil}";_.close}'

gemdocs

Put this snippet into your ~/.bash_login (or alternatives) to browse your gem docs from the command line.


function gemdocs() { 

  if [[ -n "$(whereis gem_server)" ]]; then
     gem_server >/dev/null 2>&1 &
     sleep 3
     open http://127.0.0.1:8808/
  fi

}

pman -- create, print, save, view PDF man pages

pman -- create, print, save, view PDF man pages

Author: ntk
License: The MIT License, Copyright (c) 2007 ntk
Description: (batch) convert man pages into PDF documents and save them to a specified directory; (batch) print or view PDF man pages from the command line
Platform: Mac OS X 10.4.10; man bash
Installation: put pman() into ~/.bash_login (or alternatives)





# Usage:
   
pman ls; pman getopts                                 # convert a singel man page to a PDF file, save and open it
pman 8 sticky                                         # same with manual section number
pman m toe                                     
pman -b ls 'open(2)' dd "chmod(2)" curl 'open(n)'     # batch convert man pages into PDF files
pman -p rm srm open\(2\) 'toe(m)' 'ncurses(3)'        # print man pages using the default printer



pman() {

section="$1"    
manpage="$2"    

mandir="/Users/Shared/manpages"    #  save the created PDF man pages to the specified directory


# batch process man pages to PDF files with the "-b" switch and save them to $mandir
# example: pman -b ls 'open(2)' dd 'chmod(2)' 'open(n)' 'sticky(8)'
# cf. man -aW open for "man n open"

if [[ "$1" = "-b" ]]; then         

if [[ ! -d $mandir ]]; then        
   mkdir -p $mandir
   chmod 1777 $mandir
fi

shift   # remove "-b" from "$@"

for manfile in "$@"; do 

# example for $manfile: open(2)
manpage="`echo $manfile | grep -Eos '^[^\(]+'`"                              # extract name of man page
section="`echo $manfile | grep -Eos '\([^\)]+\)' | grep -Eos '[^\(\)]+'`"    # extract section of man page

if [[ ! "$section" ]]; then
   section="1"
fi

if [[ ! -f "`man ${section} -W ${manpage} 2>/dev/null`" ]]; then
#if [[ ! -f "`man -W ${section} ${manpage} 2>/dev/null `" ]]; then
   echo "No such man page: man ${section} ${manpage}"
   continue
fi

manfile="${mandir}/${manpage}(${section}).pdf"
echo "$manfile"

if [[ ! -f "$manfile" ]]; then
   man $section -t $manpage 2>/dev/null | pstopdf -i -o "$manfile" 2>/dev/null
   chmod 1755 "$manfile"
   # hide file extension .pdf
   if [[ -f /Developer/Tools/SetFile ]]; then /Developer/Tools/SetFile -a E "$manfile"; fi
fi

done

return 0
   
fi          # END of batch processing man pages to PDF files



# print PDF man pages using the default printer (see man lpr and man lpoptions)
# if necessary, create the specified PDF man pages and save them to $mandir
# example: pman -p rm srm

if [[ "$1" = "-p" ]]; then         

if [[ ! -d $mandir ]]; then        
   mkdir -p $mandir
   chmod 1777 $mandir
fi

shift   # remove "-p" from "$@"

for manfile in "$@"; do 

# example for $manfile: open(2)
manpage="`echo $manfile | grep -Eos '^[^\(]+'`"                              # extract name of man page
section="`echo $manfile | grep -Eos '\([^\)]+\)' | grep -Eos '[^\(\)]+'`"    # extract section of man page

if [[ ! "$section" ]]; then
   section="1"
fi

if [[ ! -f "`man ${section} -W ${manpage} 2>/dev/null`" ]]; then
   echo "No such man page: man ${section} ${manpage}"
   continue
fi

manfile="${mandir}/${manpage}(${section}).pdf"
echo "$manfile"

if [[ ! -f "$manfile" ]]; then
   man -t $section $manpage 2>/dev/null | pstopdf -i -o "$manfile" 2>/dev/null
   chmod 1755 "$manfile"
   # hide file extension .pdf
   if [[ -f /Developer/Tools/SetFile ]]; then /Developer/Tools/SetFile -a E "$manfile"; fi
   lpr "$manfile"
else
   lpr "$manfile"
fi

done

return 0
   
fi          # END of printing man pages using the default printer



# convert a single man page to a PDF file, save it to $mandir and then open it in a PDF viewer

if [[ -z "$1" ]] || [[ $# -gt 2 ]]; then       # check number of arguments
#if [[ -z "$1" || $# -gt 2 ]]; then  
#if [ -z "$1" -o $# -gt 2 ]; then
  echo "Wrong number of arguments!" 
  return 1
fi 

if [[ ! "$manpage" ]]; then     # turn "pman ls" into "pman 1 ls"
   manpage="$section"         # if $manpage is an empty string because there has been no "$2" then $manpage is set to "$section" and ...
   section="1"                # ... $section is set to "1"
fi                            

if [[ ! -f "`man ${section} -W ${manpage} 2>/dev/null`" ]]; then
   echo "No such man page: man ${section} ${manpage}"
   return 1
fi

if [[ ! -d $mandir ]]; then
   mkdir -p $mandir
   chmod 1777 $mandir
fi

manfile="${mandir}/${manpage}(${section}).pdf"

if [[ -f "$manfile" ]]; then
   open "$manfile"
else
   man $section -t $manpage 2>/dev/null | pstopdf -i -o "$manfile" 2>/dev/null
   chmod 1755 "$manfile"
   # hide file extension .pdf
   if [[ -f /Developer/Tools/SetFile ]]; then /Developer/Tools/SetFile -a E "$manfile"; fi
   open "$manfile"
fi

return 0

}



Setting up Ruby On Rails with PostgreSQL on Mac OS X 10.4.9

This is a modified version of the Ruby on Rails Tutorial (TutorialStepOne, TutorialStepOnePostgresql, ...).

Log in to an admin user account and, if necessary, fix your command search path in $HOME/.bash_profile, $HOME/.bash_login or $HOME/.profile to include "/usr/local" and "/usr/local/sbin" (cf. Using /usr/local > Set The Path; echo $PATH | tr ":" "\n"). If there are no such files, just create them: touch $HOME/.bash_login && touch $HOME/.bashrc (ls -a | grep \.bash).

To avoid RubyGems loading issues it's no bad idea to add export RUBYOPT=rubygems to your $HOME/.bash_login file as well.

If you want your system path changes to take effect not only for you you can modify the global system path settings in the systemwide initialization files /private/etc/profile and /private/etc/bashrc accordingly.

To fix the paths of installed manual pages add the lines "MANPATH /usr/local/share/man" and "MANPATH /usr/local/man" to sudo nano +45 /usr/share/misc/man.conf (man -w | tr ":" "\n").


(USE THE FOLLOWING AT YOUR OWN RISK!)


REQUIREMENTS:

I. Xcode


II. PostgreSQL Database Server

Install this PostgreSQL Database Server package.

# test after installation
which psql     # /usr/local/bin/psql
psql --version     # psql (PostgreSQL) 8.2.3, contains support for command-line editing


Alternative installation: Getting PostgreSQL running for Rails on a Mac


III. Ruby 1.8.6, Ruby On Rails 1.2.3 & Mongrel 1.0.1

Building Ruby, Rails, Subversion, Mongrel, and MySQL on Mac OS X

To install both Mongrel & Mongrel Cluster use: sudo gem install -y mongrel mongrel_cluster (cf. Using Mongrel Cluster).

# test after installation
ruby -v     # ruby 1.8.6
rails -v     # Rails 1.2.3
gem list     # ... mongrel (1.0.1) ...



IV. ruby-postgres 0.7.1
sudo gem install -y ruby-postgres



As an alternative you may try Ruby on rails installer script for Mac OSX or choose to install via MacPorts as described in Installing Ruby on Rails and PostgreSQL on OS X, Second Edition or Installing Rails on Mac OS X Tiger (10.4.8). However, you then may have to change your system paths mentioned above accordingly.


1. create a database server


# first create a directory called PostgreSQL-db on your Desktop
mkdir -p $HOME/Desktop/PostgreSQL-db

# create a new db server called railsdb
/usr/local/bin/initdb -E UTF8 -D $HOME/Desktop/PostgreSQL-db/railsdb

# START DB SERVER
dir="$HOME/Desktop/PostgreSQL-db"; /usr/local/bin/pg_ctl -D $dir/railsdb -l $dir/railsdb/postgres.log start

# STOP DB SERVER
#dir="$HOME/Desktop/PostgreSQL-db"; /usr/local/bin/pg_ctl -D $dir/railsdb -l $dir/railsdb/postgres.log stop -m smart

# check
cat $HOME/Desktop/PostgreSQL-db/railsdb/pg_hba.conf

   ...
   # TYPE  DATABASE    USER        CIDR-ADDRESS          METHOD

   # "local" is for Unix domain socket connections only
   local   all         all                               trust
   # IPv4 local connections:
   host    all         all         127.0.0.1/32          trust
   # IPv6 local connections:
   host    all         all         ::1/128               trust



2. create PostgreSQL database


createdb `whoami`_development
#dropdb `whoami`_development

createdb `whoami`_test
#dropdb `whoami`_test

#createdb `whoami`_production
#dropdb `whoami`_production



3. set up your Rails project


cd $HOME/Desktop/RubyOnRails-projects
rails -d postgresql `whoami`
cd `whoami`



4. edit /config/database.yml


open -e $HOME/Desktop/RubyOnRails-projects/`whoami`/config/database.yml

# just uncomment the following line
#encoding: UTF8 



5. make Rails & PostgreSQL work together


cd $HOME/Desktop/RubyOnRails-projects/`whoami`

ruby script/generate migration People

# edit /db/migrate/001_people.rb
# cf. http://wiki.rubyonrails.org/rails/pages/TutorialStepOneMigrations

open -e $HOME/Desktop/RubyOnRails-projects/`whoami`/db/migrate/001_people.rb

 class People < ActiveRecord::Migration

    def self.up
      create_table :people do |table|
      # note that "id" is added implicitly, by default
        table.column :name, :string
        table.column :street1, :string
        table.column :street2, :string
        table.column :city, :string
        table.column :state, :string
        table.column :zip, :string
     end
    end

    def self.down
      drop_table :people 
    end
  end



rake db:migrate

ruby script/generate model Person  

#open -e $HOME/Desktop/RubyOnRails-projects/`whoami`/app/models/person.rb  # file will be explained below

ruby script/console

>> ...
        entry = Person.new
        entry.name = "Name" 
        entry.street1 = "123 Somwhere" 
        entry.street2 = "" 
        entry.city = "Smallville" 
        entry.state = "KS" 
        entry.zip = "123456" 
        entry.save
        exit


# check newly created db table

psql `whoami`_development
SELECT * FROM people;
\q


# test
rake      # ... 0 failures, 0 errors

# create new controller
ruby script/generate controller People list view new edit


# edit /app/controllers/people_controller.rb
open -e $HOME/Desktop/RubyOnRails-projects/`whoami`/app/controllers/people_controller.rb

  def view
   @person = Person.find(1)
  end



# edit /app/views/people/view.rhtml
open -e $HOME/Desktop/RubyOnRails-projects/`whoami`/app/views/people/view.rhtml

# copy & paste & uncomment the following lines

#<html>
#  <body>
#    <h1>People#view</h1>
#    <p>This page will display one person.</p>
#    <p>
#    <%= @person.name %><br />
#    <%= @person.street1 %><br />
#    <%= @person.street2 %><br />
#    <%= @person.city %><br />
#    <%= @person.state %><br />
#    <%= @person.zip %><br />
#    </p>
#  </body>
#</html>


# the file /app/models/person.rb explained (see above)
# open -e $HOME/Desktop/RubyOnRails-projects/`whoami`/app/models/person.rb

# class Person < ActiveRecord::Base
# end

# How does this know to map to the people table we created? ActiveRecord pluralizes the class name and looks for that 
# table in the database. This doesn’t just mean adding an ’s’. Irregular plural forms are also handled, so Rails knows 
# that the plural of ‘person’ is ‘people’. The rules for how it does this are described in the documentation
# (see http://wiki.rubyonrails.org/rails/pages/TutorialStepSix).

# test
rake

# start your Rails app
cd $HOME/Desktop/RubyOnRails-projects/`whoami`

ruby script/server
#ruby script/server --environment=development

# open a second shell window and go to ...  
open -a Safari http://localhost:3000/people/view


Command-line progress indicators

... just some proof-of-concept snippets ...

inspired by: http://www.ruby-forum.com/topic/87404



# move cursor to beginning of line
cr = "\r"           


# ANSI escape code to clear line from cursor to end of line
# "\e" is an alternative to "\033"
# cf. http://en.wikipedia.org/wiki/ANSI_escape_code

clear = "\e[0K"     

# reset lines
reset = cr + clear



#-------------------------------- Example 1 --------------------------------


(1..100).each do |i| 
  print "#{reset}#{i}%"
  sleep(0.08)
  $stdout.flush
end

print "#{reset}"     # clear current line

$stdout.flush
puts "done"



#-------------------------------- Example 2 --------------------------------


chars = [ "|", "/", "-", "\\" ]

# 7 turns on reverse video mode, 31 red , ...
n = 31

str = "#{reset}\e[#{n};1m"   


(1..100).each do |i| 

   case i
      when   0..10    then print "#{str}#{chars[0]}"
      when  10..20    then print "#{str}#{chars[1]}"
      when  20..30    then print "#{str}#{chars[2]}"
      when  30..40    then print "#{str}#{chars[3]}"
      when  40..50    then print "#{str}#{chars[0]}"
      when  50..60    then print "#{str}#{chars[1]}"
      when  60..70    then print "#{str}#{chars[2]}"
      when  70..80    then print "#{str}#{chars[3]}"
      when  80..90    then print "#{str}#{chars[0]}"
      when  90..100   then print "#{str}#{chars[1]}"
   end

   sleep(0.1)
   $stdout.flush

end

print "\e[0m"
print "#{reset}" 

$stdout.flush
puts "done"



#-------------------------------- Example 3 --------------------------------


MAX = 80 

$stdout.sync = true     # alternative to $stdout.flush below

10.times do
   foo_string = Time.now.to_s
   s = foo_string[0..MAX].center(MAX)   # or rjust or ljust 
   print cr + s 
   #$stdout.flush 
   sleep(1.1)
end

print "\e[0m" 
print "#{reset}" 

$stdout.flush
puts "done"


« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS