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
This commit is contained in:
Ben Busby 2022-07-27 13:50:23 -06:00
parent 7045b62ccf
commit a6dabe8bf3
No known key found for this signature in database
GPG key ID: B9B7231E01D924A1
6 changed files with 24 additions and 13 deletions

View file

@ -63,8 +63,8 @@ Farside's routing is very minimal, with only the following routes:
particular service with the specified path particular service with the specified path
- Ex: `/libreddit/r/popular` would navigate to `<libreddit instance - Ex: `/libreddit/r/popular` would navigate to `<libreddit instance
URL>/r/popular` URL>/r/popular`
- If the service provided is actually a URL to a "parent" service - If the service provided is actually a URL to a "parent" service
(i.e. "youtube.com" instead of "piped" or "invidious"), Farside (i.e. "youtube.com" instead of "piped" or "invidious"), Farside
will determine the correct frontend to use for the specified URL. will determine the correct frontend to use for the specified URL.
- Note that a path is not required. `/libreddit` for example will still - Note that a path is not required. `/libreddit` for example will still
redirect the user to a working libreddit instance redirect the user to a working libreddit instance
@ -108,3 +108,6 @@ request per second per IP.
| Name | Purpose | | Name | Purpose |
| -- | -- | | -- | -- |
| FARSIDE_TEST | If enabled, bypasses the instance availability check and adds all instances to the pool. | | 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`) |

View file

@ -1,13 +1,10 @@
import Config import Config
config :farside, config :farside,
port: 4001,
redis_conn: "redis://localhost:6379",
update_file: ".update-results", update_file: ".update-results",
service_prefix: "service-", service_prefix: "service-",
fallback_suffix: "-fallback", fallback_suffix: "-fallback",
previous_suffix: "-previous", previous_suffix: "-previous",
services_json: "services.json",
index: "index.eex", index: "index.eex",
route: "route.eex", route: "route.eex",
headers: [ headers: [

6
config/runtime.exs Normal file
View file

@ -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")

View file

@ -1,22 +1,27 @@
defmodule Farside.Application do defmodule Farside.Application do
@farside_port Application.fetch_env!(:farside, :port) #@farside_port Application.fetch_env!(:farside, :port)
@redis_conn Application.fetch_env!(:farside, :redis_conn) #@redis_conn Application.fetch_env!(:farside, :redis_conn)
@moduledoc false @moduledoc false
use Application use Application
@impl true @impl true
def start(_type, _args) do 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 = [ children = [
Plug.Cowboy.child_spec( Plug.Cowboy.child_spec(
scheme: :http, scheme: :http,
plug: Farside.Router, plug: Farside.Router,
options: [ options: [
port: @farside_port port: String.to_integer(farside_port)
] ]
), ),
{PlugAttack.Storage.Ets, name: Farside.Throttle.Storage, clean_period: 60_000}, {PlugAttack.Storage.Ets, name: Farside.Throttle.Storage, clean_period: 60_000},
{Redix, {@redis_conn, [name: :redix]}}, {Redix, {redis_conn, [name: :redix]}},
Farside.Scheduler, Farside.Scheduler,
Farside.Server Farside.Server
] ]

View file

@ -1,7 +1,6 @@
defmodule Farside.Instances do defmodule Farside.Instances do
@fallback_suffix Application.fetch_env!(:farside, :fallback_suffix) @fallback_suffix Application.fetch_env!(:farside, :fallback_suffix)
@update_file Application.fetch_env!(:farside, :update_file) @update_file Application.fetch_env!(:farside, :update_file)
@services_json Application.fetch_env!(:farside, :services_json)
@service_prefix Application.fetch_env!(:farside, :service_prefix) @service_prefix Application.fetch_env!(:farside, :service_prefix)
@headers Application.fetch_env!(:farside, :headers) @headers Application.fetch_env!(:farside, :headers)
@queries Application.fetch_env!(:farside, :queries) @queries Application.fetch_env!(:farside, :queries)
@ -42,7 +41,8 @@ defmodule Farside.Instances do
end end
def update() do 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) {:ok, json} = Jason.decode(file)
# Loop through all instances and check each for availability # Loop through all instances and check each for availability

View file

@ -1,5 +1,4 @@
defmodule FarsideTest do defmodule FarsideTest do
@services_json Application.fetch_env!(:farside, :services_json)
use ExUnit.Case use ExUnit.Case
use Plug.Test use Plug.Test
@ -49,7 +48,8 @@ defmodule FarsideTest do
end end
test "/:service" do 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) {:ok, service_list} = Jason.decode(file)
service_names = service_names =