forked from splice/indaba-parkplace
/
parkplace.rb
145 lines (131 loc) · 5.48 KB
/
parkplace.rb
1
2
3
4
5
6
7
8
9
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
require 'rubygems'
require 'camping'
require 'camping/session'
require 'digest/sha1'
require 'base64'
require 'time'
require 'md5'
require 'active_record/acts/nested_set'
ActiveRecord::Base.send :include, ActiveRecord::Acts::NestedSet
Camping.goes :ParkPlace
require 'parkplace/errors'
require 'parkplace/helpers'
require 'parkplace/models'
require 'parkplace/controllers'
if $PARKPLACE_ACCESSORIES
require 'parkplace/control'
end
begin
require 'parkplace/torrent'
puts "-- RubyTorrent found, torrent support is turned on."
puts "-- TORRENT SUPPORT IS EXTREMELY EXPERIMENTAL -- WHAT I MEAN IS: IT PROBABLY DOESN'T WORK."
rescue LoadError
puts "-- No RubyTorrent found, torrent support disbled."
end
require 'parkplace/s3'
module ParkPlace
VERSION = "0.7"
BUFSIZE = (4 * 1024)
LOG_PATH = File.expand_path('../log', File.dirname(__FILE__))
STORAGE_PATH = File.join(Dir.pwd, 'storage')
STATIC_PATH = File.expand_path('../static', File.dirname(__FILE__))
RESOURCE_TYPES = %w[acl torrent]
CANNED_ACLS = {
'private' => 0600,
'public-read' => 0644,
'public-read-write' => 0666,
'authenticated-read' => 0640,
'authenticated-read-write' => 0660
}
READABLE = 0004
WRITABLE = 0002
READABLE_BY_AUTH = 0040
WRITABLE_BY_AUTH = 0020
class << self
def create
v = 0.0
v = 1.0 if Models::Bucket.table_exists?
Camping::Models::Session.create_schema
Models.create_schema :assume => v
end
def options
require 'ostruct'
options = OpenStruct.new
if options.parkplace_dir.nil?
homes = []
homes << [ENV['HOME'], File.join( ENV['HOME'], '.parkplace' )] if ENV['HOME']
homes << [ENV['APPDATA'], File.join( ENV['APPDATA'], 'ParkPlace' )] if ENV['APPDATA']
homes.each do |home_top, home_dir|
next unless home_top
if File.exists? home_top
options.parkplace_dir = home_dir
break
end
end
end
options
end
def config(options)
require 'ftools'
require 'yaml'
abort "** No home directory found, please say the directory when you run #$O." unless options.parkplace_dir
File.makedirs( options.parkplace_dir )
conf = File.join( options.parkplace_dir, 'config.yaml' )
if File.exists? conf
YAML.load_file( conf ).each { |k,v| options.__send__("#{k}=", v) if options.__send__(k).nil? }
end
options.storage_dir = File.expand_path(options.storage_dir || 'storage', options.parkplace_dir)
options.database ||= {:adapter => 'sqlite3', :database => File.join(options.parkplace_dir, 'park.db')}
if options.database[:adapter] == 'sqlite3'
begin
require 'sqlite3_api'
rescue LoadError
puts "!! Your SQLite3 adapter isn't a compiled extension."
abort "!! Please check out http://code.whytheluckystiff.net/camping/wiki/BeAlertWhenOnSqlite3 for tips."
end
end
ParkPlace::STORAGE_PATH.replace options.storage_dir
options.log_dir = File.expand_path(options.log_dir || 'log', options.parkplace_dir)
File.makedirs( options.log_dir )
ParkPlace::LOG_PATH.replace options.log_dir
end
def serve(host, port)
require 'mongrel'
require 'mongrel/camping'
require 'mongrel/debug' if $VERBOSE || $DAEMONIZE
if $PARKPLACE_PROGRESS
require_gem 'mongrel_upload_progress'
GemPlugin::Manager.instance.load "mongrel" => GemPlugin::INCLUDE
end
cwd = Dir.pwd
pid_file = File.join(ParkPlace::LOG_PATH,'mongrel.pid')
log_file = File.join(ParkPlace::LOG_PATH,'mongrel.log')
config = Mongrel::Configurator.new :host => host, :pid_file => pid_file do
if $DAEMONIZE
daemonize :cwd => cwd, :log_file => log_file
# Need to reopen this log file because daemonize closes any
# file opened before it was called.
if $DEBUG
ParkPlace::Models::Base.logger = Logger.new(File.join(ParkPlace::LOG_PATH,'camping.log'))
ParkPlace::Models::Base.colorize_logging = false
ParkPlace::Models::Base.logger.formatter = Logger::Formatter.new
end
end
listener :port => port do
uri "/", :handler => plugin('/handlers/requestlog::access') if $VERBOSE || $DAEMONIZE
uri "/", :handler => Mongrel::Camping::CampingHandler.new(ParkPlace)
if $PARKPLACE_PROGRESS
uri "/control/buckets", :handler => plugin('/handlers/upload')
end
uri "/favicon", :handler => Mongrel::Error404Handler.new("")
trap("INT") { stop }
run
write_pid_file if $DAEMONIZE
end
end
puts "** ParkPlace example is running at http://#{host}:#{port}/"
puts "** Visit http://#{host}:#{port}/control/ for the control center."
config.join
end
end
end