zf

zenflows testing
git clone https://s.sonu.ch/~srfsh/zf.git
Log | Files | Refs | Submodules | README | LICENSE

runtime.exs (3489B)


      1 # Zenflows is designed to implement the Valueflows vocabulary,
      2 # written and maintained by srfsh <info@dyne.org>.
      3 # Copyright (C) 2021-2023 Dyne.org foundation <foundation@dyne.org>.
      4 #
      5 # This program is free software: you can redistribute it and/or modify
      6 # it under the terms of the GNU Affero General Public License as published by
      7 # the Free Software Foundation, either version 3 of the License, or
      8 # (at your option) any later version.
      9 #
     10 # This program is distributed in the hope that it will be useful,
     11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 # GNU Affero General Public License for more details.
     14 #
     15 # You should have received a copy of the GNU Affero General Public License
     16 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
     17 
     18 import Config
     19 import System, only: [fetch_env!: 1, fetch_env: 1, get_env: 2]
     20 
     21 # Just like `System.get_env/2`, but converts the variable to integer if it
     22 # exists or fallbacks to `int`.  It is just a shortcut.
     23 get_env_int = fn varname, int ->
     24 	case fetch_env(varname) do
     25 		{:ok, val} -> String.to_integer(val)
     26 		:error -> int
     27 	end
     28 end
     29 
     30 get_env_url = fn varname, default ->
     31 	with {:ok, %{scheme: scheme, host: host, port: port}} when not is_nil(host) and scheme in ["http", "https"]
     32 				  <- URI.new(get_env(varname, default)) do
     33 		%{scheme: :"#{scheme}", host: host, port: port}
     34 	else
     35 		err -> raise err
     36 	end
     37 end
     38 
     39 #
     40 # database
     41 #
     42 db_conf = case {fetch_env("DB_URI"), fetch_env("DB_SOCK")} do
     43 	{{:ok, db_uri}, _} ->
     44 		[url: db_uri]
     45 
     46 	{_, {:ok, db_sock}} ->
     47 		db_name = case config_env() do
     48 			:prod -> get_env("DB_NAME", "zenflows")
     49 			:dev -> get_env("DB_NAME", "zenflows_dev")
     50 			:test -> get_env("DB_NAME", "zenflows_test")
     51 		end
     52 
     53 		[databese: db_name, socket: db_sock]
     54 
     55 	_ ->
     56 		db_name = case config_env() do
     57 			:prod -> get_env("DB_NAME", "zenflows")
     58 			:dev -> get_env("DB_NAME", "zenflows_dev")
     59 			:test -> get_env("DB_NAME", "zenflows_test")
     60 		end
     61 
     62 		db_port = get_env_int.("DB_PORT", 5432)
     63 		if db_port not in 0..65535,
     64 			do: raise "DB_PORT must be between 0 and 65535 inclusive"
     65 
     66 		[
     67 			database: db_name,
     68 			username: fetch_env!("DB_USER"),
     69 			password: fetch_env!("DB_PASS"),
     70 			hostname: get_env("DB_HOST", "localhost"),
     71 			port: db_port,
     72 		]
     73 end
     74 
     75 config :zenflows, Zenflows.DB.Repo, db_conf
     76 
     77 #
     78 # restroom
     79 #
     80 room_uri = get_env_url.("ROOM_URI", "http://localhost")
     81 config :zenflows, Zenflows.Restroom,
     82 	room_uri: room_uri,
     83 	room_salt: fetch_env!("ROOM_SALT")
     84 
     85 #
     86 # did
     87 #
     88 did_keyring = Base.decode64!(get_env("DID_KEYRING", ""))
     89 did_uri = get_env_url.("DID_URI", "https://did.dyne.org")
     90 config :zenflows, Zenflows.DID,
     91 	did_uri: did_uri,
     92 	did_keyring: if(did_keyring == "", do: nil, else: Jason.decode!(did_keyring))
     93 
     94 #
     95 # admin
     96 #
     97 admin_key = fetch_env!("ADMIN_KEY") |> Base.decode16!(case: :lower)
     98 if byte_size(admin_key) != 64,
     99 	do: raise "ADMIN_KEY must be a 64-octect long, lowercase-base16-encoded string"
    100 config :zenflows, Zenflows.Admin,
    101 	admin_key: admin_key
    102 
    103 #
    104 # gql
    105 #
    106 def_page_size = get_env_int.("GQL_DEF_PAGE_SIZE", 50)
    107 if def_page_size < 1,
    108 	do: raise "GQL_DEF_PAGE_SIZE must be greater than 1"
    109 max_page_size = get_env_int.("GQL_MAX_PAGE_SIZE", 100)
    110 if max_page_size < def_page_size,
    111 	do: raise "GQL_MAX_PAGE_SIZE can't be less than GQL_DEF_PAGE_SIZE"
    112 auth? = get_env("GQL_AUTH_CALLS", "true") != "false"
    113 config :zenflows, Zenflows.GQL,
    114 	def_page_size: def_page_size,
    115 	max_page_size: max_page_size,
    116 	authenticate_calls?: auth?