benofsky-bolt 0.3.3 → 0.4

Sign up to get free protection for your applications and to get access to all the features.
data/bin/bolt CHANGED
@@ -8,11 +8,18 @@
8
8
  # Copyright 2010 Ben McRedmond. All rights reserved.
9
9
  #
10
10
 
11
- begin
12
- require 'bolt'
13
- rescue LoadError
11
+ unless ENV['BOLT_DEV'] == "true"
12
+ begin
13
+ require 'bolt'
14
+ rescue LoadError
15
+ require 'rubygems'
16
+ require 'bolt'
17
+ end
18
+ else
19
+ base = File.expand_path(File.dirname(File.dirname(__FILE__)))
20
+ $:.push("#{base}/lib/")
21
+ require 'bolt.rb'
14
22
  require 'rubygems'
15
- require 'bolt'
16
23
  end
17
24
 
18
25
  b = Bolt::Bolt.new
data/lib/bolt/page.rb CHANGED
@@ -15,7 +15,8 @@ def render(view, options = {})
15
15
  options[:template] = $config_file['primary_template'] if options[:template].nil?
16
16
  options[:engine] ||= "haml"
17
17
 
18
- raise ArgumentError, ":engine cannot be view" if options[:engine] == "view"
18
+ raise ArgumentError, ":engine cannot be view" if options[:engine] == "view"
19
+
19
20
  require options[:engine_require] || options[:engine]
20
21
 
21
22
  @content = render_view(view_as_string(view, options[:engine]), options[:engine])
