Skip to content
This repository was archived by the owner on May 9, 2021. It is now read-only.

Commit a438a72

Browse files
committed
Initial commit
0 parents  commit a438a72

25 files changed

+687
-0
lines changed

.formatter.exs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[
2+
import_deps: [:phoenix],
3+
inputs: ["*.{ex,exs}", "{config,lib,test}/**/*.{ex,exs}"]
4+
]

.gitignore

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# The directory Mix will write compiled artifacts to.
2+
/_build/
3+
4+
# If you run "mix test --cover", coverage assets end up here.
5+
/cover/
6+
7+
# The directory Mix downloads your dependencies sources to.
8+
/deps/
9+
10+
# Where 3rd-party dependencies like ExDoc output generated docs.
11+
/doc/
12+
13+
# Ignore .fetch files in case you like to edit your project deps locally.
14+
/.fetch
15+
16+
# If the VM crashes, it generates a dump, let's ignore it too.
17+
erl_crash.dump
18+
19+
# Also ignore archive artifacts (built via "mix archive.build").
20+
*.ez
21+
22+
# Ignore package tarball (built via "mix hex.build").
23+
agma-*.tar
24+
25+
# Since we are building assets from assets/,
26+
# we ignore priv/static. You may want to comment
27+
# this depending on your deployment strategy.
28+
/priv/static/

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# 악마
2+
3+
A host stats daemon.

config/config.exs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# This file is responsible for configuring your application
2+
# and its dependencies with the aid of the Mix.Config module.
3+
#
4+
# This configuration file is loaded before any dependency and
5+
# is restricted to this project.
6+
7+
# General application configuration
8+
use Mix.Config
9+
10+
# Configures the endpoint
11+
config :agma, AgmaWeb.Endpoint,
12+
url: [host: "localhost"],
13+
secret_key_base: "o46Y9nQPeuH+6pTrVWxbIcVIPHp7DlbVJziMjOg662aw7xEMsDuqh04UtmHjaVyd",
14+
render_errors: [view: AgmaWeb.ErrorView, accepts: ~w(json), layout: false],
15+
pubsub_server: Agma.PubSub,
16+
live_view: [signing_salt: "TLbs89UN"]
17+
18+
# Configures Elixir's Logger
19+
config :logger, :console,
20+
format: "$time $metadata[$level] $message\n",
21+
metadata: [:request_id]
22+
23+
# Use Jason for JSON parsing in Phoenix
24+
config :phoenix, :json_library, Jason
25+
26+
# Import environment specific config. This must remain at the bottom
27+
# of this file so it overrides the configuration defined above.
28+
import_config "#{Mix.env()}.exs"

config/dev.exs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use Mix.Config
2+
3+
config :agma, AgmaWeb.Endpoint,
4+
http: [port: 4040],
5+
debug_errors: true,
6+
code_reloader: true,
7+
check_origin: false,
8+
watchers: []
9+
10+
config :logger, :console,
11+
format: "$time $metadata[$level] $message\n",
12+
metadata: [:request_id]
13+
14+
config :phoenix, :stacktrace_depth, 20
15+
16+
config :phoenix, :plug_init_mode, :runtime

config/prod.exs

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use Mix.Config
2+
3+
# For production, don't forget to configure the url host
4+
# to something meaningful, Phoenix uses this information
5+
# when generating URLs.
6+
#
7+
# Note we also include the path to a cache manifest
8+
# containing the digested version of static files. This
9+
# manifest is generated by the `mix phx.digest` task,
10+
# which you should run after static files are built and
11+
# before starting your production server.
12+
config :agma, AgmaWeb.Endpoint,
13+
url: [host: "example.com", port: 80],
14+
cache_static_manifest: "priv/static/cache_manifest.json"
15+
16+
# Do not print debug messages in production
17+
config :logger, level: :info
18+
19+
# ## SSL Support
20+
#
21+
# To get SSL working, you will need to add the `https` key
22+
# to the previous section and set your `:url` port to 443:
23+
#
24+
# config :agma, AgmaWeb.Endpoint,
25+
# ...
26+
# url: [host: "example.com", port: 443],
27+
# https: [
28+
# port: 443,
29+
# cipher_suite: :strong,
30+
# keyfile: System.get_env("SOME_APP_SSL_KEY_PATH"),
31+
# certfile: System.get_env("SOME_APP_SSL_CERT_PATH"),
32+
# transport_options: [socket_opts: [:inet6]]
33+
# ]
34+
#
35+
# The `cipher_suite` is set to `:strong` to support only the
36+
# latest and more secure SSL ciphers. This means old browsers
37+
# and clients may not be supported. You can set it to
38+
# `:compatible` for wider support.
39+
#
40+
# `:keyfile` and `:certfile` expect an absolute path to the key
41+
# and cert in disk or a relative path inside priv, for example
42+
# "priv/ssl/server.key". For all supported SSL configuration
43+
# options, see https://hexdocs.pm/plug/Plug.SSL.html#configure/1
44+
#
45+
# We also recommend setting `force_ssl` in your endpoint, ensuring
46+
# no data is ever sent via http, always redirecting to https:
47+
#
48+
# config :agma, AgmaWeb.Endpoint,
49+
# force_ssl: [hsts: true]
50+
#
51+
# Check `Plug.SSL` for all available options in `force_ssl`.
52+
53+
# Finally import the config/prod.secret.exs which loads secrets
54+
# and configuration from environment variables.
55+
import_config "prod.secret.exs"

