cortex-reaver 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (141) hide show
  1. data/README +1 -1
  2. data/bin/cortex_reaver +3 -4
  3. data/lib/cortex_reaver.rb +270 -110
  4. data/lib/cortex_reaver/cache.rb +23 -0
  5. data/lib/cortex_reaver/config.rb +178 -74
  6. data/lib/cortex_reaver/controller/admin.rb +64 -4
  7. data/lib/cortex_reaver/controller/comment.rb +4 -4
  8. data/lib/cortex_reaver/controller/controller.rb +3 -3
  9. data/lib/cortex_reaver/controller/journal.rb +4 -2
  10. data/lib/cortex_reaver/controller/main.rb +117 -26
  11. data/lib/cortex_reaver/controller/page.rb +7 -0
  12. data/lib/cortex_reaver/controller/photograph.rb +12 -10
  13. data/lib/cortex_reaver/controller/tag.rb +1 -1
  14. data/lib/cortex_reaver/controller/user.rb +7 -2
  15. data/lib/cortex_reaver/helper/attachments.rb +2 -2
  16. data/lib/cortex_reaver/helper/crud.rb +7 -7
  17. data/lib/cortex_reaver/helper/feeds.rb +56 -38
  18. data/lib/cortex_reaver/helper/form.rb +12 -16
  19. data/lib/cortex_reaver/helper/navigation.rb +35 -16
  20. data/lib/cortex_reaver/helper/photographs.rb +2 -2
  21. data/lib/cortex_reaver/helper/sidebar.rb +44 -0
  22. data/lib/cortex_reaver/helper/tags.rb +32 -9
  23. data/lib/cortex_reaver/helper/workflow.rb +2 -14
  24. data/lib/cortex_reaver/layout/blank.rhtml +27 -31
  25. data/lib/cortex_reaver/layout/text.rhtml +54 -67
  26. data/lib/cortex_reaver/migrations/014_convert_projects_to_pages.rb +79 -0
  27. data/lib/cortex_reaver/model/comment.rb +7 -8
  28. data/lib/cortex_reaver/model/page.rb +5 -3
  29. data/lib/cortex_reaver/model/photograph.rb +57 -13
  30. data/lib/cortex_reaver/model/tag.rb +1 -4
  31. data/lib/cortex_reaver/model/user.rb +6 -3
  32. data/lib/cortex_reaver/plugin.rb +1 -1
  33. data/lib/cortex_reaver/plugins/twitter.rb +185 -0
  34. data/lib/cortex_reaver/public/css/actions.css +31 -0
  35. data/lib/cortex_reaver/public/css/admin.css +50 -27
  36. data/lib/cortex_reaver/public/css/attachments.css +11 -0
  37. data/lib/cortex_reaver/public/css/autotags.css +38 -0
  38. data/lib/cortex_reaver/public/css/code.css +10 -2
  39. data/lib/cortex_reaver/public/css/colophon.css +10 -0
  40. data/lib/cortex_reaver/public/css/commments.css +7 -0
  41. data/lib/cortex_reaver/public/css/custom.css +1 -0
  42. data/lib/cortex_reaver/public/css/flash.css +17 -0
  43. data/lib/cortex_reaver/public/css/fonts.css +22 -0
  44. data/lib/cortex_reaver/public/css/form.css +15 -3
  45. data/lib/cortex_reaver/public/css/generics.css +39 -0
  46. data/lib/cortex_reaver/public/css/icons.css +4 -0
  47. data/lib/cortex_reaver/public/css/journals.css +3 -0
  48. data/lib/cortex_reaver/public/css/main.css +30 -312
  49. data/lib/cortex_reaver/public/css/pagination.css +50 -0
  50. data/lib/cortex_reaver/public/css/photo-show.css +87 -0
  51. data/lib/cortex_reaver/public/css/photo.css +28 -97
  52. data/lib/cortex_reaver/public/css/progress.css +35 -0
  53. data/lib/cortex_reaver/public/css/sidebar.css +83 -0
  54. data/lib/cortex_reaver/public/css/table-of-contents.css +26 -0
  55. data/lib/cortex_reaver/public/css/table.css +3 -0
  56. data/lib/cortex_reaver/public/css/tags.css +29 -0
  57. data/lib/cortex_reaver/public/css/text.css +19 -1
  58. data/lib/cortex_reaver/public/css/top_actions.css +50 -0
  59. data/lib/cortex_reaver/public/css/users.css +3 -0
  60. data/lib/cortex_reaver/public/images/admin/icons.png +0 -0
  61. data/lib/cortex_reaver/public/images/admin/icons.xcf +0 -0
  62. data/lib/cortex_reaver/public/images/background_tile.png +0 -0
  63. data/lib/cortex_reaver/public/images/edit_34.png +0 -0
  64. data/lib/cortex_reaver/public/images/edit_34_prelight.png +0 -0
  65. data/lib/cortex_reaver/public/images/elided.png +0 -0
  66. data/lib/cortex_reaver/public/images/grid_34.png +0 -0
  67. data/lib/cortex_reaver/public/images/grid_34_prelight.png +0 -0
  68. data/lib/cortex_reaver/public/images/next_11.png +0 -0
  69. data/lib/cortex_reaver/public/images/next_34.png +0 -0
  70. data/lib/cortex_reaver/public/images/next_34_prelight.png +0 -0
  71. data/lib/cortex_reaver/public/images/prev_11.png +0 -0
  72. data/lib/cortex_reaver/public/images/prev_34.png +0 -0
  73. data/lib/cortex_reaver/public/images/prev_34_prelight.png +0 -0
  74. data/lib/cortex_reaver/public/js/admin.js +15 -22
  75. data/lib/cortex_reaver/public/js/autotags.js +120 -0
  76. data/lib/cortex_reaver/public/js/jquery.autocomplete.js +135 -176
  77. data/lib/cortex_reaver/public/js/jquery.color.js +124 -0
  78. data/lib/cortex_reaver/public/js/jquery.corners.min.js +7 -0
  79. data/lib/cortex_reaver/public/js/jquery.hotkeys-0.7.9.js +244 -0
  80. data/lib/cortex_reaver/public/js/jquery.js +4361 -4
  81. data/lib/cortex_reaver/public/js/jquery.periodicalupdater.js +98 -0
  82. data/lib/cortex_reaver/public/js/photo.js +3 -32
  83. data/lib/cortex_reaver/public/robots.txt +3 -0
  84. data/lib/cortex_reaver/snippets/ramaze/cache/memcached.rb +13 -10
  85. data/lib/cortex_reaver/snippets/range.rb +9 -0
  86. data/lib/cortex_reaver/support/attachments.rb +12 -0
  87. data/lib/cortex_reaver/support/comments.rb +1 -3
  88. data/lib/cortex_reaver/support/renderer.rb +20 -17
  89. data/lib/cortex_reaver/support/sequenceable.rb +6 -6
  90. data/lib/cortex_reaver/support/tags.rb +15 -7
  91. data/lib/cortex_reaver/version.rb +1 -1
  92. data/lib/cortex_reaver/view/admin/configuration.rhtml +7 -0
  93. data/lib/cortex_reaver/view/admin/index.rhtml +3 -0
  94. data/lib/cortex_reaver/view/admin/regenerate_photo_sizes.rhtml +16 -0
  95. data/lib/cortex_reaver/view/adminbox.rhtml +34 -44
  96. data/lib/cortex_reaver/view/comments/comment.rhtml +1 -1
  97. data/lib/cortex_reaver/view/comments/form.rhtml +1 -2
  98. data/lib/cortex_reaver/view/comments/post_form.rhtml +2 -10
  99. data/lib/cortex_reaver/view/head.rhtml +11 -0
  100. data/lib/cortex_reaver/view/journals/journal.rhtml +3 -3
  101. data/lib/cortex_reaver/view/journals/show.rhtml +0 -4
  102. data/lib/cortex_reaver/view/js.rhtml +1 -0
  103. data/lib/cortex_reaver/view/pages/list.rhtml +3 -23
  104. data/lib/cortex_reaver/view/pages/row.rhtml +13 -0
  105. data/lib/cortex_reaver/view/photographs/grid.rhtml +30 -36
  106. data/lib/cortex_reaver/view/photographs/show.rhtml +42 -108
  107. data/lib/cortex_reaver/view/sidebar/explore_photos.rhtml +7 -0
  108. data/lib/cortex_reaver/view/sidebar/photographs.rhtml +15 -0
  109. data/lib/cortex_reaver/view/sidebar/sections.rhtml +4 -0
  110. data/lib/cortex_reaver/view/sidebar/twitter.rhtml +12 -0
  111. data/lib/cortex_reaver/view/tags/show.rhtml +0 -10
  112. data/lib/cortex_reaver/view/tracker.rhtml +0 -0
  113. data/lib/cortex_reaver/view/users/list.rhtml +1 -7
  114. data/lib/cortex_reaver/view/users/login.rhtml +1 -1
  115. metadata +103 -43
  116. data/lib/cortex_reaver/controller/project.rb +0 -53
  117. data/lib/cortex_reaver/helper/template.rb +0 -37
  118. data/lib/cortex_reaver/model/project.rb +0 -57
  119. data/lib/cortex_reaver/public/css/ramaze_error.css +0 -90
  120. data/lib/cortex_reaver/public/images/atom-xml-icon.png +0 -0
  121. data/lib/cortex_reaver/public/images/body.png +0 -0
  122. data/lib/cortex_reaver/public/images/border_bottom.png +0 -0
  123. data/lib/cortex_reaver/public/images/border_bottom_left.png +0 -0
  124. data/lib/cortex_reaver/public/images/border_bottom_right.png +0 -0
  125. data/lib/cortex_reaver/public/images/border_left.png +0 -0
  126. data/lib/cortex_reaver/public/images/border_right.png +0 -0
  127. data/lib/cortex_reaver/public/images/border_top.png +0 -0
  128. data/lib/cortex_reaver/public/images/border_top_left.png +0 -0
  129. data/lib/cortex_reaver/public/images/border_top_right.png +0 -0
  130. data/lib/cortex_reaver/public/images/header.png +0 -0
  131. data/lib/cortex_reaver/public/images/header.xcf +0 -0
  132. data/lib/cortex_reaver/public/images/indicator.gif +0 -0
  133. data/lib/cortex_reaver/public/images/rss-xml-icon.png +0 -0
  134. data/lib/cortex_reaver/public/images/sections.png +0 -0
  135. data/lib/cortex_reaver/public/images/sections_highlight.png +0 -0
  136. data/lib/cortex_reaver/public/js/jquery.autocompletefb.js +0 -125
  137. data/lib/cortex_reaver/view/photographs/sidebar.rhtml +0 -7
  138. data/lib/cortex_reaver/view/projects/form.rhtml +0 -14
  139. data/lib/cortex_reaver/view/projects/list.rhtml +0 -31
  140. data/lib/cortex_reaver/view/projects/show.rhtml +0 -42
  141. data/lib/proto/cortex_reaver.yaml +0 -47
