Class Config
Where a server's settings come from, in one resolution order.
The shape is the one a Spring Boot developer already knows, because the problem is the same one: the SAME code has to run against a SQLite file on a laptop and a managed PostgreSQL in production, and the difference between those two cannot live in the source. It lives here.
# application.properties, committed
cn1.datasource.url=${DATABASE_URL}
cn1.server.port=8080
# application-dev.properties, also committed
cn1.datasource.url=:memory:
CN1_PROFILE=dev ./server # SQLite, nothing installed
DATABASE_URL=postgres://user:pw@db/app ./server # production
A key is looked for in this order, and the first layer that has it wins:
-
a system property of exactly that name (
-Dcn1.server.port=9000), which only the local JVM loop can set; -
an environment variable of the name in upper case with dots as underscores (
CN1_SERVER_PORT), which is how a container sets one; -
the environment variable a platform already sets for it, where one exists:
PORTandDATABASE_URLare set for you by every PaaS worth the name, and a server that ignored them would need a wrapper script to start at all; -
application-<profile>.properties; -
application.properties; -
the default the caller passed in.
A value may reference an environment variable as ${NAME} or
${NAME:fallback}. That resolution happens when the value is READ
rather than when the file is loaded, which is what lets a committed
application.properties name a variable that only production sets: the dev
profile overrides the key, so the unset variable is never looked at. A
reference that IS read and cannot be resolved is an error rather than a value
with a dollar sign in it -- the alternative is a server that tries to open a
SQLite file named "${DATABASE_URL}".
Nothing here is required. A binary with no properties file beside it reads its whole configuration from the environment, which is the normal shape for a container built FROM SCRATCH: there is no file next to the binary because there is nothing next to the binary.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringHow long a borrow waits for a free connection, in milliseconds.static final StringHow long SQLite waits on a locked database, in milliseconds.static final StringHow many connections the pool holds.static final StringThe database, as a SQLite path or a PostgreSQL or MySQL URL.static final StringThe directory the properties files are read from.static final StringWhether the generated daos create their tables at start-up.static final StringWhich profile is active.static final StringThe listen backlog.static final StringThe port to listen on.static final StringHow long a stop waits for requests in flight, in milliseconds.static final StringThe size of the request thread pool.static final StringThe Cache-Control header those files carry.static final StringThe file a directory request is answered with.static final StringThe path prefix those files are served under.static final StringA directory to serve static files from.static final StringA PEM certificate chain to terminate TLS with.static final StringWhether to offer HTTP/2 through ALPN when TLS is terminated here.static final StringThe private key forTLS_CERTIFICATE. -
Method Summary
Modifier and TypeMethodDescriptiondescribe()What was read, for a start-up line.The value forkey, or null when no layer has one.The value forkey, orfallbackwhen no layer has one.booleangetBoolean(String key, boolean fallback) The value forkeyas a flag, orfallback.intThe value forkeyas a number, orfallback.The active profile: "default" unless something named another.booleanWhether the active profile is a development one -- dev, development, test or local.static Configload()Reads the configuration for this process: the active profile, then the two properties files, fromLOCATIONor the working directory.static ConfigReads the configuration from properties files indirectory.static ConfigA configuration with no files behind it, holding exactly what it is given.
-
Field Details
-
PROFILE
-
LOCATION
The directory the properties files are read from. Defaults to ".".- See Also:
-
SERVER_PORT
-
SERVER_BACKLOG
-
SERVER_WORKERS
-
SERVER_SHUTDOWN_MILLIS
How long a stop waits for requests in flight, in milliseconds.- See Also:
-
TLS_CERTIFICATE
-
TLS_KEY
-
TLS_HTTP2
Whether to offer HTTP/2 through ALPN when TLS is terminated here.- See Also:
-
STATIC_ROOT
-
STATIC_PREFIX
The path prefix those files are served under. Defaults to /static.- See Also:
-
STATIC_INDEX
The file a directory request is answered with. Defaults to index.html.- See Also:
-
STATIC_CACHE_CONTROL
The Cache-Control header those files carry.- See Also:
-
DATASOURCE_URL
The database, as a SQLite path or a PostgreSQL or MySQL URL. Also read from DATABASE_URL.- See Also:
-
DATASOURCE_POOL_SIZE
-
DATASOURCE_BORROW_MILLIS
How long a borrow waits for a free connection, in milliseconds.- See Also:
-
DATASOURCE_BUSY_MILLIS
How long SQLite waits on a locked database, in milliseconds.- See Also:
-
ORM_CREATE_TABLES
Whether the generated daos create their tables at start-up. Defaults to true on a development profile and false everywhere else: a laptop wants a schema without being asked, and production wants its migrations run by whatever runs migrations.- See Also:
-
-
Method Details
-
load
Reads the configuration for this process: the active profile, then the two properties files, fromLOCATIONor the working directory.- Throws:
IOException
-
load
Reads the configuration from properties files indirectory.- Throws:
IOException
-
of
-
getProfile
The active profile: "default" unless something named another. -
isDevelopmentProfile
public boolean isDevelopmentProfile()Whether the active profile is a development one -- dev, development, test or local.
This decides two defaults and nothing else: an unconfigured database becomes an in-memory SQLite one rather than a refusal, and the ORM creates its tables. Both are wrong in production and right on a laptop, and both are overridable by naming the key.
-
get
The value forkey, or null when no layer has one.- Throws:
IOException
-
get
The value forkey, orfallbackwhen no layer has one.- Throws:
IOException
-
getInt
The value forkeyas a number, orfallback.- Throws:
IOException
-
getBoolean
The value for
keyas a flag, orfallback."true", "yes", "on" and "1" are true; "false", "no", "off" and "0" are false; anything else is an error rather than false. A setting the operator spelled "ture" is a setting they believe is on.
- Throws:
IOException
-
describe
What was read, for a start-up line. NEVER any value: the datasource URL holds a password, and a configuration dump is how it reaches a log.
-