monorail 0.0.1 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,149 @@
1
+ # $Id: appgen.rb 2406 2006-04-28 16:36:00Z francis $
2
+ #
3
+ # Monorail::Application
4
+ # This runs a monorail system from a command-line binary.
5
+ # The binary is generated by rake and invokes us here.
6
+ #
7
+ #--
8
+ # This program is free software; you can redistribute it and/or modify
9
+ # it under the terms of the GNU General Public License as published by
10
+ # the Free Software Foundation; either version 2 of the License, or
11
+ # (at your option) any later version.
12
+ #
13
+ # This program is free software; you can redistribute it and/or modify
14
+ # it under the terms of the GNU General Public License as published by
15
+ # the Free Software Foundation; either version 2 of the License, or
16
+ # (at your option) any later version.
17
+ #
18
+ # This program is distributed in the hope that it will be useful,
19
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
20
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
+ # GNU General Public License for more details.
22
+ #
23
+ # You should have received a copy of the GNU General Public License
24
+ # along with this program; if not, write to the Free Software
25
+ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26
+ #
27
+
28
+
29
+
30
+
31
+ require 'ostruct'
32
+ require 'optparse'
33
+ require 'pathname'
34
+
35
+
36
+ module Monorail
37
+
38
+
39
+ class ApplicationGenerator
40
+
41
+
42
+ def initialize
43
+ end
44
+
45
+
46
+ def run
47
+ parse_arguments
48
+ generate_directory
49
+ end
50
+
51
+
52
+ # nodoc:
53
+ # This hardcodes the monorail directory structure.
54
+ # Obviously not satisfactory. We'll need some softer way of doing this
55
+ # eventually.
56
+ # WE ARE ASSUMING the directory structure of the gem.
57
+ # We expect to be in directory lib/monorail, and we look to copy the
58
+ # directory found at ../../skel relative to ourself.
59
+ #
60
+ def generate_directory
61
+ target = File.expand_path( @options.dir )
62
+ puts "Creating monorail app in directory: #{target}"
63
+
64
+ if (@options.erase)
65
+ r = Readline.readline( "*** Erase the target directory [yN]? " )
66
+ unless r =~ /\Ay/i
67
+ puts "Exiting..."
68
+ exit
69
+ end
70
+ FileUtils.rm_rf target
71
+ end
72
+
73
+ skel = File.dirname( __FILE__ ) + "/../../skel/."
74
+ FileUtils.cp_r( skel, target )
75
+
76
+ File.open( target + "/monorail_config.rb", "w") {|f|
77
+ f.puts "# Monorail Config file"
78
+ f.puts "# Generated at #{Time.now}"
79
+ f.puts "#"
80
+ f.puts
81
+
82
+ f.puts <<EOF
83
+ Monorail::Request::directory_alias "/public", "#{target}/public", :directory, false
84
+ Monorail::Request::directory_alias "/", "#{target}/c", :monorail, false
85
+
86
+ Monorail::Request::set_default_pathinfo "/public/index.html"
87
+
88
+ Monorail::Request::use_sessions true
89
+
90
+ Monorail::Request::verbose true
91
+ Monorail::Request::debug true
92
+ EOF
93
+
94
+ puts "Done."
95
+ puts "To see your new app:"
96
+ puts " Type: monorail server #{(@options.dir == ".") ? "" : "-d #{@options.dir}"}"
97
+ puts " Browse to: http://127.0.0.1:8900"
98
+ }
99
+ end
100
+
101
+ def parse_arguments
102
+ @options = OpenStruct.new
103
+ @options.dir = '.'
104
+ @options.erase = false
105
+
106
+ opts = OptionParser.new {|opts|
107
+ opts.banner = "Usage: monorail application [options]"
108
+
109
+ opts.separator ""
110
+ opts.separator "Options summary:"
111
+
112
+ opts.on("-d", "--dir [DIR]", "generate a monorail application in directory",
113
+ " default: the current directory") do |d|
114
+ @options.dir = d
115
+ end
116
+
117
+ opts.on("--[no-]erase", "erase the target directory before copying files",
118
+ " default: do not erase") {|erase|
119
+ @options.erase = erase
120
+ }
121
+
122
+ # Options summary
123
+ opts.on_tail("-h", "--help", "Show this message") {
124
+ puts opts
125
+ exit
126
+ }
127
+
128
+ # Version
129
+ opts.on_tail("--version", "Show version") {
130
+ puts Version
131
+ exit
132
+ }
133
+
134
+
135
+ }
136
+
137
+ opts.parse!(ARGV)
138
+
139
+ end
140
+
141
+
142
+ end # class ApplicationGenerator
143
+
144
+
145
+ end # module Monorail
146
+
147
+
148
+ #----------------------
149
+
@@ -0,0 +1,147 @@
1
+ # $Id: congen.rb 2406 2006-04-28 16:36:00Z francis $
2
+ #
3
+ # Monorail::Generator
4
+ # This invokes a command-line script to generate a monorail application shell.
5
+ # The binary is generated by rake and invokes us here.
6
+ #
7
+ #--
8
+ # This program is free software; you can redistribute it and/or modify
9
+ # it under the terms of the GNU General Public License as published by
10
+ # the Free Software Foundation; either version 2 of the License, or
11
+ # (at your option) any later version.
12
+ #
13
+ # This program is free software; you can redistribute it and/or modify
14
+ # it under the terms of the GNU General Public License as published by
15
+ # the Free Software Foundation; either version 2 of the License, or
16
+ # (at your option) any later version.
17
+ #
18
+ # This program is distributed in the hope that it will be useful,
19
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
20
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
+ # GNU General Public License for more details.
22
+ #
23
+ # You should have received a copy of the GNU General Public License
24
+ # along with this program; if not, write to the Free Software
25
+ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26
+ #
27
+
28
+
29
+ require 'fileutils'
30
+ require 'readline'
31
+
32
+
33
+ module Monorail
34
+
35
+
36
+ class ControllerGenerator
37
+
38
+
39
+ def initialize
40
+ end
41
+
42
+
43
+ def run
44
+ parse_arguments
45
+ generate_controller
46
+ end
47
+
48
+
49
+ def parse_arguments
50
+ @options = OpenStruct.new
51
+ @options.dir = '.'
52
+ @options.erase = false
53
+ @options.controller = nil
54
+
55
+ opts = OptionParser.new {|opts|
56
+ opts.banner = "Usage: monorail controller [options] [controller-name]"
57
+
58
+ opts.separator ""
59
+ opts.separator "Options summary:"
60
+
61
+ opts.on("-d", "--dir [DIR]", "generate a monorail controller in directory",
62
+ " default: the current directory") do |d|
63
+ @options.dir = d
64
+ end
65
+
66
+ # Options summary
67
+ opts.on_tail("-h", "--help", "Show this message") {
68
+ puts opts
69
+ exit
70
+ }
71
+
72
+ # Version
73
+ opts.on_tail("--version", "Show version") {
74
+ puts Version
75
+ exit
76
+ }
77
+
78
+
79
+ }
80
+
81
+ opts.parse!(ARGV)
82
+ @options.controller = ARGV.shift
83
+
84
+
85
+ end
86
+
87
+
88
+
89
+ def generate_controller
90
+ cont = @options.controller
91
+ unless cont and cont.length > 0
92
+ puts "No controller specified, exiting."
93
+ puts "For help: monorail controller --help"
94
+ exit
95
+ end
96
+
97
+ if cont =~ /[^\w\d]/
98
+ puts "Illegal characters in controller name, exiting."
99
+ puts "For help: monorail controller --help"
100
+ exit
101
+ end
102
+
103
+ path = File.expand_path( @options.dir )
104
+ puts "Generating controller: #{cont}"
105
+ puts " in directory: #{path}"
106
+
107
+ target = File.join( path, "c", cont )
108
+
109
+ if File.exist?(target) || File.exist?(target+".rb")
110
+ r = Readline.readline( "*** Controller #{cont} already exists. Erase it [yN]? " )
111
+ unless r =~ /\Ay/i
112
+ puts "Controller not re-generated, exiting."
113
+ exit
114
+ end
115
+
116
+ puts "Erasing existing controller and re-generating"
117
+ FileUtils.rm( target + ".rb" )
118
+ FileUtils.rm_rf( target )
119
+ end
120
+
121
+ File.open( target + ".rb", "w" ) do |f|
122
+ f.write <<EOF
123
+ # Monorail controller #{cont}
124
+ # Generated #{Time.now}
125
+
126
+ module Controller_#{cont}
127
+
128
+ def index
129
+ "---Monorail: #{cont}---"
130
+ end
131
+
132
+ end
133
+ EOF
134
+ end
135
+
136
+ FileUtils.mkdir target
137
+
138
+ puts "Generated controller #{cont}"
139
+ end
140
+
141
+
142
+ end
143
+
144
+
145
+ end # module Monorail
146
+
147
+
@@ -25,7 +25,6 @@
25
25
 