data/README CHANGED
@@ -28,7 +28,7 @@ cortex_reaver --migrate # Initialize an SQLite database.
28
28
  cortex_reaver --start # Start the daemon.
29
29
 
30
30
  Then point your web browser at localhost:7000, hit control-x, and log in with
31
- username "shodan", password "shodan".
31
+ username "shodan", password "citadelstation".
32
32
 
33
33
  You can check Cortex Reaver's status with --status, and when you're done, shut
34
34
  it down with --stop.
data/bin/cortex_reaver CHANGED
@@ -59,7 +59,6 @@ module CortexReaver
59
59
  @values[:load_file] = file
60
60
  end
61
61
 
62
-
63
62
  o.on '-m', '--migrate [version]',
64
63
  'Migrate the database to schema version, or to the latest version' do |version|
65
64
  @action = :migrate
@@ -109,7 +108,7 @@ module CortexReaver
109
108
 
110
109
  # Connect to dump DB
111
110
  dump = Sequel.connect "sqlite:////#{File.expand_path(file)}"
112
- Sequel::Migrator.apply dump, File.join(LIB_DIR,:migrations), current_version
111
+ Sequel::Migrator.apply dump, File.join(LIB_DIR, 'migrations'), current_version
113
112
 
114
113
  # Copy tables
