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
|
require 'logger'
# set default environment
ENV['RACK_ENV'] = 'test' unless ENV['RACK_ENV']
# load/setup configuration
basedir = File.join(ENV['HOME'], ".opentox")
config_dir = File.join(basedir, "config")
config_file = File.join(config_dir, "#{ENV['RACK_ENV']}.yaml")
user_file = File.join(config_dir, "users.yaml")
TMP_DIR = File.join(basedir, "tmp")
LOG_DIR = File.join(basedir, "log")
if File.exist?(config_file)
@@config = YAML.load_file(config_file)
else
FileUtils.mkdir_p TMP_DIR
FileUtils.mkdir_p LOG_DIR
FileUtils.mkdir_p config_dir
FileUtils.cp(File.join(File.dirname(__FILE__), 'templates/config.yaml'), config_file)
puts "Please edit #{config_file} and restart your application."
exit
end
# database
if @@config[:database]
['dm-core', 'dm-serializer', 'dm-timestamps', 'dm-types'].each{|lib| require lib }
case @@config[:database][:adapter]
when /sqlite/i
db_dir = File.join(basedir, "db")
FileUtils.mkdir_p db_dir
DataMapper::setup(:default, "sqlite3://#{db_dir}/opentox.sqlite3")
else
DataMapper.setup(:default, {
:adapter => @@config[:database][:adapter],
:database => @@config[:database][:database],
:username => @@config[:database][:username],
:password => @@config[:database][:password],
:host => @@config[:database][:host]})
end
end
# hack: store sinatra in global var to make url_for and halt methods accessible
before {$sinatra = self unless $sinatra}
class Sinatra::Base
# overwriting halt to log halts (!= 202)
def halt(*response)
LOGGER.error "halt "+response.first.to_s+" "+(response.size>1 ? response[1].to_s : "") if response.first >= 300
# orig sinatra code:
response = response.first if response.length == 1
throw :halt, response
end
end
# logging
class MyLogger < Logger
def pwd
path = Dir.pwd.to_s
index = path.rindex(/\//)
return path if index==nil
path[(index+1)..-1]
end
def trace()
lines = caller(0)
n = 2
line = lines[n]
while (line =~ /spork.rb/ or line =~ /as_task/ or line =~ /environment.rb/)
n += 1
line = lines[n]
end
index = line.rindex(/\/.*\.rb/)
return line if index==nil
line[index..-1]
end
def format(msg)
pwd.ljust(18)+" :: "+msg.to_s+" :: "+trace+" :: "+ENV['REMOTE_ADDR'].to_s
end
def debug(msg)
super format(msg)
end
def info(msg)
super format(msg)
end
def warn(msg)
super format(msg)
end
def error(msg)
super format(msg)
end
end
logfile = "#{LOG_DIR}/#{ENV["RACK_ENV"]}.log"
LOGGER = MyLogger.new(logfile,'daily') # daily rotation
#LOGGER = MyLogger.new(STDOUT)
#LOGGER.datetime_format = "%Y-%m-%d %H:%M:%S "
#LOGGER.level = Logger::DEBUG
if File.exist?(user_file)
@@users = YAML.load_file(user_file)
else
FileUtils.cp(File.join(File.dirname(__FILE__), 'templates/users.yaml'), user_file)
puts "Please edit #{user_file} and restart your application."
exit
end
begin
0 < @@users[:users].keys.length
rescue
raise "Please edit #{user_file} and restart your application. Create at least one user with password."
end
# RDF namespaces
RDF = Redland::Namespace.new 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
OWL = Redland::Namespace.new 'http://www.w3.org/2002/07/owl#'
DC = Redland::Namespace.new 'http://purl.org/dc/elements/1.1/'
OT = Redland::Namespace.new 'http://www.opentox.org/api/1.1#'
# Regular expressions for parsing classification data
TRUE_REGEXP = /^(true|active|$1^)/
FALSE_REGEXP = /^(false|inactive|$0^)/
|