Class: HTTPApp
Defined Under Namespace
Classes: ControllerInfo, Instance
Constant Summary
collapse
- @@mutex =
Mutex.new
Instance Attribute Summary collapse
Attributes inherited from Application
#cluster, #running
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Application
#appswarmBaseDir, #config, #getApp, #getAppDataPath, #getAppDataPaths, #getAppDataWritePath, getAppDir, #getAppDir, #getAppId, #getAppName, #getService, #info, #log, #monitor, protect, provides, #provides?, providing, #providingAPIs, #quit?, requirements, requires, #retrieveConfig, #shortName, #warning, #withApp, #withAppFork
Constructor Details
#initialize(cluster, options) ⇒ HTTPApp
Returns a new instance of HTTPApp.
26
27
28
29
30
31
32
33
34
|
# File 'lib/appswarm/http/http_app.rb', line 26
def initialize(cluster,options)
super(cluster,options)
@port=options[:httpPort]
@instances=options[:instances]
@instances||=[]
@controllers=[]
getControllerPaths.each{|cntrlPath|loadController(cntrlPath)}
provides(:httpApp)
end
|
Instance Attribute Details
#instanceName ⇒ Object
Returns the value of attribute instanceName.
24
25
26
|
# File 'lib/appswarm/http/http_app.rb', line 24
def instanceName
@instanceName
end
|
#instances ⇒ Object
Returns the value of attribute instances.
24
25
26
|
# File 'lib/appswarm/http/http_app.rb', line 24
def instances
@instances
end
|
#port ⇒ Object
Returns the value of attribute port.
24
25
26
|
# File 'lib/appswarm/http/http_app.rb', line 24
def port
@port
end
|
Class Method Details
.index(name) ⇒ Object
20
21
22
|
# File 'lib/appswarm/http/http_app.rb', line 20
def self.index(name)
@index=name
end
|
Instance Method Details
#controllerName(controller) ⇒ Object
202
203
204
|
# File 'lib/appswarm/http/http_app.rb', line 202
def controllerName(controller)
controller.gsub(/.*\//,"").gsub(".rb","").camelCase
end
|
#createInstance(name) ⇒ Object
51
52
53
54
55
56
57
|
# File 'lib/appswarm/http/http_app.rb', line 51
def createInstance(name)
i=getInstanceByName(name)
return i if i
inst=Instance.new(self.class.to_s,cluster.createInstanceId,name,getAppName)
@instances<<inst
inst
end
|
#defaultInstance ⇒ Object
70
71
72
|
# File 'lib/appswarm/http/http_app.rb', line 70
def defaultInstance
createInstance("default")
end
|
#deleteInstance(instance) ⇒ Object
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/appswarm/http/http_app.rb', line 59
def deleteInstance(instance)
case instance
when String
instance=getInstanceByName(instance)
when Instance
else
instance=nil
end
@instances.delete(instance) if instance
end
|
#findFile(fileName) ⇒ Object
111
112
113
114
115
116
117
|
# File 'lib/appswarm/http/http_app.rb', line 111
def findFile(fileName)
f=File.join(getAppDir,fileName)
return f if File.exists?(f)
return nil
end
|
#getController(hash) ⇒ Object
211
212
213
214
215
216
217
218
219
220
221
222
|
# File 'lib/appswarm/http/http_app.rb', line 211
def getController(hash)
info=@controllers.select{|c|
ok=true
hash.each{|k,v|
ok&=(c.send(k)==v)
}
ok
}[0]
return nil unless info
info=loadController(info.filePath) if $DEBUGGING
info
end
|
#getControllerPaths ⇒ Object
195
196
197
198
199
|
# File 'lib/appswarm/http/http_app.rb', line 195
def getControllerPaths
path=File.join(getAppDir,"controllers","*.rb")
log "Controllers #{Dir[path]}"
Dir[path]
end
|
#getInstanceByName(name) ⇒ Object
74
75
76
|
# File 'lib/appswarm/http/http_app.rb', line 74
def getInstanceByName(name)
@instances.select{|i|i.instanceName==name}[0]
end
|
#halt ⇒ Object
191
192
193
|
# File 'lib/appswarm/http/http_app.rb', line 191
def halt
@p.stop if @p.is_a?(HTTPProvider)
end
|
#handle(req, res, args, sessionId, instance, currentUrl) ⇒ Object
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
|
# File 'lib/appswarm/http/http_app.rb', line 78
def handle(req,res,args,sessionId,instance,currentUrl)
begin
controller,func,*dummy=req.path_info.sub(/^\//,'').split("/")
log "path_info #{req.path_info}"
log "HANDLE #{controller} :: #{func}"
controller=getController(:urlPart=>controller) if controller
if controller.nil?
controller||=mainController
func,*dummy=req.path_info.sub(/^\//,'').split("/")
end
raise "NoController defined" if controller.nil?
func||="index"
path_rest=dummy
l=controller.klass.new(self,instance)
l.run(instance,res,func,args,path_rest,currentUrl)
rescue Object=>e
path=req.path_info
log "PATH:",path
log "PATH:",path
unless render_static(res,path)
path=path.gsub(/[^\/]*\//,'')
unless render_static(res,path)
raise e
end
end
end
end
|
#instancesFor(provider, appName) ⇒ Object
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
# File 'lib/appswarm/http/http_app.rb', line 150
def instancesFor(provider,appName)
instances=retrieveConfig[:instances].content
if instances
else
if @instances
instances=@instances.map{|i|[i,i]}
else
instances=[["defaultInstance",""]]
end
end
instances.map{|instance|
instanceName,url=instance
[instanceName,"/"+appName+"/"+url]
}
end
|
#loadController(controllerPath) ⇒ Object
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
# File 'lib/appswarm/http/http_app.rb', line 224
def loadController(controllerPath)
log "Loading Controller #{controllerPath}"
controllerName=controllerName(controllerPath)
log controllerName
controllerPrg=File.load(controllerPath)
return unless controllerPrg
log "OK loaded"
self.class.send(:class_eval,controllerPrg)
klass=self.class.class_eval(controllerName)
info=ControllerInfo.new(controllerName,klass,controllerPath,controllerName.underscored)
@controllers<<info unless @controllers.select{|c|c.klass==klass}.length>0
info
end
|
#machine ⇒ Object
183
184
185
|
# File 'lib/appswarm/http/http_app.rb', line 183
def machine
"localhost"
end
|
#mainController ⇒ Object
206
207
208
209
|
# File 'lib/appswarm/http/http_app.rb', line 206
def mainController
cntrl=@controllers.select{|c|c.name.downcase==getAppName.downcase}[0]
cntrl||@controllers[0] end
|
#quit ⇒ Object
36
37
38
39
|
# File 'lib/appswarm/http/http_app.rb', line 36
def quit
saveRunningInstances
super
end
|
#render_static(res, filename) ⇒ Object
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
|
# File 'lib/appswarm/http/http_app.rb', line 119
def render_static(res,filename)
paths=[File.join(getAppDir,"static/"),File.expand_path('../static',__FILE__)]
paths.each{|path|
cp=File.join(path,filename)
if cp and File.file?(cp)
f=File.open(cp,"r")
c=f.read
f.close
res.body=c
res["Content-Type"]=case filename.gsub(/.*\./,"")
when "css"
"text/css"
when "svg"
"image/svg+xml"
when "ico"
"image/icon"
else
"text/plain"
end
return true
end
}
false
end
|
#run ⇒ Object
146
147
148
|
# File 'lib/appswarm/http/http_app.rb', line 146
def run
end
|
#serviceAddress(instance, path = "") ⇒ Object
177
178
179
180
|
# File 'lib/appswarm/http/http_app.rb', line 177
def serviceAddress(instance,path="")
raise "Instance not found!" unless @instances.member?(instance)
[getAppName,instance,path]
end
|
#settings ⇒ Object
47
48
49
|
# File 'lib/appswarm/http/http_app.rb', line 47
def settings
HTTPAppSettings.new(self)
end
|
#startup ⇒ Object
41
42
43
44
|
# File 'lib/appswarm/http/http_app.rb', line 41
def startup
runStoredInstances
super
end
|
#stop ⇒ Object
187
188
189
|
# File 'lib/appswarm/http/http_app.rb', line 187
def stop
@p.stop if @p.is_a?(HTTPProvider)
end
|
#url ⇒ Object
167
168
169
170
171
172
173
174
175
|
# File 'lib/appswarm/http/http_app.rb', line 167
def url
base=""
base= @p.url if @p
mc=mainController
log "ControllerName #{mc} #{mc.urlPart}"
log "BASE #{base}"
return base+"/"+mc.urlPart if mc
"/"
end
|