115
114
  db.tables.each do |table|
@@ -142,7 +141,7 @@ module CortexReaver
142
141
 
143
142
  # Drop current migrations and move to the dump's version
144
143
  system($0, '-f', '-m', '0')
145
- Sequel::Migrator.apply db, LIB_DIR/:migrations, current_version
144
+ Sequel::Migrator.apply db, File.join(LIB_DIR, 'migrations'), current_version
146
145
 
147
146
  # Copy tables
148
147
  dump.tables.each do |table|
@@ -236,7 +235,7 @@ module CortexReaver
236
235
  stop
237
236
 
238
237
  when :test
239
- require File.join(DIR, '..', 'spec', 'main.rb')
238
+ require File.join(LIB_DIR, '..', '..', 'spec', 'spec')
240
239
 
241
240
  else
242
241
  abort("Unknown action: #{@action}")
data/lib/cortex_reaver.rb CHANGED
@@ -1,12 +1,16 @@
1
1
  #!/usr/bin/ruby
2
2
 
3
3
  begin
4
+ require 'forwardable' # Patch for broken Thin...
4
5
  require 'find'
5
6
  require 'rubygems'
6
7
  require 'ramaze'
7
8
  require 'sequel'
8
- require 'yaml'
9
+ require 'construct'
9
10
  require 'socket'