data/lib/bolt/serve.rb ADDED
@@ -0,0 +1,145 @@
1
+ #
2
+ # serve.rb
3
+ # bolt
4
+ #
5
+ # Created by Ben McRedmond on 2010-06-06.
6
+ # Copyright 2010 Ben McRedmond. All rights reserved.
7
+ #
8
+
9
+ require 'socket'
10
+ require 'time'
11
+ require 'drb/drb'
12
+
13
+ require 'bolt/build'
14
+
15
+ class File
16
+ def content_type
17
+ mime = {'.jpeg' => 'image/jpeg', '.jpg' => 'image/jpeg',
18
+ '.png' => 'image/png', '.gif' => 'image/gif',
19
+ '.mp3' => 'audio/mpeg3', '.aif' => 'audio/aiff',
20
+ '.mid' => 'audio/midi', '.wav' => 'audio/wav',
21
+ '.m4a' => 'audio/MP4A-LATM', '.3gp' => 'video/3gpp',
22
+ '.dv' => 'video/DV', '.mp4' => 'video/H264',
23
+ '.pdf' => 'application/pdf', '.js' => 'application/js',
24
+ '.json' => 'application/json', '.xml' => 'text/xml',
25
+ '.html' => 'text/html', '.htm' => 'text/htm',
26
+ '.css' => 'text/css', '.txt' => 'text/plain'}
27
+ mime[File.extname(self.path)] || 'application/octet-stream'
28
+ end
29
+
30
+ def to_s
31
+ s = ""
32
+ self.each {|line| s += line}
33
+ s
34
+ end
35
+ end
36
+
37
+ module Bolt
38
+ class DRbHash < Hash
39
+ include DRbUndumped
40
+ end
41
+
42
+ class Serve < Build
43
+ attr_accessor :pages
44
+
45
+ def initialize
46
+ super
47
+ @pages = DRbHash.new
48
+
49
+ DRb.start_service(nil, self)
50
+ $drb_uri = DRb.uri
51
+
52
+ @server = HTTPServer.new(:host => $config.serve_host, :port => $config.serve_port)
53
+
54
+ @errors_base = File.expand_path(File.dirname(File.dirname(__FILE__))) + '/bolt/serve_errors/'
55
+ trap("INT") { exit! }
56
+ end
57
+
58
+ def run
59
+ # This has to be loaded after the DRb server is started above
60
+ load 'bolt/serve_page.rb'
61
+
62
+ begin
63
+ while(request = @server.listen)
64
+ # Bit of magic here (eek)
65
+ # Basically the parent class Build loads each page in the pages directory
66
+ # for whatever project we're in, however the bolt/serve_page require up above
67
+ # overrides the standard page method used in these pages to instead send us
68
+ # the block used to generate the page, so basically @pages is full of
69
+ # url => block references.
70
+ load_pages
71
+ parse_config
72
+
73
+ page = @pages[request['GET'].gsub(/\.html/,'')[1..-1]]
74
+
75
+ if(!page.nil?)
76
+ @server.reply(page.call)
77
+ elsif(File.exists?(d($config.resources) + request['GET']))
78
+ f = File.new(d($config.resources) + request['GET'])
79
+ @server.reply(f.to_s, 200, 'Content-Type' => f.content_type)
80
+ else
81
+ @server.reply(File.new(@errors_base + '404.html').to_s, 404)
82
+ end
83
+ end
84
+ rescue Exception => e
85
+ puts e
86
+ @server.reply(File.new(@errors_base + '500.html').to_s, 500)
87
+ retry
88
+ end
89
+
90
+ DRb.thread.join
91
+ end
92
+
93
+ end
94
+
95
+ class HTTPServer
96
+ def initialize(options={})
97
+ options[:host] ||= '127.0.0.1'
98
+ options[:port] ||= '2658'
99
+
100
+ @server = TCPServer.new(options[:host], options[:port])
101
+ puts "## Starting BoltServe on http://#{options[:host]}:#{options[:port]} ## \n\n"
102
+ end
103
+
104
+ def listen()
105
+ @session = @server.accept
106
+ @request = {}
107
+ @request['headers'] = {}
108
+
109
+ while(header = @session.gets)
110
+ if(header == "\r\n")
111
+ break
112
+ elsif(!header.scan(/GET .* HTTP.*/).empty?)
113
+ @request['GET'] = header.gsub(/GET /, '').gsub(/ HTTP.*/, '').strip
114
+ else
115
+ h = header.split(':')
116
+ @request['headers'][h[0]] = h[1].strip unless h[1].nil?
117
+ end
118
+ end
119
+
120
+ puts "Request from: #{@session.addr[2]} \n\tfor: #{@request['GET']}\n\n"
121
+ @request
122
+ end
123
+
124
+ def reply(body, status = '200', headers={})
125
+ headers['Content-Length'] ||= body.length
126
+ headers['Connection'] ||= 'close'
127
+ headers['Date'] ||= Time.now.getgm
128
+ headers['Last-Modified'] ||= headers['Date']
129
+ headers['Content-Type'] ||= 'text/html; charset=UTF-8'
130
+
131
+ headers['Server'] = 'Bolt Development Server'
132
+
133
+ @session.print("HTTP/1.1 #{status}\n")
134
+ headers.each do |header, value|
135
+ @session.print("#{header}: #{value}\n")
136
+ end
137
+
138
+ @session.print("\n")
139
+ @session.print(body)
140
+ @session.print("\r\n")
141
+
142
+ @session.close()
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,58 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3
+
4
+ <html>
5
+ <head>
6
+ <meta http-equiv="Content-type" content="text/html; charset=utf-8">
7
+ <title>404 - Page Not found</title>
8
+ <style type="text/css">
9
+ html, body {
10
+ padding: 0;
11
+ margin: 0;
12
+ }
13
+
14
+ body {
15
+ background-color: #fff;
16
+ border-top: 2px solid red;
17
+ font-family: Georgia, serif;
18
+ font-size: 12px;
19
+ }
20
+
21
+ h1 {
22
+ text-shadow: #ddd 1px 2px 0px;
23
+ text-align: center;
24
+ color: #000;
25
+ font-size: 25px;
26
+ font-family: Helvetica;
27
+ }
28
+
29
+ p, ul {
30
+ line-height: 18px;
31
+ }
32
+
33
+ ul li {
34
+ margin-bottom: 5px;
35
+ }
36
+
37
+ #container {
38
+ width: 350px;
39
+ margin: 100px auto;
40
+ }
41
+ </style>
42
+ </head>
43
+
44
+ <body>
45
+ <div id="container">
46
+ <h1>Page Not Found</h1>
47
+ <p>
48
+ Sorry BoltServe couldn't find the page you requested, a few suggestions:
49
+ <ul>
50
+ <li>Check the console for errors.</li>
51
+ <li>Check for typos in the <em>page</em> block.</li>
52
+ <li>Check that you copied the file you want into the resources directory (if it's not a page) and that it has correct permissions for BoltServe to access it.</li>
53
+ </ul>
54
+ Hope that helps and sorry if it doesn't!
55
+ </p>
56
+ </div>
57
+ </body>
58
+ </html>
@@ -0,0 +1,57 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3
+
4
+ <html>
5
+ <head>
6
+ <meta http-equiv="Content-type" content="text/html; charset=utf-8">
7
+ <title>500 - Server Error</title>
8
+ <style type="text/css">
9
+ html, body {
10
+ padding: 0;
11
+ margin: 0;
12
+ }
13
+
14
+ body {
15
+ background-color: #fff;
16
+ border-top: 2px solid red;
17
+ font-family: Georgia, serif;
18
+ font-size: 12px;
19
+ }
20
+
21
+ h1 {
22
+ text-shadow: #ddd 1px 2px 0px;
23
+ text-align: center;
24
+ color: #000;
25
+ font-size: 25px;
26
+ font-family: Helvetica;
27
+ }
28
+
29
+ p, ul {
30
+ line-height: 18px;
31
+ }
32
+
33
+ ul li {
34
+ margin-bottom: 5px;
35
+ }
36
+
37
+ #container {
38
+ width: 350px;
39
+ margin: 100px auto;
40
+ }
41
+ </style>
42
+ </head>
43
+
44
+ <body>
45
+ <div id="container">
46
+ <h1>Server Error</h1>
47
+ <p>
48
+ Sorry BoltServe couldn't generate the page you requested, something is wrong. Some suggestions:
49
+ <ul>
50
+ <li>Check the console for errors, there will be one there.</li>
51
+ <li>Check for typos in your pages.</li>
52
+ </ul>
53
+ Hope that helps and sorry if it doesn't!
54
+ </p>
55
+ </div>
56
+ </body>
57
+ </html>
@@ -0,0 +1,9 @@
1
+ require 'drb'
2
+
3
+ DRb.start_service()
4
+ @serve_instance = DRbObject.new(nil, $drb_uri)
5
+
6
+ def page(page, &block)
7
+ page = "" if page == "index"
8
+ @serve_instance.pages[page] = block
9
+ end
data/lib/bolt.rb CHANGED
@@ -14,7 +14,8 @@ module Bolt
14
14
  def initialize
