Class: ActionFramework::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/actionframework.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeServer

Returns a new instance of Server.



27
28
29
30
31
32
33
# File 'lib/actionframework.rb', line 27

def initialize
	require 'bundler'
	Bundler.require(:default)

	@routesklass = ActionFramework::Routes.new
	@settings = ActionFramework::Settings.new
end

Class Method Details

.currentObject



35
36
37
38
39
40
41
42
43
# File 'lib/actionframework.rb', line 35

def self.current
   if($runningserver.nil?)
   	 $runningserver = ActionFramework::Server.new
   	 $runningserver.autoimport
     $runningserver
   else
     $runningserver
   end
end

.current=(runningserver) ⇒ Object



45
46
47
# File 'lib/actionframework.rb', line 45

def self.current=(runningserver)
  $runningserver = runningserver
end

Instance Method Details

#autoimportObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/actionframework.rb', line 49

def autoimport
  Dir.glob("controllers/*.rb").each do |file|
    require './'+file
  end

  Dir.glob("controller/mailers/*.rb") do |file|
  	require './'+file
  end

  Dir.glob("models/*.rb").each do |file|
    require './'+file
  end

  require './config/routes'
  require './config/settings'
  require './config/plugables'
  require './config/mailer'

  Dir.glob("initializers/*.rb").each do |file|
    require './'+file
  end

end

#call(env) ⇒ Object



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
# File 'lib/actionframework.rb', line 73

def call env
	req = Rack::Request.new(env)
	res = Rack::Response.new

	# redirection
	redirect = @routesklass.redirect? req
	if(!redirect.nil?)
		res.redirect(redirect.to)
		return res.finish
	end


	# auto-api feature (only at path /api/*)
	reso = getModelResponse(req,res)
	if(!reso.nil?)
		return reso
	end

	controllerinfo = @routesklass.route(req.path,req.request_method)
	if(controllerinfo == nil)
		res.body = [ActionFramework::ErrorHandler.new(env,req,res,{}).send("error_404")]
		res.status = 404
		return res.finish
	end

	controller = controllerinfo[0]
	matcheddata = controllerinfo[1]

	result = ActionFramework::ControllerSupervisor.new.run(controller,env,req,res,matcheddata)
	res.write result
	res.finish
end

#get_settingsObject



113
114
115
# File 'lib/actionframework.rb', line 113

def get_settings
	@settings
end

#getModelResponse(req, res) ⇒ Object



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/actionframework.rb', line 121

def getModelResponse req,res
	if(matcheddata = req.path.match(Regexp.new("/api/(?<modelname>(.*))")))
			return nil unless @routesklass.models.include?(matcheddata[:modelname])

res["Content-type"] = "application/json"
model = Object.const_get(matcheddata[:modelname].capitalize)
model.instance_variable_set("@req",req)

case req.request_method
when "POST"
	return ActionFramework::ModelHelper.post model,req,res
when "GET"
	return ActionFramework::ModelHelper.get model,res
when "UPDATE"
	return ActionFramework::ModelHelper.update model,res
when "DELETE"

else

end
			end
			nil
end

#routes(&block) ⇒ Object



105
106
107
# File 'lib/actionframework.rb', line 105

def routes &block
	@routesklass.instance_eval &block
end

#settings {|@settings| ... } ⇒ Object

Yields:



109
110
111
# File 'lib/actionframework.rb', line 109

def settings
	yield @settings
end

#startObject



117
118
119
# File 'lib/actionframework.rb', line 117

def start
  Rack::Server.new({:app => self,:server => @settings.server, :Port => @settings.port}).start
end