Skip to content

Commit

Permalink
feat: reimplement functionality originally provided by trot
Browse files Browse the repository at this point in the history
  • Loading branch information
tynn committed Apr 26, 2023
1 parent 869bc91 commit 3e8f1ca
Show file tree
Hide file tree
Showing 35 changed files with 463 additions and 372 deletions.
6 changes: 5 additions & 1 deletion .formatter.exs
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
[inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]]
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"],
locals_without_parens: [aar: 1],
import_deps: [:plug]
]
11 changes: 3 additions & 8 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,16 @@ on:
- cron: '0 11 * * 1'

jobs:
build:
test:
runs-on: ubuntu-latest
name: Elixir ${{ matrix.elixir }} OTP ${{ matrix.otp }}
strategy:
matrix:
elixir:
- 1.14.4
- 1.13.4
- 1.12.3
- 1.11.4
otp:
- 25.3
- 24.3
include:
- elixir: 1.14.4
otp: 25.3
steps:
- uses: actions/checkout@v3
- uses: erlef/setup-beam@v1
Expand All @@ -36,7 +31,7 @@ jobs:
- run: mix coveralls

deploy:
needs: build
needs: test
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
uses: ./.github/workflows/deploy.yml
secrets: inherit
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,27 @@

Get Sdk Levels of Android libraries as a shield to use as a badge.


## Gigalixir App

https://asapi.gigalixirapp.com

* https://github.com/HashNuke/heroku-buildpack-elixir
## License

Copyright (C) 2017-2023 tynn

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.



[build]: https://github.com/asapi/Lv/actions/workflows/build.yml
Expand Down
16 changes: 0 additions & 16 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,4 @@ config :logger, level: :warn

config :tesla, adapter: Tesla.Adapter.Hackney

config :trot,
router: Asapi.Router,
pre_routing: [
"Elixir.Trot.LiveReload": [env: Mix.env()],
"Elixir.Plug.Logger": [],
"Elixir.PlugHeartbeat": [],
"Elixir.Plug.Static": [at: "/", from: :asapi],
"Elixir.Asapi.Library": [],
"Elixir.Asapi.Type": []
],
post_routing: [
"Elixir.Asapi.Reload": [],
"Elixir.Asapi.Lv": [],
"Elixir.Trot.NotFound": []
]

import_config "#{Mix.env()}.exs"
18 changes: 8 additions & 10 deletions config/prod.exs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import Config
import System

config :trot,
port: get_env("PORT"),
pre_routing: [
"Elixir.Plug.SSL": [rewrite_on: [:x_forwarded_proto]],
"Elixir.PlugHeartbeat": [],
"Elixir.Plug.Static": [at: "/", from: :asapi],
"Elixir.Asapi.Library": [],
"Elixir.Asapi.Type": []
]
try do
System.get_env("PORT")
|> String.to_integer()
rescue
_ -> nil
else
port -> config :asapi, port: port
end
4 changes: 2 additions & 2 deletions elixir_buildpack.config
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
erlang_version=24.3
elixir_version=1.13.3
erlang_version=25.3
elixir_version=1.14.4
40 changes: 13 additions & 27 deletions lib/asapi.ex
Original file line number Diff line number Diff line change
@@ -1,35 +1,21 @@
# Copyright 2017 Christian Schmitz
# Copyright 2023 Christian Schmitz
# SPDX-License-Identifier: AGPL-3.0-or-later

defmodule Asapi do
@loading "…"
@unknown "unknown"
use Application

def shield(status, json \\ false) when status in [@loading, @unknown] do
shield(status, "inactive", json)
end

def shield(status, json) do
shield(status, "informational", json)
end
@impl true
def start(_type, _args) do
cowboy_options = [
compress: true,
port: Application.get_env(:asapi, :port, 4000)
]

defp shield(status, color, json) do
unless json do
"https://img.shields.io/badge/API-#{encode(status)}-#{color}"
else
"{\"schemaVersion\":1,\"label\":\"API\",\"message\":\"#{status}\",\"color\":\"#{color}\"}"
end
end

defp encode(part) do
String.replace(part, "-", "--")
end
children = [
Asapi.Ext,
{Plug.Cowboy, scheme: :http, plug: Asapi.Router, options: cowboy_options}
]

defmacro __using__(_) do
quote do
import Asapi
@loading unquote(@loading)
@unknown unquote(@unknown)
end
Supervisor.start_link(children, strategy: :one_for_one)
end
end
19 changes: 13 additions & 6 deletions lib/asapi/ext.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@
# SPDX-License-Identifier: AGPL-3.0-or-later

defmodule Asapi.Ext do
use Supervisor

@day :timer.hours(24)

def start(_type, _args) do
Cachex.start_link(:lvc,
disable_ode: false,
default_ttl: @day * 7,
ttl_interval: @day
)
def start_link(init_arg) do
Supervisor.start_link(__MODULE__, init_arg, name: __MODULE__)
end

@impl true
def init(_init_arg) do
children = [
{Cachex, name: :lvc, disable_ode: false, default_ttl: @day * 7, ttl_interval: @day}
]

Supervisor.init(children, strategy: :one_for_one)
end
end
13 changes: 4 additions & 9 deletions lib/asapi/ext/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
# SPDX-License-Identifier: AGPL-3.0-or-later

defmodule Asapi.Ext.Repo do
use Tesla

alias Asapi.Ext.Version
alias Asapi.Aar
use Tesla

