Class: Oximg::Server::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/oximg/server/config.rb

Overview

The client-side half of an oximg deployment's configuration: where the server is, and the signing material it was started with. What the server does with a request (quality profiles, source origin, memory caps) is server-side config and is deliberately not restated here — a knob duplicated on both sides is a knob that drifts.

The defaults read the same environment variables the server itself reads (+OXIMG_KEY+, OXIMG_SALT, OXIMG_OPTIONS_PREFIX), so an app deployed alongside a configured oximg needs no Ruby-side setup at all. OXIMG_ENDPOINT is client-only: the server has no notion of its own public URL.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env: ENV) ⇒ Config

Returns a new instance of Config.



34
35
36
37
38
39
# File 'lib/oximg/server/config.rb', line 34

def initialize(env: ENV)
  self.endpoint = env["OXIMG_ENDPOINT"]
  self.key = env["OXIMG_KEY"]
  self.salt = env["OXIMG_SALT"]
  self.options_prefix = env["OXIMG_OPTIONS_PREFIX"]
end

Instance Attribute Details

#endpointObject

Root of the oximg deployment, without a trailing slash (+https://img.example.com+). Leave it nil to emit root-relative URLs — the right answer when oximg is mounted on the app's own origin behind a CDN.



21
22
23
# File 'lib/oximg/server/config.rb', line 21

def endpoint
  @endpoint
end

#keyObject

Hex +OXIMG_KEY+/+OXIMG_SALT+, decoded to bytes. Both nil means signing is off; exactly one set is a configuration error, checked at URL-build time (the server refuses to boot on the same half-configuration rather than serving unsigned).



27
28
29
# File 'lib/oximg/server/config.rb', line 27

def key
  @key
end

#options_prefixObject

Mount point of the Cloudflare-Images-style options route, matching the server's OXIMG_OPTIONS_PREFIX (e.g. /image). Nil means the route is not mounted and #options_url_for is unavailable.



32
33
34
# File 'lib/oximg/server/config.rb', line 32

def options_prefix
  @options_prefix
end

#saltObject

Hex +OXIMG_KEY+/+OXIMG_SALT+, decoded to bytes. Both nil means signing is off; exactly one set is a configuration error, checked at URL-build time (the server refuses to boot on the same half-configuration rather than serving unsigned).



27
28
29
# File 'lib/oximg/server/config.rb', line 27

def salt
  @salt
end

Instance Method Details

#signing?Boolean

True when URLs will carry a signature.

Returns:

  • (Boolean)


64
65
66
# File 'lib/oximg/server/config.rb', line 64

def signing?
  !@key.nil? && !@salt.nil?
end

#validate!Object

Fail closed the way the server does: a half-configured signing pair must never quietly produce unsigned URLs that the server will answer with 403.

Raises:



71
72
73
74
75
76
# File 'lib/oximg/server/config.rb', line 71

def validate!
  return if @key.nil? == @salt.nil?

  raise ConfigurationError,
    "key and salt must both be set to enable signing (got only #{@key.nil? ? "salt" : "key"})"
end