Skip to content

Commit

Permalink
⭐ twitter account에 관한 설정을 읽고 실행
Browse files Browse the repository at this point in the history
- twitter account 정보로 TwitterProcess 프로세스를 만드는 사전 작업
- 필요한 정보를 읽을 뿐 프로세스를 생성하지는 않는다
- runtime.exs
  - 환경 변수에서 설정을 읽어서 :tbot800, :tbot_accounts 설정
- dev.secret.exs
  - 개발 중에는 바로 :tbot800, :tbot_accounts 설정
  - 버전 컨트롤에서 파일 제외
  • Loading branch information
ohyecloudy committed Apr 26, 2023
1 parent f28b925 commit 0aaac28
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -24,3 +24,5 @@ tbot_800-*.tar

# Temporary files, for example, from tests.
/tmp/

dev.secret.exs
12 changes: 12 additions & 0 deletions 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
1 change: 1 addition & 0 deletions apps/tbot800/lib/tbot800/default_impl.ex
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions 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
3 changes: 2 additions & 1 deletion apps/tbot800/mix.exs
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions config/config.exs
Expand Up @@ -16,3 +16,5 @@ import Config
# format: "$date $time [$level] $metadata$message\n",
# metadata: [:user_id]
#

import_config "#{Mix.env()}.exs"
5 changes: 5 additions & 0 deletions config/dev.exs
@@ -0,0 +1,5 @@
import Config

if File.exists?("config/dev.secret.exs") do
import_config "dev.secret.exs"
end
1 change: 1 addition & 0 deletions config/prod.exs
@@ -0,0 +1 @@
import Config
47 changes: 47 additions & 0 deletions 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"
# ]
# ]
1 change: 1 addition & 0 deletions config/test.exs
@@ -0,0 +1 @@
import Config

0 comments on commit 0aaac28

Please sign in to comment.