plug(Tesla.Middleware.FollowRedirects)
plug(Tesla.Middleware.Retry, max_retries: 3)
plug Tesla.Middleware.FollowRedirects
plug Tesla.Middleware.Retry, max_retries: 3

@repos [
"https://maven.google.com",
Expand All @@ -20,9 +21,6 @@ defmodule Asapi.Ext.Repo do
case aar.revision do
nil -> ""
"" -> ""
"latest.integration" -> ""
"latest.milestone" -> ""
"latest.release" -> ""
rev -> String.slice(rev, 0..-2)
end

Expand Down Expand Up @@ -89,9 +87,6 @@ defmodule Asapi.Ext.Repo do
|> case do
nil -> true
"" -> true
"latest.integration" -> true
"latest.milestone" -> true
"latest.release" -> true
rev -> String.ends_with?(rev, "+")
end
end
Expand Down
11 changes: 6 additions & 5 deletions lib/asapi/library.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@

defmodule Asapi.Library do
alias Plug.Conn
import Plug.Conn, only: [fetch_query_params: 1]
import Trot.Router, only: [do_redirect: 2]
import Asapi.Util, only: [build_url: 3]

@behaviour Plug

import Asapi.Response
import Plug.Conn, only: [fetch_query_params: 1]

@impl true
def init(opts), do: opts

@impl true
def call(%Conn{} = conn, _opts) do
conn
|> fetch_query_params
Expand All @@ -19,8 +21,7 @@ defmodule Asapi.Library do

defp handle(%Conn{query_params: %{"library" => lib}} = conn) do
conn
|> build_url("/#{String.replace(lib, ":", "/")}", ["library"])
|> do_redirect(conn)
|> redirect_to("/#{String.replace(lib, ":", "/")}", ["library"])
end

defp handle(%Conn{} = conn), do: conn
Expand Down
59 changes: 15 additions & 44 deletions lib/asapi/lv.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,75 +2,46 @@
# SPDX-License-Identifier: AGPL-3.0-or-later

defmodule Asapi.Lv do
use Asapi.Shield

alias Plug.Conn
alias Asapi.Aar
alias Asapi.Ext.Data
require Logger

import Trot.Router, only: [make_response: 2]
import Asapi.Util

use Asapi
use Trot.Template
@behaviour Plug

@template_root "priv/temp"
require Logger

@behaviour Plug
import Asapi.Response

@impl true
def init(opts), do: opts

@impl true
def call(%Conn{state: :unset} = conn, _opts) do
conn
|> asapi_lv
|> make_response(conn)
asapi_lv(conn)
end

def call(%Conn{} = conn, _opts), do: conn

defp asapi_lv(%Conn{assigns: %{asapi_aar: %Aar{} = aar, asapi_ext: :html}} = conn) do
host =
case conn.port do
80 -> conn.host
443 -> conn.host
port -> "#{conn.host}:#{port}"
end

path =
case Enum.join(conn.path_info, "/") do
"" -> "/"
path -> "/#{path}"
end

render_template("asapi.html.eex",
host: host,
path: path,
lib: to_string(aar),
api: api_lv(aar),
loading: "#{shield(@loading)}.svg"
)
send_html(conn, to_string(aar), api_lv(aar), "#{shield(@loading)}.svg")
end

defp asapi_lv(%Conn{assigns: %{asapi_aar: aar, asapi_ext: type}} = conn)
when type in [:png, :svg] do
conn
|> build_url("#{shield(api_lv(aar))}.#{type}")
|> redirect_to
redirect_to(conn, "#{shield(api_lv(aar))}.#{type}")
end

defp asapi_lv(%Conn{assigns: %{asapi_aar: aar, asapi_ext: :json}}) do
aar
|> api_lv
|> shield(json: true)
|> send_json
defp asapi_lv(%Conn{assigns: %{asapi_aar: aar, asapi_ext: :json}} = conn) do
send_json(conn, shield(api_lv(aar), json: true))
end

defp asapi_lv(%Conn{assigns: %{asapi_aar: aar, asapi_ext: :txt}}) do
aar
|> api_lv
|> send_text
defp asapi_lv(%Conn{assigns: %{asapi_aar: aar, asapi_ext: :txt}} = conn) do
send_text(conn, api_lv(aar))
end

defp asapi_lv(%Conn{} = conn), do: conn
defp asapi_lv(conn), do: conn

defp api_lv(%Aar{group: nil}), do: @unknown
defp api_lv(%Aar{name: nil}), do: @unknown
Expand Down
16 changes: 8 additions & 8 deletions lib/asapi/reload.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@
# SPDX-License-Identifier: AGPL-3.0-or-later

defmodule Asapi.Reload do
require Logger
alias Plug.Conn
alias Asapi.Aar
alias Asapi.Ext.Data

import Trot.Router, only: [do_redirect: 2]
import Asapi.Util, only: [build_url: 3]

@behaviour Plug

require Logger

import Asapi.Response

@impl true
def init(opts), do: opts

@impl true
def call(%Conn{query_params: %{"reload" => _}} = conn, _opts) do
conn
|> clear_cached
|> build_url(conn.request_path, ["reload"])
|> do_redirect(conn)
|> redirect_to(conn.request_path, ["reload"])
end

def call(%Conn{} = conn, _opts), do: conn
Expand All @@ -30,8 +31,7 @@ defmodule Asapi.Reload do
try do
Data.clear!(aar)
rescue
error ->
Logger.warn(Exception.message(error))
error -> Logger.warn(Exception.message(error))
end

conn
Expand Down
Loading

0 comments on commit 3e8f1ca

Please sign in to comment.