Class: Mxrb::Oql::Server
- Inherits:
-
Object
- Object
- Mxrb::Oql::Server
- Defined in:
- lib/mxrb/oql/server.rb
Overview
Loopback-only JSON API for read-only queries against a synchronized Mendix Runtime database.
Constant Summary collapse
- MAX_BODY_BYTES =
1_048_576
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Instance Method Summary collapse
-
#execute(payload) ⇒ Object
rubocop:disable Metrics/MethodLength.
-
#initialize(project_path, host: '127.0.0.1', port: 4567, database_port: 55_432, workspace: nil, logger: nil) ⇒ Server
constructor
rubocop:disable Metrics/ParameterLists.
- #shutdown ⇒ Object
-
#start(prepare: true) {|@server| ... } ⇒ Object
rubocop:enable Metrics/ParameterLists.
Constructor Details
#initialize(project_path, host: '127.0.0.1', port: 4567, database_port: 55_432, workspace: nil, logger: nil) ⇒ Server
rubocop:disable Metrics/ParameterLists
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/mxrb/oql/server.rb', line 16 def initialize(project_path, host: '127.0.0.1', port: 4567, database_port: 55_432, workspace: nil, logger: nil) @host = host raise ArgumentError, 'the query server must bind to a loopback address' \ unless %w[127.0.0.1 ::1 localhost].include?(@host) @port = Integer(port) @workspace = workspace || Runtime::DatabaseWorkspace.new(project_path, port: database_port) @logger = logger || WEBrick::Log.new(File::NULL) end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
13 14 15 |
# File 'lib/mxrb/oql/server.rb', line 13 def host @host end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
13 14 15 |
# File 'lib/mxrb/oql/server.rb', line 13 def port @port end |
Instance Method Details
#execute(payload) ⇒ Object
rubocop:disable Metrics/MethodLength
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/mxrb/oql/server.rb', line 41 def execute(payload) started = Process.clock_gettime(Process::CLOCK_MONOTONIC) sql, warnings, params = query_sql(payload) rows = @workspace.query_rows(sql, params:) { ok: true, rows:, row_count: rows.size, elapsed_ms: elapsed_ms(started), warnings: } rescue ArgumentError, JSON::ParserError => e error_payload(e, started, 'invalid_request') rescue StandardError => e error_payload(e, started, 'query_failed') end |
#shutdown ⇒ Object
38 |
# File 'lib/mxrb/oql/server.rb', line 38 def shutdown = @server&.shutdown |
#start(prepare: true) {|@server| ... } ⇒ Object
rubocop:enable Metrics/ParameterLists
28 29 30 31 32 33 34 35 36 |
# File 'lib/mxrb/oql/server.rb', line 28 def start(prepare: true) @workspace.up if prepare @server = WEBrick::HTTPServer.new( BindAddress: @host, Port: @port, Logger: @logger, AccessLog: [] ) @server.mount_proc('/query') { |request, response| dispatch(request, response) } yield(@server) if block_given? @server.start end |