11
+ require 'cssmin'
12
+ require 'jsmin'
13
+ require 'json'
10
14
 
11
15
  require 'sequel/extensions/migration'
12
16
  require 'sequel/extensions/inflector'
@@ -14,8 +18,8 @@ begin
14
18
  rescue LoadError => e
15
19
  puts e
16
20
  puts "You probably need to install some packages Cortex Reaver needs. Try:
17
- apt-get install librmagick-ruby libmysql-ruby;
18
- gem install thin mongrel ramaze sequel yaml erubis BlueCloth rmagick exifr hpricot builder coderay;"
21
+ apt-get install libmagick9-dev libmysql-ruby;
22
+ gem install thin mongrel ramaze sequel yaml erubis BlueCloth rmagick exifr hpricot;"
19
23
  exit 255
20
24
  end
21
25
 
@@ -29,6 +33,113 @@ module CortexReaver
29
33
  require File.join(LIB_DIR, 'version')
30
34
  require File.join(LIB_DIR, 'config')
31
35
 
36
+ # Reads files from stock_dir and custom_dir matching pattern, and appends
37
+ # their contents. Returns a string.
38
+ def self.collect_files(stock_dir, custom_dir, pattern = /^[^\.].+/, opts = {})
39
+ str = ""
40
+ # Get target files
41
+ files = Dir.entries(stock_dir) | Dir.entries(custom_dir)
42
+
43
+ # Reorder files if necessary.
44
+ if order = opts[:order]
45
+ files = (order & files) + (files - order)
46
+ end
47
+
48
+ # Read files
49
+ files.each do |file|
50
+ next unless file =~ pattern
51
+ custom_file = File.join(custom_dir, file)
52
+ stock_file = File.join(stock_dir, file)
53
+ if File.exists? custom_file
54
+ str << File.read(custom_file)
55
+ else
56
+ str << File.read(stock_file)
57
+ end
58
+ str << "\n"
59
+ end
60
+
61
+ str
62
+ end
63
+
64
+ # Compiles CSS files and creates minified version.
65
+ def self.compile_css
66
+ Ramaze::Log.info "Compiling CSS"
67
+ stock_dir = File.join(LIB_DIR, 'public', 'css')
68
+ custom_dir = File.join(config.public_root, 'css')
69
+
70
+ # Get CSS files
71
+ FileUtils.mkdir_p(custom_dir)
72
+ css = collect_files(stock_dir, custom_dir, /^((?!style).)*\.css$/, :order => config.css)
73
+
74
+ # Write minified CSS
75
+ File.open(File.join(custom_dir, 'style.css'), 'w') do |file|
76
+ file.write CSSMin.minify(css)
77
+ end
78
+ end
79
+
80
+ # Compiles JS files and creates minified version.
81
+ def self.compile_js
82
+ Ramaze::Log.info "Compiling JS"
83
+ stock_dir = File.join(LIB_DIR, 'public', 'js')
84
+ custom_dir = File.join(config.public_root, 'js')
85
+
86
+ # Get JS files
87
+ FileUtils.mkdir_p(custom_dir)
88
+ js = collect_files(stock_dir, custom_dir, /^((?!site).)*\.js$/, :order => config.js)
89
+
90
+ # Write minified JS
91
+ File.open(File.join(custom_dir, 'site.js'), 'w') do |file|
92
+ file.write js #JSMin.minify(js)
93
+ end
94
+ end
95
+
96
+ def self.compile_resources
97
+ # Prepare CSS/JS
98
+ self.compile_css
99
+ self.compile_js
100
+
101
+ if config.mode == :development
102
+ # Recompile CSS/JS on changes.
103
+ @asset_compiler = Thread.new do
104
+ # Get files to watch
105
+ files = Dir.glob(File.join(LIB_DIR, 'public', 'css', '*.css'))
106
+ files |= Dir.glob(File.join(config.public_root, 'css', '*.css'))
107
+ files |= Dir.glob(File.join(LIB_DIR, 'public', 'js', '*.js'))
108
+ files |= Dir.glob(File.join(config.public_root, 'js', '*.js'))
109
+ files -= [
110
+ File.join(config.public_root, 'css', 'style.css'),
111
+ File.join(config.public_root, 'js', 'site.js')
112
+ ]
113
+ last = {}
114
+
115
+ # Get initial modification times.
116
+ files.each do |f|
117
+ last[f] = File.stat(f).mtime
118
+ end
119
+
120
+ loop do
121
+ # Check files
122
+ rebuild = false
123
+ files.each do |f|
124
+ mtime = File.stat(f).mtime
125
+ if last[f] < mtime
126
+ last[f] = mtime
127
+ rebuild = true
128
+ end
129
+ end
130
+
131
+ if rebuild
132
+ # Recompile
133
+ self.compile_css
134
+ self.compile_js
135
+ end
136
+
137
+ sleep 2
138
+ end
139
+ end
140
+ end
141
+ end
142
+
32
143
  # Returns the site configuration