config/prod.secret.exs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# In this file, we load production configuration and secrets
2+
# from environment variables. You can also hardcode secrets,
3+
# although such is generally not recommended and you have to
4+
# remember to add this file to your .gitignore.
5+
use Mix.Config
6+
7+
secret_key_base =
8+
System.get_env("SECRET_KEY_BASE") ||
9+
raise """
10+
environment variable SECRET_KEY_BASE is missing.
11+
You can generate one by calling: mix phx.gen.secret
12+
"""
13+
14+
config :agma, AgmaWeb.Endpoint,
15+
http: [
16+
port: String.to_integer(System.get_env("PORT") || "4000"),
17+
transport_options: [socket_opts: [:inet6]]
18+
],
19+
secret_key_base: secret_key_base
20+
21+
# ## Using releases (Elixir v1.9+)
22+
#
23+
# If you are doing OTP releases, you need to instruct Phoenix
24+
# to start each relevant endpoint:
25+
#
26+
# config :agma, AgmaWeb.Endpoint, server: true
27+
#
28+
# Then you can assemble a release by calling `mix release`.
29+
# See `mix help release` for more information.

config/test.exs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use Mix.Config
2+
3+
# We don't run a server during test. If one is required,
4+
# you can enable the server option below.
5+
config :agma, AgmaWeb.Endpoint,
6+
http: [port: 4002],
7+
server: false
8+
9+
# Print only warnings and errors during test
10+
config :logger, level: :warn

lib/agma.ex

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
defmodule Agma do
2+
@moduledoc """
3+
Agma keeps the contexts that define your domain
4+
and business logic.
5+
6+
Contexts are also responsible for managing your data, regardless
7+
if it comes from the database, an external API or others.
8+
"""
9+
end

lib/agma/application.ex

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
defmodule Agma.Application do
2+
@moduledoc false
3+
4+
use Application
5+
6+
def start(_type, _args) do
7+
children = [
8+
AgmaWeb.Telemetry,
9+
{Phoenix.PubSub, name: Agma.PubSub},
10+
AgmaWeb.Endpoint,
11+
{Singyeong.Client, Singyeong.parse_dsn("singyeong://agma:password@localhost:4567")},
12+
Singyeong.Producer,
13+
Agma.Consumer,
14+
Agma.Stats,
15+
]
16+
17+
opts = [strategy: :one_for_one, name: Agma.Supervisor]
18+
Supervisor.start_link(children, opts)
19+
end
20+
21+
def config_change(changed, _new, removed) do
22+
AgmaWeb.Endpoint.config_change(changed, removed)
23+
:ok
24+
end
25+
end

lib/agma/consumer.ex

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
defmodule Agma.Consumer do
2+
use Singyeong.Consumer
3+
4+
def start_link do
5+
Consumer.start_link __MODULE__
6+
end
7+
8+
def handle_event(event) do
9+
IO.inspect event, pretty: true
10+
:ok
11+
end
12+
end

