From 0aaac2879b2c1d622e9e1ace92fc359c1d07855f Mon Sep 17 00:00:00 2001 From: ohyecloudy Date: Wed, 26 Apr 2023 21:05:54 +0900 Subject: [PATCH] =?UTF-8?q?:star:=20twitter=20account=EC=97=90=20=EA=B4=80?= =?UTF-8?q?=ED=95=9C=20=EC=84=A4=EC=A0=95=EC=9D=84=20=EC=9D=BD=EA=B3=A0=20?= =?UTF-8?q?=EC=8B=A4=ED=96=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - twitter account 정보로 TwitterProcess 프로세스를 만드는 사전 작업 - 필요한 정보를 읽을 뿐 프로세스를 생성하지는 않는다 - runtime.exs - 환경 변수에서 설정을 읽어서 :tbot800, :tbot_accounts 설정 - dev.secret.exs - 개발 중에는 바로 :tbot800, :tbot_accounts 설정 - 버전 컨트롤에서 파일 제외 --- .gitignore | 2 + apps/tbot800/lib/tbot800/application.ex | 12 +++++ apps/tbot800/lib/tbot800/default_impl.ex | 1 + .../lib/tbot800/default_impl/supervisor.ex | 26 ++++++++++ apps/tbot800/mix.exs | 3 +- config/config.exs | 2 + config/dev.exs | 5 ++ config/prod.exs | 1 + config/runtime.exs | 47 +++++++++++++++++++ config/test.exs | 1 + 10 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 apps/tbot800/lib/tbot800/application.ex create mode 100644 apps/tbot800/lib/tbot800/default_impl/supervisor.ex create mode 100644 config/dev.exs create mode 100644 config/prod.exs create mode 100644 config/runtime.exs create mode 100644 config/test.exs diff --git a/.gitignore b/.gitignore index 3a92458..5dd2e67 100644 --- a/.gitignore +++ b/.gitignore @@ -24,3 +24,5 @@ tbot_800-*.tar # Temporary files, for example, from tests. /tmp/ + +dev.secret.exs diff --git a/apps/tbot800/lib/tbot800/application.ex b/apps/tbot800/lib/tbot800/application.ex new file mode 100644 index 0000000..c6e6418 --- /dev/null +++ b/apps/tbot800/lib/tbot800/application.ex @@ -0,0 +1,12 @@ +defmodule Tbot800.Application do + use Application + + def start(_type, _args) do + children = [ + Tbot800.DefaultImpl + ] + + opts = [strategy: :one_for_one, name: Tbot800.Supervisor] + Supervisor.start_link(children, opts) + end +end diff --git a/apps/tbot800/lib/tbot800/default_impl.ex b/apps/tbot800/lib/tbot800/default_impl.ex index f988f33..8bef126 100644 --- a/apps/tbot800/lib/tbot800/default_impl.ex +++ b/apps/tbot800/lib/tbot800/default_impl.ex @@ -4,6 +4,7 @@ defmodule Tbot800.DefaultImpl do @behaviour Tbot800.Impl + defdelegate child_spec(opts), to: __MODULE__.Supervisor @spec random_tweet(String.t(), String.t(), String.t(), String.t(), [String.t()]) :: :ok def random_tweet(consumer_key, consumer_secret, access_token, access_token_secret, contents) do diff --git a/apps/tbot800/lib/tbot800/default_impl/supervisor.ex b/apps/tbot800/lib/tbot800/default_impl/supervisor.ex new file mode 100644 index 0000000..03e2b4f --- /dev/null +++ b/apps/tbot800/lib/tbot800/default_impl/supervisor.ex @@ -0,0 +1,26 @@ +defmodule Tbot800.DefaultImpl.Supervisor do + use Supervisor + require Logger + + def start_link(init_args) do + Supervisor.start_link(__MODULE__, init_args, name: __MODULE__) + end + + def init(_init_args) do + tbot_accounts = + case Application.fetch_env(:tbot800, :tbot_accounts) do + {:ok, value} -> value + :error -> raise "twitter account settings do not exist. see config/runtime.exs" + end + + Logger.info("########################################") + + tbot_accounts + |> Enum.each(fn account -> Logger.info("tweet items path: #{account[:tweet_items_path]}") end) + + Logger.info("########################################") + + children = [] + Supervisor.init(children, strategy: :one_for_one) + end +end diff --git a/apps/tbot800/mix.exs b/apps/tbot800/mix.exs index bbe4ef0..f516089 100644 --- a/apps/tbot800/mix.exs +++ b/apps/tbot800/mix.exs @@ -18,7 +18,8 @@ defmodule Tbot800.MixProject do # Run "mix help compile.app" to learn about applications. def application do [ - extra_applications: [:logger] + extra_applications: [:logger], + mod: {Tbot800.Application, []} ] end diff --git a/config/config.exs b/config/config.exs index ab23e80..91bb650 100644 --- a/config/config.exs +++ b/config/config.exs @@ -16,3 +16,5 @@ import Config # format: "$date $time [$level] $metadata$message\n", # metadata: [:user_id] # + +import_config "#{Mix.env()}.exs" diff --git a/config/dev.exs b/config/dev.exs new file mode 100644 index 0000000..9f7a0fe --- /dev/null +++ b/config/dev.exs @@ -0,0 +1,5 @@ +import Config + +if File.exists?("config/dev.secret.exs") do + import_config "dev.secret.exs" +end diff --git a/config/prod.exs b/config/prod.exs new file mode 100644 index 0000000..becde76 --- /dev/null +++ b/config/prod.exs @@ -0,0 +1 @@ +import Config diff --git a/config/runtime.exs b/config/runtime.exs new file mode 100644 index 0000000..df01ff1 --- /dev/null +++ b/config/runtime.exs @@ -0,0 +1,47 @@ +import Config + +# 환경 변수로 설정한 계정 정보를 elixir config로 변환한다. +# 설정해야 할 환경 변수가 비어있으면 그 후 읽기를 중단한다. +# 예를 들어 ACCOUNT1_ 설정이 다 되어 있어야지 ACCOUNT2_ 설정 읽기를 시도한다. + +accounts = + Stream.iterate(1, &(&1 + 1)) + |> Stream.map(fn index -> + [ + consumer_key: System.get_env("ACCOUNT#{index}_KEY"), + consumer_secret: System.get_env("ACCOUNT#{index}_SECRET"), + access_token: System.get_env("ACCOUNT#{index}_TOKEN"), + access_token_secret: System.get_env("ACCOUNT#{index}_TOKEN_SECRET"), + interval: + String.to_integer(System.get_env("ACCOUNT#{index}_INTERVAL_MINUTE", "60")) * 1000 * 60, + tweet_items_path: System.get_env("ACCOUNT#{index}_TWEET_ITEMS_PATH") + ] + end) + |> Enum.take_while(fn account -> + Enum.all?(Keyword.values(account), &(!is_nil(&1))) + end) + +if accounts != [] do + config :tbot800, tbot_accounts: accounts +end + +# 개발 편의가 필요할 때는 dev.secret.exs 같은 파일에 아래와 같은 설정을 넣어서 실행한다 +# config :tbot800, +# tbot_accounts: [ +# [ +# consumer_key: "consumer_key", +# consumer_secret: "consumer_secret", +# access_token: "access_token", +# access_token_secret: "access_token_secret", +# interval: 1000 * 60 * 60, +# tweet_items_path: "pqoutes.exs" +# ], +# [ +# consumer_key: "consumer_key", +# consumer_secret: "consumer_secret", +# access_token: "access_token", +# access_token_secret: "access_token_secret", +# interval: 1000 * 60 * 60, +# tweet_items_path: "book_quotes.exs" +# ] +# ] diff --git a/config/test.exs b/config/test.exs new file mode 100644 index 0000000..becde76 --- /dev/null +++ b/config/test.exs @@ -0,0 +1 @@ +import Config