Class: IceJade::Cradle::Server
- Inherits:
-
Object
- Object
- IceJade::Cradle::Server
- Defined in:
- lib/ice_jade/cradle/server.rb
Overview
简易 HTTP 测试服务器
基于 Ruby 标准库 socket + thread,零外部依赖。 内置常用测试路由,支持 JSON / Form / Multipart, 用于本地测试 ice-jade 各分支(Quantum / Poster)的 HTTP 调用。
Defined Under Namespace
Classes: HTTPRequest, Route
Constant Summary collapse
- DEFAULT_PORT =
8765- DEFAULT_HOST =
'0.0.0.0'
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
Instance Method Summary collapse
-
#initialize(port: DEFAULT_PORT, host: DEFAULT_HOST, silent: false) ⇒ Server
constructor
A new instance of Server.
-
#route(method, path) {|req| ... } ⇒ Object
注册路由.
-
#start ⇒ Object
启动服务器(阻塞).
-
#stop ⇒ Object
停止服务器.
Constructor Details
#initialize(port: DEFAULT_PORT, host: DEFAULT_HOST, silent: false) ⇒ Server
Returns a new instance of Server.
28 29 30 31 32 33 34 35 36 |
# File 'lib/ice_jade/cradle/server.rb', line 28 def initialize(port: DEFAULT_PORT, host: DEFAULT_HOST, silent: false) @port = port @host = host @silent = silent @routes = [] @server = nil @running = false setup_default_routes end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
20 21 22 |
# File 'lib/ice_jade/cradle/server.rb', line 20 def host @host end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
20 21 22 |
# File 'lib/ice_jade/cradle/server.rb', line 20 def port @port end |
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
20 21 22 |
# File 'lib/ice_jade/cradle/server.rb', line 20 def routes @routes end |
Instance Method Details
#route(method, path) {|req| ... } ⇒ Object
注册路由
44 45 46 |
# File 'lib/ice_jade/cradle/server.rb', line 44 def route(method, path, &block) @routes << Route.new(method.to_s.upcase, path, block) end |
#start ⇒ Object
启动服务器(阻塞)
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/ice_jade/cradle/server.rb', line 49 def start @server = TCPServer.new(@host, @port) @running = true log "Cradle HTTP Server listening on http://#{@host}:#{@port}" log "Press Ctrl+C to stop" while @running begin socket = @server.accept Thread.new { handle_connection(socket) } rescue IOError break unless @running end end end |
#stop ⇒ Object
停止服务器
66 67 68 69 70 |
# File 'lib/ice_jade/cradle/server.rb', line 66 def stop @running = false @server&.close log "Server stopped." end |