Class: Mxrb::DomainDiagram::Server

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

Overview

Loopback-only server for the standalone editor.

Constant Summary collapse

MAX_BODY_BYTES =
2_097_152
LOOPBACK_HOSTS =
%w[127.0.0.1 ::1 localhost].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, output: nil, modules: [], host: '127.0.0.1', port: 4568, force: false, reuse: false, lifecycle_token: nil) ⇒ Server

Returns a new instance of Server.

Raises:

  • (ArgumentError)


269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/mxrb/domain_diagram.rb', line 269

def initialize(source, output: nil, modules: [], host: '127.0.0.1', port: 4568, force: false,
               reuse: false, lifecycle_token: nil)
  @source = File.expand_path(source)
  raise ArgumentError, "MPR not found: #{@source}" unless File.file?(@source)

  @output = File.expand_path(output || default_output)
  raise ArgumentError, 'output must be a safe copy, not the source MPR' if same_file_as_source?
  raise ArgumentError, "output cannot be a directory: #{@output}" if File.directory?(@output)
  raise ArgumentError, "output cannot be a symbolic link: #{@output}" if File.symlink?(@output)

  @host = host.to_s
  raise ArgumentError, 'domain model server must bind to loopback' unless LOOPBACK_HOSTS.include?(@host)

  @port = Integer(port)
  @modules = Array(modules)
  @token = SecureRandom.hex(24)
  @lifecycle_token = lifecycle_token
  @managed_paths = [@output]
  prepare_output(force:, reuse:)
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



267
268
269
# File 'lib/mxrb/domain_diagram.rb', line 267

def host
  @host
end

#managed_pathsObject (readonly)

Returns the value of attribute managed_paths.



267
268
269
# File 'lib/mxrb/domain_diagram.rb', line 267

def managed_paths
  @managed_paths
end

#outputObject (readonly)

Returns the value of attribute output.



267
268
269
# File 'lib/mxrb/domain_diagram.rb', line 267

def output
  @output
end

#portObject (readonly)

Returns the value of attribute port.



267
268
269
# File 'lib/mxrb/domain_diagram.rb', line 267

def port
  @port
end

#sourceObject (readonly)

Returns the value of attribute source.



267
268
269
# File 'lib/mxrb/domain_diagram.rb', line 267

def source
  @source
end

Instance Method Details

#shutdownObject



295
# File 'lib/mxrb/domain_diagram.rb', line 295

def shutdown = @server&.shutdown

#startObject



290
291
292
293
# File 'lib/mxrb/domain_diagram.rb', line 290

def start
  @server = Http::Server.new(host:, port:) { dispatch(_1, _2) }
  @server.start { yield(_1) if block_given? }
end