Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Refactor caddy. #1

Merged
merged 10 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions .github/workflows/elixir_ci.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
name: Elixir CI

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

Expand All @@ -24,18 +22,20 @@ jobs:
elixir-version: 1.18
otp-version: 27

- name: Restore dependencies cache
uses: actions/cache@v3
with:
path: deps
key: ${{ runner.os }}-mix-${{ hashFiles('**/mix.lock') }}
restore-keys: ${{ runner.os }}-mix-

- name: Install dependencies
run: mix deps.get --only test
run: mix deps.get
env:
MIX_ENV: test

- name: Install caddy
run: |
curl -sSLf https://github.com/gsmlg-dev/Foundation/releases/download/caddy-v2.8.4/caddy_linux_amd64 -o /tmp/caddy
install -m 755 -D /tmp/caddy ${{ github.workspace }}/priv/bin/caddy

- name: Run tests
run: mix test
run: |
export PATH=${{ github.workspace }}/priv/bin:$PATH
mix test
env:
MIX_ENV: test

8 changes: 4 additions & 4 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,22 @@ jobs:
run: |
mix deps.get
sed -i 's%@version "[0-9\.]\+"%@version "${{ github.event.inputs.release-version }}"%' mix.exs
sed -i 's%{:phoenix_react_server, "~> [0-9\.]\+"}%{:phoenix_react_server, "~> ${{ github.event.inputs.release-version }}"}%' README.md
sed -i 's%{:caddy, "~> [0-9\.]\+"}%{:caddy, "~> ${{ github.event.inputs.release-version }}"}%' README.md

- name: Publish to hex.pm
env:
MIX_ENV: prod
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
HEX_API_KEY: ${{ secrets.HEX_API_KEY }}
run: |
mix hex.publish --yes
mix publish

- uses: actions/github-script@v7
with:
script: |
github.rest.git.createRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: 'refs/tags/${{ github.event.inputs.release-version }}',
ref: 'refs/tags/v${{ github.event.inputs.release-version }}',
sha: context.sha
})
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Caddy

By set `mix.exs` to install

```elixir
{:caddy, "~> 1.0"}
{:caddy, "~> 2.0"}
```

## Start Caddy Server by `Port`
Expand All @@ -21,9 +22,6 @@ def start(_type, _args) do
# Start a Caddy by calling: Caddy.start_link([])
{Caddy, [
caddy_bin: "<path to caddy binary>",
caddy_file: "<path to caddyfile config>", # caddyfile to load, if not set use config instead
config: %{}, # map() parsed json config,
merge_saved: false, # Merge saved config, defaults to false
]}
]

Expand All @@ -33,3 +31,13 @@ def start(_type, _args) do
Supervisor.start_link(children, opts)
end
```

Start in extra_applications

```elixir
def application do
[
extra_applications: [Caddy.Application]
]
end
```
65 changes: 60 additions & 5 deletions lib/caddy.ex
Original file line number Diff line number Diff line change
@@ -1,12 +1,69 @@
defmodule Caddy do
@moduledoc """

# Caddy

Start Caddy HTTP Server in supervisor tree

- Start in your application

```elixir
def start(_type, _args) do
children = [
# Start a Caddy by calling: Caddy.start_link([])
{Caddy, [
caddy_bin: "/usr/bin/caddy",
]}
# Start the Telemetry supervisor
PhoenixWeb.Telemetry,
# Start the PubSub system
{Phoenix.PubSub, name: PhoenixWeb.PubSub},
# Start the Endpoint (http/https)
PhoenixWeb.Endpoint
]

opts = [strategy: :one_for_one, name: PhoenixWeb.Supervisor]
Supervisor.start_link(children, opts)
end
```

- Start in extra_applications

```elixir
def application do
[
extra_applications: [Caddy.Application]
]
end
```

* Notice

If caddy_bin is not specifiy, Caddy.Server will not start.
You can set it latter by `Caddy.Config.set_bin("path/to/caddy")` and restart server by `Caddy.restart_server()`

"""
require Logger

use Supervisor

@doc """
Restart Caddy Server
"""
def restart_server() do
case Supervisor.restart_child(__MODULE__, Caddy.Server) do
{:error, :running} ->
Supervisor.terminate_child(__MODULE__, Caddy.Server)
Supervisor.restart_child(__MODULE__, Caddy.Server)

out ->
out
end
end

@spec start(caddy_bin: binary) :: :ignore | {:error, any()} | {:ok, pid()}
def start(caddy_bin: caddy_bin), do: start_link(caddy_bin: caddy_bin)

@spec start() :: :ignore | {:error, any()} | {:ok, pid()}
def start() do
caddy_bin = System.find_executable("caddy")
Expand All @@ -27,11 +84,9 @@ defmodule Caddy do
def init(args) do
children = [
{Caddy.Config, args},
Caddy.Bootstrap,
Caddy.Logger.Buffer,
Caddy.Logger.Store,
Caddy.Server,
Caddy.Admin
Caddy.Logger,
Caddy.Server
# Caddy.Admin
]

opts = [strategy: :rest_for_one, name: __MODULE__]
Expand Down
5 changes: 3 additions & 2 deletions lib/caddy/admin.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ defmodule Caddy.Admin do
defp check_caddy_server() do
%{"listen" => "unix/" <> _} = Caddy.Admin.Api.get_config("admin")
rescue
_ ->
Caddy.Bootstrap.restart()
error ->
Logger.error("Caddy Admin: check_caddy_server failed #{inspect(error)}")
Caddy.Server.stop()
end
end
8 changes: 4 additions & 4 deletions lib/caddy/admin/request.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ defmodule Caddy.Admin.Request do
require Logger

@type t :: %__MODULE__{
status: integer(),
headers: Keyword.t(),
body: binary(),
}
status: integer(),
headers: Keyword.t(),
body: binary()
}

defstruct status: 0, headers: [], body: ""

Expand Down
3 changes: 1 addition & 2 deletions lib/caddy/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ defmodule Caddy.Application do
@impl true
def start(_type, args) do
children = [
{Caddy, args},
{Caddy, args}
]

opts = [strategy: :one_for_one, name: Caddy.Application]
Supervisor.start_link(children, opts)
end

end
118 changes: 0 additions & 118 deletions lib/caddy/bootstrap.ex

This file was deleted.

Loading