33
144
  def self.config
34
145
  @config
@@ -42,35 +153,55 @@ module CortexReaver
42
153
  @config_file = file
43
154
  end
44
155
 
45
- def self.db
46
- @db
47
- end
156
+ # The total span of items in the CR DB, for copyright notices and such.
157
+ def self.content_range
158
+ return @content_range if @content_range
48
159
 
49
- # Prepare Ramaze, create directories, etc.
50
- def self.init
51
- # App
52
- Ramaze.options.app.name = :cortex_reaver
53
-
54
- # Root directory
55
- Ramaze::App[:cortex_reaver].options.roots = [LIB_DIR]
56
- if config[:root]
57
- Ramaze::App[:cortex_reaver].options.roots.unshift(config[:root])
160
+ @content_range = [Journal, Page, Photograph].inject(nil) do |total, ds|
161
+ begin
162
+ if total
163
+ total | ds.range(:created_on)
164
+ else
165
+ ds.range(:created_on)
166
+ end
167
+ rescue
168
+ # Some range was invalid
169
+ total
170
+ end
171
+ end
172
+ if @content_range.first.kind_of? String
173
+ @content_range = DateTime.parse(@content_range.begin) .. DateTime.parse(@content_range.end)
58
174
  end
59
175
 
60
- # Server options
61
- Ramaze.options.adapter.handler = config[:adapter]
62
- Ramaze.options.adapter.host = config[:host]
63
- Ramaze.options.adapter.port = config[:port]
176
+ # If there's no content, default to today!
177
+ @content_range ||= Time.now .. Time.now
178
+ end
64
179
 
