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(name = nil, worktree: true) ⇒ 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.
-
#worktree_prefix ⇒ Object
The git-worktree subdomain prefix for this project dir (nil if none).
Constructor Details
#initialize(data, dir = Dir.pwd) ⇒ Config
Returns a new instance of Config.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/portless/config.rb', line 16 def initialize(data, dir = Dir.pwd) @dir = dir @name = sanitize_label(data["name"] || infer_name(dir)) # Env overrides beat portless.json (the portless env-var contract): # PORTLESS_TLD replaces the tld, PORTLESS_HTTPS=0/1 forces TLS off/on. @tld = (ENV["PORTLESS_TLD"] || data["tld"] || Constants::DEFAULT_TLD).to_s @app_port = data["appPort"] || data["app_port"] @tls = data.fetch("tls", true) case ENV["PORTLESS_HTTPS"].to_s.downcase when "0", "false" then @tls = false when "1", "true" then @tls = true end # 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
62 63 64 65 66 67 68 69 |
# File 'lib/portless/config.rb', line 62 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(name = nil, worktree: true) ⇒ Object
The full hostname an app registers (e.g. "shirabe.org.localhost" when tld is
set to that, or "name overrides the base
(used by --name); worktree: prepends the git-worktree branch prefix so a
linked worktree gets its own URL (auth.<name>.localhost).
36 37 38 39 40 41 |
# File 'lib/portless/config.rb', line 36 def hostname(name = nil, worktree: true) base = name ? sanitize_label(name) : @name host = tld.split(".").include?(base) ? tld : "#{base}.#{tld}" prefix = worktree ? worktree_prefix : nil prefix ? "#{prefix}.#{host}" : host end |
#tld_warning ⇒ Object
A warning string if the tld looks risky, else nil. (.localhost / .test are safe.)
55 56 57 58 59 60 |
# File 'lib/portless/config.rb', line 55 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 |
#worktree_prefix ⇒ Object
The git-worktree subdomain prefix for this project dir (nil if none). Memoized — it shells out to git.
45 46 47 48 49 |
# File 'lib/portless/config.rb', line 45 def worktree_prefix return @worktree_prefix if defined?(@worktree_prefix) @worktree_prefix = Worktree.prefix(@dir) end |