-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
403 additions
and
398 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,27 @@ | ||
defmodule SpaceAgeApi.Models.ServerConfig do | ||
@moduledoc false | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
alias SpaceAgeApi.Util | ||
|
||
@primary_key false | ||
schema "server_configs" do | ||
field :authkey, :string | ||
field :name, :string | ||
field :location, :string | ||
field :rcon_password, :string | ||
field :steam_account_token, :string | ||
|
||
timestamps() | ||
end | ||
|
||
@doc false | ||
def changeset(server, attrs) do | ||
server | ||
|> cast(attrs, [:name, :authkey, :location, :rcon_password, :steam_account_token]) | ||
|> validate_required([:name, :authkey, :location]) | ||
|> unique_constraint(:name) | ||
|> unique_constraint(:authkey) | ||
|> put_change(:updated_at, Util.naive_date_time()) | ||
end | ||
end | ||
|
||
defmodule SpaceAgeApi.Models.ServerConfig do | ||
@moduledoc false | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
alias SpaceAgeApi.Util | ||
|
||
@primary_key false | ||
schema "server_configs" do | ||
field :authkey, :string | ||
field :name, :string | ||
field :location, :string | ||
field :rcon_password, :string | ||
field :steam_account_token, :string | ||
|
||
timestamps() | ||
end | ||
|
||
@doc false | ||
def changeset(server, attrs) do | ||
server | ||
|> cast(attrs, [:name, :authkey, :location, :rcon_password, :steam_account_token]) | ||
|> validate_required([:name, :authkey, :location]) | ||
|> unique_constraint(:name) | ||
|> unique_constraint(:authkey) | ||
|> put_change(:updated_at, Util.naive_date_time()) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,22 @@ | ||
defmodule SpaceAgeApi.Plug.Cache do | ||
@moduledoc """ | ||
Adds Cache-Control headers when needed | ||
""" | ||
import Plug.Conn | ||
|
||
def init(options), do: options | ||
|
||
def call(conn, opts) do | ||
conn | ||
|> put_cache_control(opts[:time]) | ||
end | ||
|
||
defp put_cache_control(conn, nil) do | ||
conn | ||
|> put_cache_control(300) | ||
end | ||
defp put_cache_control(conn, time) do | ||
conn | ||
|> put_resp_header("cache-control", "public, max-age=#{time}") | ||
end | ||
end | ||
defmodule SpaceAgeApi.Plug.Cache do | ||
@moduledoc """ | ||
Adds Cache-Control headers when needed | ||
""" | ||
import Plug.Conn | ||
|
||
def init(options), do: options | ||
|
||
def call(conn, opts) do | ||
conn | ||
|> put_cache_control(opts[:time]) | ||
end | ||
|
||
defp put_cache_control(conn, nil) do | ||
conn | ||
|> put_cache_control(300) | ||
end | ||
defp put_cache_control(conn, time) do | ||
conn | ||
|> put_resp_header("cache-control", "public, max-age=#{time}") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,50 @@ | ||
defmodule SpaceAgeApi.Plug.RawBodyReader do | ||
@moduledoc """ | ||
A body reader that caches raw request body for later use. | ||
This module is intended to be used as the `:body_reader` option of `Plug.Parsers`. | ||
Note that caching is only enabled for specific paths. See `enabled_for?/1`. | ||
""" | ||
|
||
@raw_body_key :raw_body | ||
|
||
def read_body(%Plug.Conn{} = conn, opts \\ []) do | ||
case Plug.Conn.read_body(conn, opts) do | ||
{:ok, binary, conn} -> | ||
{:ok, binary, maybe_store_body_chunk(conn, binary)} | ||
|
||
{:more, binary, conn} -> | ||
{:more, binary, maybe_store_body_chunk(conn, binary)} | ||
|
||
{:error, reason} -> | ||
{:error, reason} | ||
end | ||
end | ||
|
||
defp enabled_for?(conn) do | ||
case conn.path_info do | ||
["v2" | ["discord" | _rest]] -> true | ||
_ -> false | ||
end | ||
end | ||
|
||
defp maybe_store_body_chunk(conn, chunk) do | ||
if enabled_for?(conn) do | ||
store_body_chunk(conn, chunk) | ||
else | ||
conn | ||
end | ||
end | ||
|
||
def store_body_chunk(%Plug.Conn{} = conn, chunk) when is_binary(chunk) do | ||
chunks = conn.private[@raw_body_key] || [] | ||
Plug.Conn.put_private(conn, @raw_body_key, [chunk | chunks]) | ||
end | ||
|
||
def get_raw_body(%Plug.Conn{} = conn) do | ||
case conn.private[@raw_body_key] do | ||
nil -> nil | ||
chunks -> chunks |> Enum.reverse() |> Enum.join("") | ||
end | ||
end | ||
end | ||
defmodule SpaceAgeApi.Plug.RawBodyReader do | ||
@moduledoc """ | ||
A body reader that caches raw request body for later use. | ||
This module is intended to be used as the `:body_reader` option of `Plug.Parsers`. | ||
Note that caching is only enabled for specific paths. See `enabled_for?/1`. | ||
""" | ||
|
||
@raw_body_key :raw_body | ||
|
||
def read_body(%Plug.Conn{} = conn, opts \\ []) do | ||
case Plug.Conn.read_body(conn, opts) do | ||
{:ok, binary, conn} -> | ||
{:ok, binary, maybe_store_body_chunk(conn, binary)} | ||
|
||
{:more, binary, conn} -> | ||
{:more, binary, maybe_store_body_chunk(conn, binary)} | ||
|
||
{:error, reason} -> | ||
{:error, reason} | ||
end | ||
end | ||
|
||
defp enabled_for?(conn) do | ||
case conn.path_info do | ||
["v2" | ["discord" | _rest]] -> true | ||
_ -> false | ||
end | ||
end | ||
|
||
defp maybe_store_body_chunk(conn, chunk) do | ||
if enabled_for?(conn) do | ||
store_body_chunk(conn, chunk) | ||
else | ||
conn | ||
end | ||
end | ||
|
||
def store_body_chunk(%Plug.Conn{} = conn, chunk) when is_binary(chunk) do | ||
chunks = conn.private[@raw_body_key] || [] | ||
Plug.Conn.put_private(conn, @raw_body_key, [chunk | chunks]) | ||
end | ||
|
||
def get_raw_body(%Plug.Conn{} = conn) do | ||
case conn.private[@raw_body_key] do | ||
nil -> nil | ||
chunks -> chunks |> Enum.reverse() |> Enum.join("") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,17 @@ | ||
defmodule SpaceAgeApi.Token do | ||
use Joken.Config | ||
|
||
@default_exp 30 * 60 | ||
|
||
def default_exp, do: @default_exp | ||
|
||
def token_config do | ||
default_claims(default_exp: @default_exp, skip: [:aud]) | ||
|> add_claim("aud", nil, &(&1 in [ | ||
"https://api.spaceage.mp/v2/clientauth" | ||
])) | ||
end | ||
end | ||
defmodule SpaceAgeApi.Token do | ||
@moduledoc """ | ||
Token configuration for SpaceAge API. | ||
""" | ||
use Joken.Config | ||
|
||
@default_exp 30 * 60 | ||
|
||
def default_exp, do: @default_exp | ||
|
||
def token_config do | ||
default_claims(default_exp: @default_exp, skip: [:aud]) | ||
|> add_claim("aud", nil, &(&1 in [ | ||
"https://api.spaceage.mp/v2/clientauth" | ||
])) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.