1
0
Fork 0
mirror of https://github.com/sinatra/sinatra synced 2023-03-27 23:18:01 -04:00

* README w/MIT

* Log to file
* Tail log file
* Some refactoring
This commit is contained in:
blake.mizerany@gmail.com 2007-09-11 01:14:00 +00:00
parent 2333cf2515
commit 208158a7da
7 changed files with 150 additions and 11 deletions

43
README Normal file
View file

@ -0,0 +1,43 @@
Copyright (c) <year> <copyright holders>
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
Using Sinatra is EASY!
WARNING: Keep a fresh pair of underwear nearby. You may need them when you see this.
Get running without a file!
ruby -e 'require "sinatra"' # HIT http://localhost:4567/ and BAM!
now create a file called test.rb (or whatever you fancy) and enter this:
require 'sinatra'
get '/' do
body 'Hello World!'
end
now run 'ruby test.rb' and refresh your browser
Oh yeah!

View file

@ -1,3 +1,26 @@
# Copyright (c) 2007 Blake Mizerany
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
%w(rubygems rack).each do |library|
begin
require library
@ -14,16 +37,6 @@ Sinatra::Loader.load_files Dir.glob(SINATRA_ROOT + '/lib/sinatra/core_ext/*.rb')
Sinatra::Loader.load_files Dir.glob(SINATRA_ROOT + '/lib/sinatra/*.rb')
Sinatra::Loader.load_files Dir.glob(SINATRA_ROOT + '/vendor/*/init.rb')
SINATRA_LOGGER = Sinatra::Logger.new(STDOUT)
def set_logger(logger = SINATRA_LOGGER)
[Sinatra::Server, Sinatra::EventContext, Sinatra::Event, Sinatra::Dispatcher].each do |klass|
klass.logger = logger
end
end
set_logger
at_exit do
Sinatra::Server.new.start unless Sinatra::Server.running
end

View file

@ -45,4 +45,5 @@ class Class # :nodoc:
cattr_reader(*syms)
cattr_writer(*syms)
end
end

View file

@ -0,0 +1,11 @@
class Module
def attr_with_default(sym, default)
define_method "#{sym}=" do |obj|
instance_variable_set("@#{sym}", obj)
end
define_method sym do
instance_variable_get("@#{sym}") || default
end
end
end

View file

@ -9,6 +9,7 @@ module Sinatra
%w(info debug error warn).each do |n|
define_method n do |message|
@stream.puts message
@stream.flush
end
end

29
lib/sinatra/options.rb Normal file
View file

@ -0,0 +1,29 @@
require 'optparse'
module Sinatra
module Options
extend self
attr_with_default :port, 4567
attr_with_default :environment, :development
def parse!(args)
OptionParser.new do |opts|
opts.on '-p port', '--port port', 'Set the port (default is 4567)' do |port|
@port = port
end
opts.on '-e environment', 'Set the environment (default if development)' do |env|
@environment = env
end
opts.on '-h', '--help', '-?', 'Show this message' do
puts opts
exit!
end
end.parse!(ARGV)
end
def log_file
File.dirname($0) + ('/%s.log' % environment)
end
end
end

View file

@ -1,3 +1,5 @@
require 'fileutils'
module Sinatra
class Server
@ -7,7 +9,9 @@ module Sinatra
def start
begin
Rack::Handler::Mongrel.run(Sinatra::Dispatcher.new, :Port => 4567) do |server|
setup_environment
tail_thread = tail(Options.log_file)
Rack::Handler::Mongrel.run(Dispatcher.new, :Port => Options.port) do |server|
logger.info "== Sinatra has taken the stage on port #{server.port}!"
trap("INT") do
server.stop
@ -18,9 +22,46 @@ module Sinatra
self.class.running = true
rescue => e
logger.exception e
ensure
tail_thread.kill if tail_thread
end
end
private
def setup_environment
Options.parse!(ARGV)
set_loggers
end
def set_loggers
logger = Logger.new(open(Options.log_file, 'w'))
[Server, EventContext, Event, Dispatcher].each do |klass|
klass.logger = logger
end
end
def tail(log_file)
FileUtils.touch(log_file)
cursor = File.size(log_file)
last_checked = Time.now
tail_thread = Thread.new do
File.open(log_file, 'r') do |f|
loop do
f.seek cursor
if f.mtime > last_checked
last_checked = f.mtime
contents = f.read
cursor += contents.length
print contents
end
sleep 1
end
end
end
tail_thread
end
end
end