lib/agma/stats.ex

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
defmodule Agma.Stats do
2+
use GenServer
3+
4+
def start_link(opts) do
5+
GenServer.start_link __MODULE__, opts, name: __MODULE__
6+
end
7+
8+
def init(opts) do
9+
tick()
10+
{:ok, opts}
11+
end
12+
13+
def handle_info(:tick, state) do
14+
cpus = :erlang.system_info :logical_processors
15+
cpu_util = :cpu_sup.util()
16+
17+
%{
18+
total_memory: mem_total,
19+
free_memory: mem_free,
20+
} = Map.new :memsup.get_system_memory_data()
21+
22+
Singyeong.Client.update_metadata %{
23+
cpu_count: %{
24+
type: "integer",
25+
value: cpus,
26+
},
27+
cpu_util: %{
28+
type: "float",
29+
value: cpu_util,
30+
},
31+
mem_total: %{
32+
type: "integer",
33+
value: mem_total,
34+
},
35+
mem_free: %{
36+
type: "integer",
37+
value: mem_free,
38+
},
39+
}
40+
tick()
41+
{:noreply, state}
42+
end
43+
44+
defp tick() do
45+
Process.send_after self(), :tick, 100
46+
end
47+
end

lib/agma_web.ex

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
defmodule AgmaWeb do
2+
@moduledoc """
3+
The entrypoint for defining your web interface, such
4+
as controllers, views, channels and so on.
5+
6+
This can be used in your application as:
7+
8+
use AgmaWeb, :controller
9+
use AgmaWeb, :view
10+
11+
The definitions below will be executed for every view,
12+
controller, etc, so keep them short and clean, focused
13+
on imports, uses and aliases.
14+
15+
Do NOT define functions inside the quoted expressions
16+
below. Instead, define any helper function in modules
17+
and import those modules here.
18+
"""
19+
20+
def controller do
21+
quote do
22+
use Phoenix.Controller, namespace: AgmaWeb
23+
24+
import Plug.Conn
25+
alias AgmaWeb.Router.Helpers, as: Routes
26+
end
27+
end
28+
29+
def view do
30+
quote do
31+
use Phoenix.View,
32+
root: "lib/agma_web/templates",
33+
namespace: AgmaWeb
34+
35+
# Import convenience functions from controllers
36+
import Phoenix.Controller,
37+
only: [get_flash: 1, get_flash: 2, view_module: 1, view_template: 1]
38+
39+
# Include shared imports and aliases for views
40+
unquote(view_helpers())
41+
end
42+
end
43+
44+
def router do
45+
quote do
46+
use Phoenix.Router
47+
48+
import Plug.Conn
49+
import Phoenix.Controller
50+
end
51+
end
52+
53+
def channel do
54+
quote do
55+
use Phoenix.Channel
56+
end
57+
end
58+
59+
defp view_helpers do
60+
quote do
61+
# Import basic rendering functionality (render, render_layout, etc)
62+
import Phoenix.View
63+
64+
import AgmaWeb.ErrorHelpers
65+
alias AgmaWeb.Router.Helpers, as: Routes
66+
end
67+
end
68+
69+
@doc """
70+
When used, dispatch to the appropriate controller/view/etc.
71+
"""
72+
defmacro __using__(which) when is_atom(which) do
73+
apply(__MODULE__, which, [])
74+
end
75+
end

lib/agma_web/channels/user_socket.ex

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
defmodule AgmaWeb.UserSocket do
2+
use Phoenix.Socket
3+
4+
## Channels
5+
# channel "room:*", AgmaWeb.RoomChannel
6+
7+
# Socket params are passed from the client and can
8+
# be used to verify and authenticate a user. After
9+
# verification, you can put default assigns into
10+
# the socket that will be set for all channels, ie
11+
#
12+
# {:ok, assign(socket, :user_id, verified_user_id)}
13+
#
14+
# To deny connection, return `:error`.
15+
#
16+
# See `Phoenix.Token` documentation for examples in
17+
# performing token verification on connect.
18+
@impl true
19+
def connect(_params, socket, _connect_info) do
20+
{:ok, socket}
21+
end
22+
23+
# Socket id's are topics that allow you to identify all sockets for a given user:
24+
#
25+
# def id(socket), do: "user_socket:#{socket.assigns.user_id}"
26+
#
27+
# Would allow you to broadcast a "disconnect" event and terminate
28+
# all active sockets and channels for a given user:
29+
#
30+
# AgmaWeb.Endpoint.broadcast("user_socket:#{user.id}", "disconnect", %{})
31+
#
32+
# Returning `nil` makes this socket anonymous.
33+
@impl true
34+
def id(_socket), do: nil
35+
end

0 commit comments

Comments
 (0)