Class: StaticMatic::Server
- Inherits:
-
Mongrel::HttpHandler
- Object
- Mongrel::HttpHandler
- StaticMatic::Server
- Defined in:
- lib/staticmatic/server.rb
Constant Summary collapse
- @@file_only_methods =
["GET","HEAD"]
Class Method Summary collapse
-
.start(staticmatic) ⇒ Object
Starts the StaticMatic preview server.
Instance Method Summary collapse
- #expand_path(path_info) ⇒ Object
-
#initialize(staticmatic) ⇒ Server
constructor
A new instance of Server.
- #process(request, response) ⇒ Object
Constructor Details
#initialize(staticmatic) ⇒ Server
Returns a new instance of Server.
5 6 7 8 |
# File 'lib/staticmatic/server.rb', line 5 def initialize(staticmatic) @files = Mongrel::DirHandler.new(staticmatic.site_dir, false) @staticmatic = staticmatic end |
Class Method Details
.start(staticmatic) ⇒ Object
Starts the StaticMatic preview server
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/staticmatic/server.rb', line 77 def start(staticmatic) port = staticmatic.configuration.preview_server_port || 3000 host = staticmatic.configuration.preview_server_host || "" config = Mongrel::Configurator.new :host => host do puts "Running Preview of #{staticmatic.base_dir} on #{host}:#{port}" listener :port => port do uri "/", :handler => Server.new(staticmatic) end trap("INT") { stop } run end config.join end |
Instance Method Details
#expand_path(path_info) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/staticmatic/server.rb', line 56 def (path_info) dirname, basename = File.split(path_info) extname = File.extname(path_info).sub(/^\./, '') filename = basename.chomp(".#{extname}") if extname.empty? dir = File.join(dirname, filename) is_dir = path_info[-1, 1] == '/' || (@staticmatic.template_directory?(dir) && !@staticmatic.template_exists?(filename, dirname)) if is_dir dirname = dir filename = 'index' end extname = 'html' end [ dirname, filename, extname ] end |
#process(request, response) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/staticmatic/server.rb', line 10 def process(request, response) @staticmatic.load_helpers path_info = request.params[Mongrel::Const::PATH_INFO] get_or_head = @@file_only_methods.include? request.params[Mongrel::Const::REQUEST_METHOD] file_dir, file_name, file_ext = (path_info) # remove stylesheets/ directory if applicable file_dir.gsub!(/^\/stylesheets\/?/, "") file_dir = CGI::unescape(file_dir) file_name = CGI::unescape(file_name) if file_ext && file_ext.match(/html|css/) response.start(200) do |head, out| head["Content-Type"] = "text/#{file_ext}" output = "" if @staticmatic.template_exists?(file_name, file_dir) && !(File.basename(file_name) =~ /^\_/) begin if file_ext == "css" output = @staticmatic.generate_css(file_name, file_dir) else output = @staticmatic.generate_html_with_layout(file_name, file_dir) end rescue StaticMatic::Error => e output = e. end else if @files.can_serve(path_info) @files.process(request,response) else output = "File not Found" end end out.write output end else # try to serve static file from site dir if @files.can_serve(path_info) @files.process(request,response) end end end |