From a6dabe8bf308b660f7a26e5d2dc702c69b9b5210 Mon Sep 17 00:00:00 2001 From: Ben Busby Date: Wed, 27 Jul 2022 13:50:23 -0600 Subject: [PATCH] Make conn values and services path configurable at runtime Connection values (such as redis server port and the port to run farside on) as well as the services json file to use can now be set via environment variables: FARSIDE_PORT sets the port for Farside to run on FARSIDE_REDIS_PORT sets the redis server port for Farside to use FARSIDE_SERVICES_JSON sets the services json file for Farside to use This partially addresses the move towards de-listing Cloudflare instances by default by allowing different services json files to be used with different redis servers. See #43 --- README.md | 7 +++++-- config/config.exs | 3 --- config/runtime.exs | 6 ++++++ lib/farside/application.ex | 13 +++++++++---- lib/farside/instances.ex | 4 ++-- test/farside_test.exs | 4 ++-- 6 files changed, 24 insertions(+), 13 deletions(-) create mode 100644 config/runtime.exs diff --git a/README.md b/README.md index 4be0952..8a05aff 100644 --- a/README.md +++ b/README.md @@ -63,8 +63,8 @@ Farside's routing is very minimal, with only the following routes: particular service with the specified path - Ex: `/libreddit/r/popular` would navigate to `/r/popular` - - If the service provided is actually a URL to a "parent" service - (i.e. "youtube.com" instead of "piped" or "invidious"), Farside + - If the service provided is actually a URL to a "parent" service + (i.e. "youtube.com" instead of "piped" or "invidious"), Farside will determine the correct frontend to use for the specified URL. - Note that a path is not required. `/libreddit` for example will still redirect the user to a working libreddit instance @@ -108,3 +108,6 @@ request per second per IP. | Name | Purpose | | -- | -- | | FARSIDE_TEST | If enabled, bypasses the instance availability check and adds all instances to the pool. | +| FARSIDE_PORT | The port to run Farside on (default: `4001`) | +| FARSIDE_REDIS_PORT | The Redis server port to use (default: `6379`, same as the default for Redis) | +| FARSIDE_SERVICES_JSON | The "services" JSON file to use for selecting instances (default: `services.json`) | diff --git a/config/config.exs b/config/config.exs index d1ec18d..11f61d9 100644 --- a/config/config.exs +++ b/config/config.exs @@ -1,13 +1,10 @@ import Config config :farside, - port: 4001, - redis_conn: "redis://localhost:6379", update_file: ".update-results", service_prefix: "service-", fallback_suffix: "-fallback", previous_suffix: "-previous", - services_json: "services.json", index: "index.eex", route: "route.eex", headers: [ diff --git a/config/runtime.exs b/config/runtime.exs new file mode 100644 index 0000000..e4491ff --- /dev/null +++ b/config/runtime.exs @@ -0,0 +1,6 @@ +import Config + +config :farside, + port: System.get_env("FARSIDE_PORT", "4001"), + redis_conn: "redis://localhost:#{System.get_env("FARSIDE_REDIS_PORT", "6379")}", + services_json: System.get_env("FARSIDE_SERVICES_JSON", "services.json") diff --git a/lib/farside/application.ex b/lib/farside/application.ex index 9fd6e9d..0ab77ea 100644 --- a/lib/farside/application.ex +++ b/lib/farside/application.ex @@ -1,22 +1,27 @@ defmodule Farside.Application do - @farside_port Application.fetch_env!(:farside, :port) - @redis_conn Application.fetch_env!(:farside, :redis_conn) + #@farside_port Application.fetch_env!(:farside, :port) + #@redis_conn Application.fetch_env!(:farside, :redis_conn) @moduledoc false use Application @impl true def start(_type, _args) do + redis_conn = Application.fetch_env!(:farside, :redis_conn) + farside_port = Application.fetch_env!(:farside, :port) + IO.puts "Runing on http://localhost:#{farside_port}" + IO.puts "Redis conn: #{redis_conn}" + children = [ Plug.Cowboy.child_spec( scheme: :http, plug: Farside.Router, options: [ - port: @farside_port + port: String.to_integer(farside_port) ] ), {PlugAttack.Storage.Ets, name: Farside.Throttle.Storage, clean_period: 60_000}, - {Redix, {@redis_conn, [name: :redix]}}, + {Redix, {redis_conn, [name: :redix]}}, Farside.Scheduler, Farside.Server ] diff --git a/lib/farside/instances.ex b/lib/farside/instances.ex index 625bbb4..f37f306 100644 --- a/lib/farside/instances.ex +++ b/lib/farside/instances.ex @@ -1,7 +1,6 @@ defmodule Farside.Instances do @fallback_suffix Application.fetch_env!(:farside, :fallback_suffix) @update_file Application.fetch_env!(:farside, :update_file) - @services_json Application.fetch_env!(:farside, :services_json) @service_prefix Application.fetch_env!(:farside, :service_prefix) @headers Application.fetch_env!(:farside, :headers) @queries Application.fetch_env!(:farside, :queries) @@ -42,7 +41,8 @@ defmodule Farside.Instances do end def update() do - {:ok, file} = File.read(@services_json) + services_json = Application.fetch_env!(:farside, :services_json) + {:ok, file} = File.read(services_json) {:ok, json} = Jason.decode(file) # Loop through all instances and check each for availability diff --git a/test/farside_test.exs b/test/farside_test.exs index 25828c6..d47396e 100644 --- a/test/farside_test.exs +++ b/test/farside_test.exs @@ -1,5 +1,4 @@ defmodule FarsideTest do - @services_json Application.fetch_env!(:farside, :services_json) use ExUnit.Case use Plug.Test @@ -49,7 +48,8 @@ defmodule FarsideTest do end test "/:service" do - {:ok, file} = File.read(@services_json) + services_json = Application.fetch_env!(:farside, :services_json) + {:ok, file} = File.read(services_json) {:ok, service_list} = Jason.decode(file) service_names =