26
26
 
27
27
 
28
- require 'dl/import'
29
28
  require 'timeout'
30
29
  require 'stringio'
31
30
  require 'cgi'
@@ -38,28 +37,6 @@ module Monorail
38
37
 
39
38
 
40
39
 
41
- #
42
- # Link in the external library for WEBD (socket-machine handler).
43
- # And set up the callbacks so we can handle requests here.
44
- #
45
- =begin
46
- extend DL::Importable
47
- dlload "./libwebd"
48
-
49
- def request_callback
50
- m = Request.new
51
- resp = m.generate_response
52
- fulfill_webd_request m.http_response_code, resp, resp.length
53
- end
54
-
55
- RequestCallback = callback "void request_callback()"
56
-
57
- extern "void initialize_webd (void*)"
58
- extern "void fulfill_webd_request (int, const char*, int)"
59
- extern "const void *retrieve_post_content()"
60
- =end
61
-
62
-
63
40
  #
64
41
  # class SessionManager
65
42
  # Generate sessions and session keys.
@@ -346,6 +323,17 @@ class Request
346
323
  @@debug = d
347
324
  end
348
325
 
326
+ #
327
+ # Default pathinfo, if specified, causes us to redirect requests to "/"
328
+ # TODO: We need some kind of handling for favicon.ico and robots.txt.
329
+ # Maybe we need to detect when path_info contains only a single branch
330
+ # and does not specify the name of a known controller.
331
+ # THIS IS CURRENTLY INCOMPLETE.
332
+ @@default_pathinfo = nil
333
+ def self.set_default_pathinfo pi
334
+ @@default_pathinfo = pi
335
+ end
336
+
349
337
 
