Class: Gors::Server
- Inherits:
-
Object
- Object
- Gors::Server
- Defined in:
- lib/gors.rb
Instance Method Summary collapse
- #autoimport ⇒ Object
- #call(env) ⇒ Object
-
#initialize ⇒ Server
constructor
A new instance of Server.
- #routes(&block) ⇒ Object
- #run ⇒ Object
- #settings {|@settings| ... } ⇒ Object
- #start ⇒ Object
Constructor Details
Instance Method Details
#autoimport ⇒ Object
94 95 96 97 98 99 100 101 102 |
# File 'lib/gors.rb', line 94 def autoimport Dir.glob("controllers/*").each do |file| require './'+file end Dir.glob("models/*").each do |file| require './'+file end end |
#call(env) ⇒ Object
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 |
# File 'lib/gors.rb', line 19 def call env if(@settings.errorhandler != nil) @errorhandler = Object.const_get(@settings.errorhandler).new else @errorhandler = Gors::DefaultErrorHandler.new end routesinfo = @routesklass.routes(env["REQUEST_PATH"]) controller = routesinfo[0] @logger.log controller.inspect matcheddata = routesinfo[1] if(controller == nil) if(@settings.server != "thin") response = ["<h1>404 Not Found</h1>"] else response = "<h1>404 Not Found</h1>" end return @errorhandler.call "404" end # if(controller.include? "Gors::") ctrl = controller.split("#") params = ctrl[1].split(":") req = Rack::Request.new(env) data = Model.new(req).call(params[1]) return ["200",{"Content-type" => "application/json"},[data]] end # Call the Controller request = Request.new request.request.params = Rack::Utils.parse_query(env["QUERY_STRING"]) if(matcheddata != nil) request.request.params.merge! (matcheddata) end request.request.params.default = "" infoctrl = controller.split("#") ctrl = Object.const_get(infoctrl[0]).new(request) response = ctrl.send(infoctrl[1]) if(@settings.server != "thin") response = [response] end [ctrl.info.response.status_code,ctrl.info.response.headers,response] end |
#routes(&block) ⇒ Object
86 87 88 |
# File 'lib/gors.rb', line 86 def routes &block @routesklass.instance_eval &block end |
#run ⇒ Object
15 16 17 |
# File 'lib/gors.rb', line 15 def run end |
#settings {|@settings| ... } ⇒ Object
90 91 92 |
# File 'lib/gors.rb', line 90 def settings yield(@settings) end |
#start ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/gors.rb', line 69 def start if(@settings.errorhandler != nil) @errorhandler = Object.const_get(@settings.errorhandler).new else @errorhandler = Gors::DefaultErrorHandler.new end if(@settings.daemon) puts "Sending Gors to background" system("kill `cat running.pid`") Process.daemon true File.write("running.pid",Process.pid) end @logger.log @routesklass.inspect Rack::Server.new({:app => self,:server => @settings.server, :Port => @settings.port}).start end |