From 8042dcad0c61110111949a856c935325ca65f8ef Mon Sep 17 00:00:00 2001 From: Ben Busby Date: Sun, 7 Nov 2021 12:29:06 -0700 Subject: Refactor project to new name The name of the project is being refactored from Privacy Revolver to Farside. The reasoning behind this is: 1. A shorter name is easier to remember 2. It can stand for "FOSS alternative redirecting service" (which I know doesn't encapsulate all letters from "farside", but it's close enough). This commit also includes improvements to the update script for determining how far along the script is. --- lib/farside/application.ex | 17 +++++++++++++ lib/farside/router.ex | 51 +++++++++++++++++++++++++++++++++++++ lib/privacy_revolver/application.ex | 17 ------------- lib/privacy_revolver/router.ex | 51 ------------------------------------- 4 files changed, 68 insertions(+), 68 deletions(-) create mode 100644 lib/farside/application.ex create mode 100644 lib/farside/router.ex delete mode 100644 lib/privacy_revolver/application.ex delete mode 100644 lib/privacy_revolver/router.ex (limited to 'lib') diff --git a/lib/farside/application.ex b/lib/farside/application.ex new file mode 100644 index 0000000..708ff0d --- /dev/null +++ b/lib/farside/application.ex @@ -0,0 +1,17 @@ +defmodule Farside.Application do + @redis_conn Application.fetch_env!(:farside, :redis_conn) + @moduledoc false + + use Application + + @impl true + def start(_type, _args) do + children = [ + Plug.Cowboy.child_spec(scheme: :http, plug: Farside.Router, options: [port: 4001]), + {Redix, {@redis_conn, [name: :redix]}} + ] + + opts = [strategy: :one_for_one, name: Farside.Supervisor] + Supervisor.start_link(children, opts) + end +end diff --git a/lib/farside/router.ex b/lib/farside/router.ex new file mode 100644 index 0000000..95d9b41 --- /dev/null +++ b/lib/farside/router.ex @@ -0,0 +1,51 @@ +defmodule Farside.Router do + @fallback_str Application.fetch_env!(:farside, :fallback_str) + + use Plug.Router + + plug(:match) + plug(:dispatch) + + get "/" do + send_resp(conn, 200, "") + end + + get "/ping" do + # Useful for app healthcheck + {:ok, resp} = Redix.command(:redix, ["PING"]) + send_resp(conn, 200, resp) + end + + get "/:service/*glob" do + path = Enum.join(glob, "/") + + {:ok, instances} = + Redix.command( + :redix, + ["LRANGE", service, "0", "-1"] + ) + + # Either pick a random available instance, + # or fall back to the default one + instance = + if Enum.count(instances) > 0 do + Enum.random(instances) + else + {:ok, result} = + Redix.command( + :redix, + ["GET", "#{service}#{@fallback_str}"] + ) + + result + end + + # Redirect to the available instance + conn + |> Plug.Conn.resp(:found, "") + |> Plug.Conn.put_resp_header( + "location", + "#{instance}/#{path}" + ) + end +end diff --git a/lib/privacy_revolver/application.ex b/lib/privacy_revolver/application.ex deleted file mode 100644 index 04b7540..0000000 --- a/lib/privacy_revolver/application.ex +++ /dev/null @@ -1,17 +0,0 @@ -defmodule PrivacyRevolver.Application do - @redis_conn Application.fetch_env!(:privacy_revolver, :redis_conn) - @moduledoc false - - use Application - - @impl true - def start(_type, _args) do - children = [ - Plug.Cowboy.child_spec(scheme: :http, plug: PrivacyRevolver.Router, options: [port: 4001]), - {Redix, {@redis_conn, [name: :redix]}} - ] - - opts = [strategy: :one_for_one, name: PrivacyRevolver.Supervisor] - Supervisor.start_link(children, opts) - end -end diff --git a/lib/privacy_revolver/router.ex b/lib/privacy_revolver/router.ex deleted file mode 100644 index 2a51094..0000000 --- a/lib/privacy_revolver/router.ex +++ /dev/null @@ -1,51 +0,0 @@ -defmodule PrivacyRevolver.Router do - @fallback_str Application.fetch_env!(:privacy_revolver, :fallback_str) - - use Plug.Router - - plug(:match) - plug(:dispatch) - - get "/" do - send_resp(conn, 200, "") - end - - get "/ping" do - # Useful for app healthcheck - {:ok, resp} = Redix.command(:redix, ["PING"]) - send_resp(conn, 200, resp) - end - - get "/:service/*glob" do - path = Enum.join(glob, "/") - - {:ok, instances} = - Redix.command( - :redix, - ["LRANGE", service, "0", "-1"] - ) - - # Either pick a random available instance, - # or fall back to the default one - instance = - if Enum.count(instances) > 0 do - Enum.random(instances) - else - {:ok, result} = - Redix.command( - :redix, - ["GET", "#{service}#{@fallback_str}"] - ) - - result - end - - # Redirect to the available instance - conn - |> Plug.Conn.resp(:found, "") - |> Plug.Conn.put_resp_header( - "location", - "#{instance}/#{path}" - ) - end -end -- cgit v1.2.3