350
338
 
351
339
  # class DirectoryAlias
@@ -487,6 +475,10 @@ class Request
487
475
  # generate_content
488
476
  #
489
477
  def generate_content
478
+ if @cgi.path_info == '/' and @@default_pathinfo
479
+ return redirect_to( @@default_pathinfo )
480
+ end
481
+
490
482
  diralias = @@directory_aliases.detect {|da| da.match_request @cgi.path_info }
491
483
 
492
484
  diralias or raise FileNotFound
@@ -614,7 +606,8 @@ class Request
614
606
  @headers ['Cache-control'] = "no-cache"
615
607
 
616
608
  @extra_paths = paths || []
617
- @path_prefix = File.join( path_prefix, "pages" )
609
+ #@path_prefix = File.join( path_prefix, "pages" )
610
+ @path_prefix = File.join( path_prefix, action )
618
611
  instance_eval verb
619
612
 
620
613
  end
@@ -0,0 +1,122 @@
1
+ # $Id: server.rb 2406 2006-04-28 16:36:00Z francis $
2
+ #
3
+ # Monorail::Application
4
+ # This runs a monorail system from a command-line binary.
5
+ # The binary is generated by rake and invokes us here.
6
+ #
7
+ #--
8
+ # This program is free software; you can redistribute it and/or modify
9
+ # it under the terms of the GNU General Public License as published by
10
+ # the Free Software Foundation; either version 2 of the License, or
11
+ # (at your option) any later version.
12
+ #
13
+ # This program is free software; you can redistribute it and/or modify
14
+ # it under the terms of the GNU General Public License as published by
15
+ # the Free Software Foundation; either version 2 of the License, or
16
+ # (at your option) any later version.
17
+ #
18
+ # This program is distributed in the hope that it will be useful,
19
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
20
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
+ # GNU General Public License for more details.
22
+ #
23
+ # You should have received a copy of the GNU General Public License
24
+ # along with this program; if not, write to the Free Software
25
+ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26
+ #
27
+
28
+
29
+
30
+
31
+ require 'ostruct'
32
+ require 'optparse'
33
+
34
+
35
+ module Monorail
36
+
37
+
38
+ class MonorailServer
39
+
40
+
41
+ def initialize
42
+ end
43
+
44
+
45
+ def run
46
+ options = OpenStruct.new
47
+ options.host = "127.0.0.1"
48
+ options.port = 8900
49
+ options.ssl = false
50
+ options.daemon = false
51
+ options.environment = :monorail
52
+ options.directory = "."
53
+
54
+ opts = OptionParser.new {|opts|
55
+
56
+ opts.separator ""
57
+ opts.separator "Options summary:"
58
+
59
+ opts.on("-a", "--addr IP-address",
60
+ "Requires the host IP address to listen on",
61
+ " default: 127.0.0.1") {|addr|
62
+ options.host = addr
63
+ }
64
+
65
+ opts.on("-p", "--port TCP port",
66
+ "Requires the TCP port to listen on",
67
+ " default: 8900") {|port|
68
+ options.port = port.to_i
69
+ }
70
+
71
+ opts.on("-s", "--[no-]ssl", "Require SSL (HTTPS)",
72
+ " default: false") {|ssl|
73
+ options.ssl = ssl
74
+ }
75
+
76
+ #opts.on("-d", "--[no-]daemon", "Run in the background",
77
+ # " default: false") {|d|
78
+ # options.daemon = d
79
+ #}
80
+
81
+ opts.on("-e", "--env ENVIRONMENT", "execution environment",
82
+ " choices: monorail, rails, static",
83
+ " default: monorail") {|e|
84
+ options.environment = e.to_s.downcase.intern
85
+ }
86
+
87
+ opts.on("-d", "--dir DIRECTORY", "directory where the application is located",
88
+ " default: the current directory") {|d|
89
+ options.directory = d.to_s
90
+ }
91
+
92
+
93
+ # Options summary
94
+ opts.on_tail("-h", "--help", "Show this message") {
95
+ puts opts
96
+ exit
97
+ }
98
+
99
+ # Version
100
+ opts.on_tail("--version", "Show version") {
101
+ puts Version
102
+ exit
103
+ }
104
+
105
+
106
+ }
107
+
108
+ opts.parse!(ARGV)
109
+ Monorail.new( options ).run
110
+
111
+ end
112
+
113
+
114
+
115
+ end # class MonorailServer
116
+
117
+
118
+ end # module Monorail
119
+
120
+
121
+ #----------------------
122
+