Deployment and lifecycle management for Nix
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

server: refactor config defaults

+31 -27
+31 -27
config/runtime.exs
··· 134 134 135 135 config_file = System.get_env("SOWER_SERVER_CONFIG_FILE", "/etc/sower/server.json") 136 136 137 + defaults = %{ 138 + "listen_address" => "127.0.0.1", 139 + "listen_port" => 4000, 140 + "nix_caches" => [] 141 + } 142 + 137 143 json_config = 138 144 with {:ok, contents} <- File.read(config_file), 139 145 {:ok, json} <- Jason.decode(contents), 140 146 :ok <- ExJsonSchema.Validator.validate(ExJsonSchema.Schema.resolve(@schema), json) do 141 - json |> atomize() 147 + defaults |> Map.merge(json) |> atomize() 142 148 else 143 149 {:error, err} -> 144 150 Logger.error(~s"Failed to read configuration file #{config_file}") ··· 158 164 Logger.debug(json_config) 159 165 json_config = json_config |> Keyword.delete(:log_level) 160 166 161 - # load some defaults 162 - public_url = json_config |> Keyword.get(:public_url, "http://127.0.0.1:4000") 167 + # compute urls 168 + public_url = json_config |> Keyword.fetch!(:public_url) 163 169 164 170 json_config = 165 171 json_config 166 172 |> Keyword.put( 167 173 :auth, 168 174 json_config 169 - |> Keyword.get(:auth) 175 + |> Keyword.fetch!(:auth) 170 176 |> Keyword.put(:oidc_redirect_uri, ~s"#{public_url}/auth") 171 177 ) 172 178 173 - listen_address = json_config |> Keyword.get(:listen_address, "127.0.0.1") 174 - listen_port = json_config |> Keyword.get(:listen_port, 4000) 175 - 176 - if not Keyword.has_key?(json_config, :nix_caches) do 177 - json_config = json_config |> Keyword.put(:nix_caches, []) 178 - end 179 - 180 179 secret_key_base = 181 - with secret_key_base_file <- json_config |> Keyword.get(:secret_key_base_file), 180 + with {:ok, secret_key_base_file} <- json_config |> Keyword.fetch(:secret_key_base_file), 182 181 {:ok, secret_key_base} <- read_credential(secret_key_base_file) do 183 182 secret_key_base 184 183 else 185 - {:error, err} -> 186 - Logger.warning("Failed to load secret_key_base from secret file, #{err}.") 184 + :error -> 185 + Logger.warning("Configuration is missing `secret_key_base_file`.") 187 186 Kernel.exit(1) 188 187 189 - [_ | _] -> 190 - Logger.warning("No secret_key_base_file in configuration. Exiting!") 188 + {:error, err} -> 189 + Logger.warning("Failed to load secret_key_base from secret file, #{err}.") 191 190 Kernel.exit(1) 192 191 end 193 192 193 + # database password file 194 194 json_config = 195 - with database <- json_config |> Keyword.get(:database), 196 - password_file <- database |> Keyword.get(:password_file), 195 + with {:ok, database} <- json_config |> Keyword.fetch(:database), 196 + {:ok, password_file} <- database |> Keyword.fetch(:password_file), 197 197 {:ok, password} <- read_credential(password_file) do 198 198 json_config |> Keyword.put(:database, database |> Keyword.put(:password, password)) 199 199 else 200 200 # assume missing password_file is intentional 201 - [_ | _] -> 202 - Logger.debug("No database password_file to read.") 201 + :error -> 202 + Logger.debug("Configuration does not have `database.password_file` to read. Skipping.") 203 203 json_config 204 204 205 205 {:error, err} -> ··· 207 207 json_config 208 208 end 209 209 210 + # oidc client secret file 210 211 json_config = 211 - with auth <- json_config |> Keyword.get(:auth), 212 - oidc_client_secret_file <- auth |> Keyword.get(:oidc_client_secret_file), 212 + with {:ok, auth} <- json_config |> Keyword.fetch(:auth), 213 + {:ok, oidc_client_secret_file} <- auth |> Keyword.fetch(:oidc_client_secret_file), 213 214 {:ok, oidc_client_secret} <- read_credential(oidc_client_secret_file) do 214 215 json_config 215 216 |> Keyword.put(:auth, auth |> Keyword.put(:oidc_client_secret, oidc_client_secret)) ··· 218 219 Logger.warning("Failed to load oidc_client_secret from secret file, #{err}.") 219 220 Kernel.exit(1) 220 221 221 - [_ | _] -> 222 - Logger.warning("No auth.oidc_client_secret_file in configuration. Exiting!") 222 + :error -> 223 + Logger.warning("Configuration is missing `auth.oidc_client_secret_file`.") 223 224 Kernel.exit(1) 224 225 end 225 226 226 - Logger.debug("Modified configuration") 227 + Logger.debug("Final configuration:") 227 228 Logger.debug(json_config) 228 229 229 230 json_config |> Enum.map(fn {k, v} -> put_config(k, v) end) ··· 233 234 234 235 put_config(SowerWeb.Endpoint, 235 236 url: [host: host, port: port, scheme: scheme], 236 - http: [ip: ip_to_inet(listen_address), port: listen_port], 237 + http: [ 238 + ip: ip_to_inet(json_config |> Keyword.fetch!(:listen_address)), 239 + port: json_config |> Keyword.fetch!(:listen_port) 240 + ], 237 241 secret_key_base: secret_key_base, 238 242 persistent: true 239 243 ) 240 244 241 - Logger.info("Finished loading configuration") 245 + Logger.info("Finished loading configuration.") 242 246 end 243 247 244 248 defp atomize([head | rest]) do