Class: Portless::Config
- Inherits:
-
Object
- Object
- Portless::Config
- Defined in:
- lib/portless/config.rb
Overview
Per-project config: an optional portless.json (or portless.yml) plus inferred defaults. Mirrors portless's config.ts/auto.ts: name is inferred from the config, the git root, or the directory; tld defaults to "localhost".
Constant Summary collapse
- RISKY_TLDS =
Real/reserved TLDs that can intercept live traffic or clash with mDNS.
%w[dev app page zip mov local].freeze
Instance Attribute Summary collapse
-
#app_port ⇒ Object
readonly
Returns the value of attribute app_port.
-
#apps ⇒ Object
readonly
Returns the value of attribute apps.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#tld ⇒ Object
readonly
Returns the value of attribute tld.
-
#tls ⇒ Object
readonly
Returns the value of attribute tls.
Class Method Summary collapse
Instance Method Summary collapse
-
#hostname ⇒ Object
The full hostname an app registers (e.g. "shirabe.org.localhost" when tld is set to that, or "
.localhost" by default). -
#initialize(data, dir = Dir.pwd) ⇒ Config
constructor
A new instance of Config.
-
#tld_warning ⇒ Object
A warning string if the tld looks risky, else nil.
Constructor Details
#initialize(data, dir = Dir.pwd) ⇒ Config
Returns a new instance of Config.
16 17 18 19 20 21 22 23 24 |
# File 'lib/portless/config.rb', line 16 def initialize(data, dir = Dir.pwd) @dir = dir @name = sanitize_label(data["name"] || infer_name(dir)) @tld = (data["tld"] || Constants::DEFAULT_TLD).to_s @app_port = data["appPort"] || data["app_port"] @tls = data.fetch("tls", true) # Monorepo: { "apps": { "web": "bin/rails server", "api": "node api.js" } }. @apps = (data["apps"] || {}).transform_keys { |k| sanitize_label(k) } end |
Instance Attribute Details
#app_port ⇒ Object (readonly)
Returns the value of attribute app_port.
10 11 12 |
# File 'lib/portless/config.rb', line 10 def app_port @app_port end |
#apps ⇒ Object (readonly)
Returns the value of attribute apps.
10 11 12 |
# File 'lib/portless/config.rb', line 10 def apps @apps end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
10 11 12 |
# File 'lib/portless/config.rb', line 10 def name @name end |
#tld ⇒ Object (readonly)
Returns the value of attribute tld.
10 11 12 |
# File 'lib/portless/config.rb', line 10 def tld @tld end |
#tls ⇒ Object (readonly)
Returns the value of attribute tls.
10 11 12 |
# File 'lib/portless/config.rb', line 10 def tls @tls end |
Class Method Details
.load(dir = Dir.pwd) ⇒ Object
12 13 14 |
# File 'lib/portless/config.rb', line 12 def self.load(dir = Dir.pwd) new(read_file(dir), dir) end |
.read_file(dir) ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/portless/config.rb', line 43 def self.read_file(dir) json = File.join(dir, "portless.json") return JSON.parse(File.read(json)) if File.exist?(json) {} rescue JSON::ParserError => e raise Error, "invalid portless.json: #{e.}" end |
Instance Method Details
#hostname ⇒ Object
The full hostname an app registers (e.g. "shirabe.org.localhost" when tld is
set to that, or "
28 29 30 |
# File 'lib/portless/config.rb', line 28 def hostname tld.split(".").include?(name) ? tld : "#{name}.#{tld}" end |
#tld_warning ⇒ Object
A warning string if the tld looks risky, else nil. (.localhost / .test are safe.)
36 37 38 39 40 41 |
# File 'lib/portless/config.rb', line 36 def tld_warning last = tld.split(".").last return unless RISKY_TLDS.include?(last) "tld \".#{last}\" is a real/reserved TLD — prefer \".localhost\" so you don't intercept real traffic" end |