65
- # Check directories
66
- if config[:root] and not File.directory? config.public_root
180
+ # Creates the app directories if they don't exist.
181
+ def self.create_directories
182
+ # view
183
+ if config.view_root
184
+ if not File.directory? config.view_root
185
+ # Try to create a view directory
186
+ begin
187
+ FileUtils.mkdir_p config.view_root
188
+ rescue => e
189
+ Ramaze::Log.warn "Unable to create a view directory at #{config.view_root}: #{e}."
190
+ end
191
+ end
192
+ end
193
+
194
+ # public
195
+ if config.public_root and not File.directory? config.public_root
67
196
  # Try to create a public directory
68
197
  begin
69
- FileUtils.mkdir_p config[:public_root]
198
+ FileUtils.mkdir_p config.public_root
70
199
  rescue => e
71
200
  Ramaze::Log.warn "Unable to create a public directory at #{config.public_root}: #{e}."
72
201
  end
73
202
  end
203
+
204
+ # log
74
205
  if config[:log_root] and not File.directory? config[:log_root]
75
206
  # Try to create a log directory
76
207
  begin
@@ -82,86 +213,80 @@ module CortexReaver
82
213
  config[:log_root] = nil
83
214
  end
84
215
  end
216
+ end
85
217
 
86
- # Clear loggers
87
- Ramaze::Log.loggers.clear
218
+ def self.db
219
+ @db
220
+ end
88
221
 
89
- unless config[:daemon]
90
- # Log to console
91
- Ramaze::Log.loggers << Logger.new(STDOUT)
92
- end
222
+ # Prepare Ramaze, create directories, etc.
223
+ def self.init
224
+ # App
225
+ Ramaze.options.app.name = :cortex_reaver
226
+
227
+ # Helper load path
228
+ Ramaze.options.helpers_helper.paths.unshift LIB_DIR
229
+
230
+ # Load controllers
231
+ require File.join(LIB_DIR, 'controller', 'controller')
93
232
 
233
+ # Server options
234
+ Ramaze.options.adapter.handler = config[:adapter]
235
+ Ramaze.options.adapter.host = config[:host]
236
+ Ramaze.options.adapter.port = config[:port]
237
+
238
+ # App mode
94
239
  case config[:mode]
95
240
  when :production
96
241
  Ramaze.options.mode = :live
97
-
98
- # Set up cache
99
- case config[:cache]
100
- when :memcache
101
- Ramaze::Cache.options.default = Ramaze::Cache::MemCache
102
- end
103
-
104
- if config[:log_root]
105
- # Log to file
106
- Ramaze::Log.loggers << Logger.new(
107
- File.join(config[:log_root], 'production.log')
108
- )
109
- Ramaze::Log.level = Logger::Severity::INFO
110
- end
111
242
  when :development
112
243
  Ramaze.options.mode = :dev
113
-
114
- if config[:log_root]
115
- # Log to file
116
- Ramaze::Log.loggers << Logger.new(
117
- File.join(config[:log_root], 'development.log')
118
- )
119
- Ramaze::Log.level = Logger::Severity::DEBUG
120
-
121
- # Also use SQL log
122
- db.logger = Logger.new(
123
- File.join(config[:log_root], 'sql.log')
124
- )
125
- end
126
244
  else
127
- raise ArgumentError.new("unknown Cortex Reaver mode #{config[:mode].inspect}. Expected one of [:production, :development].")
128
- end
129
-
130
- # Prepare view directory
131
- if config.view_root
132
- if not File.directory? config.view_root
133
- # Try to create a view directory
134
- begin
135
- FileUtils.mkdir_p config.view_root
136
- rescue => e
137
- Ramaze::Log.warn "Unable to create a view directory at #{config[:view_root]}: #{e}."
138
- end
139
- end
140
- end
141
-
142
- # Load plugins
143
- config[:plugins].each do |plugin|
144
- Ramaze::Log.info "Loading plugin #{plugin}"
145
- require File.join(config[:plugin_root], plugin)
245
+ raise ArgumentError.new("unknown Cortex Reaver mode #{config.mode.inspect}. Expected one of [:production, :development].")
146
246
  end
247
+
248
+ create_directories
249
+ setup_logging
250
+ setup_cache
251
+ compile_resources
252
+ load_plugins
147
253
  end
148
254
 
