Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- ###
- # This program runs a webserver that can have resouces dynamically deployed to it.
- # The idea is that it can be used to abstract resource deployment from the parent process.
- # This program takes the following arguments:
- #
- # - Port (default: 5678)
- # - Base Directory (default: current directory)
- # - Public Directory (default: current directory)
- # - Logging (default: false)
- #
- require 'rubygems'
- require 'haml'
- require 'optparse'
- require 'fileutils'
- @options = Hash.new
- ### Defaults
- #
- @options[:port] = 5678
- @options[:base] = FileUtils::pwd
- @options[:public] = FileUtils::pwd
- OptionParser.new { |opts|
- opts.on "--port PORT", "Server port" do |port|
- puts "Port detected! [#{port}]"
- @options[:port] = port.to_i
- end
- opts.on "--base DIRECTORY", "Base directory for server" do |dir|
- raise "Depricated"
- puts "Base directory detected! [#{dir}]"
- raise "[#{base}] is not a directory" unless File::directory? dir
- @options[:base] = dir
- end
- opts.on "--public DIRECTORY", "Public resources directory for server" do |dir|
- puts "Public directory detected! [#{dir}]"
- raise "[#{public}] is not a directory" unless File::directory? dir
- @options[:public] = dir
- end
- opts.on "--logging BOOLEAN", "Turn logging on or off (default off)" do |logging|
- puts "Logging flag detected! [#{logging}]"
- raise "--logging flag must be 'true' or 'false'." unless logging =~ /true|false/
- @options[:logging] = logging =~ /true/i
- end
- }.parse!
- require 'sinatra'
- disable :logging unless @options[:logging]
- ### Activate the options
- #
- set :root, File::expand_path(@options[:base])
- set :public, File::expand_path(@options[:public])
- puts "set :public, File::expand_path(\"#{@options[:public]}\")"
- set :port, @options[:port]
- ###
- # Exit resource:
- # Provides a way to terminate the server gracefully.
- #
- get "/exit" do
- ###
- # The actual server termination is delegated to a seperate thread,
- # so that the requested page can first return a string to the client.
- #
- Thread.new do
- sleep 1
- exit!
- end
- "Server halted!\n"
- end
- ### ###
- ### # Listing resource:
- ### # Provides a listing for the requested directory.
- ### # (Mainly used for debugging purposes)
- ### #
- ### get "/*" do
- ### path = params[:splat].first
- ### #path = File::join root, path
- ### path = '.' if path.empty?
- ### if File::directory? path
- ### set = Dir[File::join(path, '*')]
- ### directories, files = set.partition {|e| File::directory? e}
- ### paths = directories.sort + files.sort
- ### haml :directoryListing, :locals => {:list => paths, :current_directory => path}
- ### elsif File::file? path
- ### ### A bit messy, but good for debugging
- ### #
- ### File::read path
- ### else
- ### raise "File not found [#{path}]"
- ### end
- ### end
- template :directoryListing do
- <<-"EOF"
- %h1= current_directory
- %ul
- - list.each do |item|
- %li
- %a{:href => item}= File::directory?(item) ? item + '/' : item
- EOF
- end
Add Comment
Please, Sign In to add comment