diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml deleted file mode 100644 index 701b1f5..0000000 --- a/.github/workflows/test.yml +++ /dev/null @@ -1,23 +0,0 @@ -name: test - -on: - push: - branches: - - master - - main - pull_request: - -jobs: - test: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - uses: erlef/setup-beam@v1 - with: - otp-version: "26.0.2" - gleam-version: "0.31.0" - rebar3-version: "3" - # elixir-version: "1.15.4" - - run: gleam format --check src test - - run: gleam deps download - - run: gleam test diff --git a/src/cockpit_cms_proxy.gleam b/src/cockpit_cms_proxy.gleam index 64fdf55..ce07f68 100644 --- a/src/cockpit_cms_proxy.gleam +++ b/src/cockpit_cms_proxy.gleam @@ -1,20 +1,35 @@ import gleam/bit_builder import gleam/erlang/process +import gleam/erlang/os import gleam/http/request.{Request} import gleam/http/response.{Response} import mist.{Connection, ResponseData} import gleam/httpc -import gleam/http.{Get, Http} -import gleam/string.{join} +import gleam/http.{Method, Scheme} +import gleam/string +import gleam/io +import gleam/option.{Option, unwrap} +import gleam/result +import gleam/int -fn handle_website(segments: List(String)) -> Response(ResponseData) { +pub type Website { + Website( + target_host: String, + target_port: Int, + scheme: Scheme, + method: Method, + path: String, + ) +} + +fn handle_website(config: Website) -> Response(ResponseData) { let result = request.new() - |> request.set_scheme(Http) - |> request.set_method(Get) - |> request.set_host("localhost") - |> request.set_port(1234) - |> request.set_path(join(segments, "/")) + |> request.set_scheme(config.scheme) + |> request.set_method(config.method) + |> request.set_host(config.target_host) + |> request.set_port(config.target_port) + |> request.set_path(config.path) |> httpc.send case result { @@ -27,15 +42,97 @@ fn handle_website(segments: List(String)) -> Response(ResponseData) { } } +type Image { + Image( + base_url: String, + api_token: String, + path: List(String), + query: Option(String), + ) +} + +fn handle_images(config: Image) -> Response(ResponseData) { + let url = + "/api/cockpit/image?token=" <> config.api_token <> "&src=" <> config.base_url <> "/storage/uploads/" <> string.join( + config.path, + "/", + ) <> "&" <> unwrap(config.query, "") + + let result = + request.new() + |> request.set_host(string.replace(config.base_url, "https://", "")) + |> request.set_path(url) + |> request.set_body(<<>>) + |> httpc.send_bits + + case result { + Ok(r) -> + response.new(200) + |> response.set_body(mist.Bytes(bit_builder.from_bit_string(r.body))) + Error(_) -> + response.new(404) + |> response.set_body(mist.Bytes(bit_builder.from_string("