149
255
  # Load libraries
150
256
  def self.load
151
257
  # Load controllers and models
258
+ require File.join(LIB_DIR, 'version')
152
259
  require File.join(LIB_DIR, 'config')
153
260
  require File.join(LIB_DIR, 'plugin')
154
- require File.join(LIB_DIR, 'version')
155
261
  Ramaze::acquire File.join(LIB_DIR, 'snippets', '**', '*')
156
262
  Ramaze::acquire File.join(LIB_DIR, 'support', '*')
263
+ require File.join(LIB_DIR, 'cache')
157
264
  require File.join(LIB_DIR, 'model', 'model')
158
- Ramaze::acquire File.join(LIB_DIR, 'helper', '*')
159
- require File.join(LIB_DIR, 'controller', 'controller')
265
+ end
266
+
267
+ # Load plugins
268
+ def self.load_plugins
269
+ # Create plugin cache
270
+ Ramaze::Cache.add(:plugin)
271
+
272
+ # Load plugins
273
+ config.plugins.enabled.each do |plugin|
274
+ Ramaze::Log.info "Loading plugin #{plugin}"
275
+ begin
276
+ require File.join(config.plugin_root, plugin)
277
+ rescue LoadError => e
278
+ require File.join(LIB_DIR, 'plugins', plugin)
279
+ end
280
+ end
160
281
  end
161
282
 
162
283
  # Reloads the site configuration
163
284
  def self.reload_config
164
- @config = CortexReaver::Config.new(config_file)
285
+ begin
286
+ @config = CortexReaver::Config.load(File.read(config_file))
287
+ rescue Errno::ENOENT
288
+ @config = CortexReaver::Config.new
289
+ end
165
290
  end
166
291
 
167
292
  # Restart Cortex Reaver
@@ -180,14 +305,15 @@ module CortexReaver
180
305
  # Shutdown callback
181
306
  at_exit do
182
307
  # Remove pidfile
183
- FileUtils.rm(config[:pidfile]) if File.exist? config[:pidfile]
308
+ FileUtils.rm(config.pidfile) if File.exist? config.pidfile
184
309
  end
185
310
 
186
311
  Ramaze::Log.info "Cortex Reaver #{Process.pid} stalking victims."
187
312
 
188
313
  # Run Ramaze
189
- # Ramaze.start :root => [config[:root], LIB_DIR]
190
- Ramaze.start :root => LIB_DIR
314
+ roots = [LIB_DIR]
315
+ roots.unshift config.root if config.root
316
+ Ramaze.start :root => roots
191
317
 
192
318
  puts "Cortex Reaver finished."
193
319
  end
@@ -204,22 +330,16 @@ module CortexReaver
204
330
  init
205
331
  end
206
332
 
207
- # Connect to DB. If check_schema is false, doesn't check to see that the schema
208
- # version is up to date.
333
+ # Connect to DB. If check_schema is false, doesn't check to see that the
334
+ # schema version is up to date.
209
335
  def self.setup_db(check_schema = true)
210
336
  unless config
211
337
  raise RuntimeError.new("no configuration available!")
212
338
  end
213
339
 
214
- # Build Sequel connection string
215
- unless (string = config[:database]).is_a? String
216
- d = config[:database]
217
- string = "#{d[:driver]}://#{d[:user]}:#{d[:password]}@#{d[:host]}/#{d[:database]}"
218
- end
219
-
220
340
  # Connect
221
341
  begin
222
- @db = Sequel.connect(string)
342
+ @db = Sequel.connect(config.database.str)
223
343
  rescue => e
224
344
  Ramaze::Log.error("Unable to connect to database: #{e}.")
225
345
  abort
@@ -234,6 +354,56 @@ module CortexReaver
234
354
  end
235
355
  end
236
356
 
