Guest User

Untitled

a guest
Jun 21st, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. ###
  4. # This program runs a webserver that can have resouces dynamically deployed to it.
  5. # The idea is that it can be used to abstract resource deployment from the parent process.
  6. # This program takes the following arguments:
  7. #
  8. # - Port (default: 5678)
  9. # - Base Directory (default: current directory)
  10. # - Public Directory (default: current directory)
  11. # - Logging (default: false)
  12. #
  13.  
  14. require 'rubygems'
  15. require 'haml'
  16. require 'optparse'
  17. require 'fileutils'
  18.  
  19. @options = Hash.new
  20.  
  21. ### Defaults
  22. #
  23. @options[:port] = 5678
  24. @options[:base] = FileUtils::pwd
  25. @options[:public] = FileUtils::pwd
  26.  
  27. OptionParser.new { |opts|
  28.  
  29. opts.on "--port PORT", "Server port" do |port|
  30. puts "Port detected! [#{port}]"
  31. @options[:port] = port.to_i
  32. end
  33.  
  34. opts.on "--base DIRECTORY", "Base directory for server" do |dir|
  35. raise "Depricated"
  36. puts "Base directory detected! [#{dir}]"
  37. raise "[#{base}] is not a directory" unless File::directory? dir
  38. @options[:base] = dir
  39. end
  40.  
  41. opts.on "--public DIRECTORY", "Public resources directory for server" do |dir|
  42. puts "Public directory detected! [#{dir}]"
  43. raise "[#{public}] is not a directory" unless File::directory? dir
  44. @options[:public] = dir
  45. end
  46.  
  47. opts.on "--logging BOOLEAN", "Turn logging on or off (default off)" do |logging|
  48. puts "Logging flag detected! [#{logging}]"
  49. raise "--logging flag must be 'true' or 'false'." unless logging =~ /true|false/
  50. @options[:logging] = logging =~ /true/i
  51. end
  52.  
  53. }.parse!
  54.  
  55. require 'sinatra'
  56.  
  57. disable :logging unless @options[:logging]
  58.  
  59. ### Activate the options
  60. #
  61. set :root, File::expand_path(@options[:base])
  62. set :public, File::expand_path(@options[:public])
  63. puts "set :public, File::expand_path(\"#{@options[:public]}\")"
  64. set :port, @options[:port]
  65.  
  66. ###
  67. # Exit resource:
  68. # Provides a way to terminate the server gracefully.
  69. #
  70. get "/exit" do
  71. ###
  72. # The actual server termination is delegated to a seperate thread,
  73. # so that the requested page can first return a string to the client.
  74. #
  75. Thread.new do
  76. sleep 1
  77. exit!
  78. end
  79.  
  80. "Server halted!\n"
  81. end
  82.  
  83. ### ###
  84. ### # Listing resource:
  85. ### # Provides a listing for the requested directory.
  86. ### # (Mainly used for debugging purposes)
  87. ### #
  88. ### get "/*" do
  89. ### path = params[:splat].first
  90. ### #path = File::join root, path
  91. ### path = '.' if path.empty?
  92. ### if File::directory? path
  93. ### set = Dir[File::join(path, '*')]
  94. ### directories, files = set.partition {|e| File::directory? e}
  95. ### paths = directories.sort + files.sort
  96. ### haml :directoryListing, :locals => {:list => paths, :current_directory => path}
  97. ### elsif File::file? path
  98. ### ### A bit messy, but good for debugging
  99. ### #
  100. ### File::read path
  101. ### else
  102. ### raise "File not found [#{path}]"
  103. ### end
  104. ### end
  105.  
  106. template :directoryListing do
  107. <<-"EOF"
  108. %h1= current_directory
  109. %ul
  110. - list.each do |item|
  111. %li
  112. %a{:href => item}= File::directory?(item) ? item + '/' : item
  113. EOF
  114. end
Add Comment
Please, Sign In to add comment