15
15
  $config = OpenStruct.new
16
16
  @commands = {"create" => true,
17
- "build" => true}
17
+ "build" => true,
18
+ "serve" => true}
18
19
  end
19
20
 
20
21
  # Parses command line options then runs the specified command
@@ -42,6 +43,11 @@ module Bolt
42
43
  require 'bolt/build'
43
44
  Build.new.run
44
45
  end
46
+
47
+ def serve
48
+ require 'bolt/serve'
49
+ Serve.new.run
50
+ end
45
51
 
46
52
  # Parses command line options
47
53
  def parse_options
@@ -51,9 +57,11 @@ module Bolt
51
57
  $config.out = "out"
52
58
  $config.pages = "pages"
53
59
  $config.config = "config.yml"
60
+ $config.serve_host = "127.0.0.1"
61
+ $config.serve_port = "2658"
54
62
 
55
63
  opts = OptionParser.new do |opts|
56
- opts.banner = "Usage: bolt {create/build} [options] [file]"
64
+ opts.banner = "Usage: bolt {create/build/serve} [options] [file]"
57
65
 
58
66
  opts.on("-r", "--resources [resource-dir]", "Resources directory") do |opts|
59
67
  $config.resources = opts
@@ -78,6 +86,14 @@ module Bolt
78
86
  opts.on("-c", "--config [config-file.yml]", "Config file") do |opts|
79
87
  $config.config = opts
80
88
  end
89
+
90
+ opts.on("-H", "--host [host]", "Serve development server host") do |opts|
91
+ $config.serve_host = opts
92
+ end
93
+
94
+ opts.on("-P", "--port [port]", "Serve development server port") do |opts|
95
+ $config.serve_port = opts
96
+ end
81
97
 
82
98
  opts.on_tail("-h", "--help", "Show this help message") do
83
99
  puts opts
metadata CHANGED
@@ -4,9 +4,8 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 3
8
- - 3
9
- version: 0.3.3
7
+ - 4
8
+ version: "0.4"
10
9
  platform: ruby
11
10
  authors:
12
11
  - Ben McRedmond
@@ -15,7 +14,7 @@ bindir:
15
14
  - bin
16
15
  cert_chain: []
17
16
 
18
- date: 2010-05-02 00:00:00 +01:00
17
+ date: 2010-06-12 00:00:00 +01:00
19
18
  default_executable:
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
@@ -44,8 +43,12 @@ files:
44
43
  - lib/bolt/build.rb
45
44
  - lib/bolt/page.rb
46
45
  - lib/bolt/project.rb
46
+ - lib/bolt/serve.rb
47
+ - lib/bolt/serve_page.rb
47
48
  - lib/bolt/view.rb
48
49
  - lib/bolt.rb
50
+ - lib/bolt/serve_errors/404.html
51
+ - lib/bolt/serve_errors/500.html
49
52
  - bin/bolt
50
53
  - LICENSE
51
54
  - default_files/application.haml