Class: Portless::Config

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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_portObject (readonly)

Returns the value of attribute app_port.



10
11
12
# File 'lib/portless/config.rb', line 10

def app_port
  @app_port
end

#appsObject (readonly)

Returns the value of attribute apps.



10
11
12
# File 'lib/portless/config.rb', line 10

def apps
  @apps
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/portless/config.rb', line 10

def name
  @name
end

#tldObject (readonly)

Returns the value of attribute tld.



10
11
12
# File 'lib/portless/config.rb', line 10

def tld
  @tld
end

#tlsObject (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.message}"
end

Instance Method Details

#hostnameObject

The full hostname an app registers (e.g. "shirabe.org.localhost" when tld is set to that, or ".localhost" by default).



28
29
30
# File 'lib/portless/config.rb', line 28

def hostname
  tld.split(".").include?(name) ? tld : "#{name}.#{tld}"
end

#tld_warningObject

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