357
+ def self.setup_cache
358
+ # Set up cache
359
+ case config.cache
360
+ when :memcache
361
+ Ramaze::Cache::MemCache::OPTIONS[:servers] = config.memcache.servers
362
+ Ramaze::Cache.options.default = Ramaze::Cache::MemCache
363
+
364
+ # Cache templates
365
+ Innate::View.options.read_cache = true
366
+ else
367
+ # Cache stub
368
+ # Hmm, this breaks session management.
369
+ # Ramaze::Log.warn "Caching disabled."
370
+ # Ramaze::Cache.options.default = CortexReaver::Cache::Noop
371
+ end
372
+ end
373
+
374
+ def self.setup_logging
375
+ # Clear loggers
376
+ Ramaze::Log.loggers.clear
377
+
378
+ unless config.daemon
379
+ # Log to console
380
+ Ramaze::Log.loggers << Logger.new(STDOUT)
381
+ end
382
+
383
+ if config.log_root
384
+ # Log to files
385
+ case config.mode
386
+ when :production
387
+ logfile = 'production.log'
388
+ level = Logger::Severity::INFO
389
+ when :development
390
+ logfile = 'development.log'
391
+ level = Logger::Severity::DEBUG
392
+
393
+ # Also use SQL log
394
+ db.logger = Logger.new(
395
+ File.join(config.log_root, 'sql.log')
396
+ )
397
+ end
398
+
399
+ # Create file logger
400
+ Ramaze::Log.loggers << Logger.new(
401
+ File.join(config.log_root, logfile)
402
+ )
403
+ Ramaze::Log.level = level
404
+ end
405
+ end
406
+
237
407
  # Disconnect from DB
238
408
  def self.shutdown_db
239
409
  @db.disconnect
@@ -244,32 +414,22 @@ module CortexReaver
244
414
  reload_config
245
415
 
246
416
  # Check PID
247
- if File.file? config[:pidfile]
248
- pid = File.read(config[:pidfile], 20).strip
417
+ if File.file? config.pidfile
418
+ pid = File.read(config.pidfile, 20).strip
249
419
  abort "Cortex Reaver already running? (#{pid})"
250
420
  end
251
421
 
252
422
  puts "Activating Cortex Reaver."
253
423
  setup
254
424
 
255
- # Check port availability
256
- # begin
257
- # socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
258
- # sockaddr = Socket.pack_sockaddr_in(*[config[:port], config[:host]])
259
- # socket.bind(sockaddr)
260
- # socket.close
261
- # rescue => e
262
- # abort "Unable to bind to port #{config[:host]}:#{config[:port]} (#{e})"
263
- # end
264
-
265
- if config[:daemon]
425
+ if config.daemon
266
426
  fork do
267
427
  # Drop console, create new session
268
428
  Process.setsid
269
429
  exit if fork
270
430
 
271
431
  # Write pidfile
272
- File.open(config[:pidfile], 'w') do |file|
432
+ File.open(config.pidfile, 'w') do |file|
273
433
  file << Process.pid
274
434
  end
275
435
 
@@ -294,16 +454,16 @@ module CortexReaver
294
454
  def self.stop
295
455
  reload_config
296
456
 
297
- unless config[:pidfile]
457
+ unless config.pidfile
298
458
  abort "No pidfile to stop."
299
459
  end
300
460
 
301
- unless File.file? config[:pidfile]
302
- abort "Cortex Reaver not running? (check #{config[:pidfile]})"
461
+ unless File.file? config.pidfile
462
+ abort "Cortex Reaver not running? (check #{config.pidfile})"
303
463
  end
304
464
 
305
465
  # Get PID
306
- pid = File.read(config[:pidfile], 20).strip
466
+ pid = File.read(config.pidfile, 20).strip
307
467
  unless (pid = pid.to_i) != 0
308
468
  abort "Invalid process ID in pidfile (#{pid})."
309
469
  end
@@ -337,11 +497,11 @@ module CortexReaver
337
497
  # Remove pidfile if killed.
338
498
  if killed
339
499
  begin
340
- FileUtils.rm(config[:pidfile])
500
+ FileUtils.rm(config.pidfile)
341
501
  rescue Errno::ENOENT
342
502
  # Pidfile gone
343
503
  rescue => e
344
- puts "Unable to remove pidfile #{config[:pidfile]}: #{e}."
504
+ puts "Unable to remove pidfile #{config.pidfile}: #{e}."
345
505
  end
346
506
  end
347
507
  end