Skip to content

Commit

Permalink
Rename the project because it will have more responsibilities.
Browse files Browse the repository at this point in the history
  • Loading branch information
Burgy Benjamin committed Dec 16, 2023
1 parent 62a67cf commit f3bd838
Show file tree
Hide file tree
Showing 28 changed files with 118 additions and 108 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ erl_crash.dump
/tmp/

# Ignore package tarball (built via "mix hex.build").
kdrive_bridge-*.tar
ksuite_middleware-*.tar

6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ FROM ${BUILDER_IMAGE} as builder

# install build dependencies
RUN apt-get update -y && apt-get install -y build-essential git \
&& apt-get clean && rm -f /var/lib/apt/lists/*_*
&& apt-get clean && rm -f /var/lib/apt/lists/*_*

# prepare build dir
WORKDIR /app

# install hex + rebar
RUN mix local.hex --force && \
mix local.rebar --force
mix local.rebar --force

# set build ENV
ENV MIX_ENV="prod"
Expand Down Expand Up @@ -80,7 +80,7 @@ RUN chown nobody /app
ENV MIX_ENV="prod"

# Only copy the final release from the build stage
COPY --from=builder --chown=nobody:root /app/_build/${MIX_ENV}/rel/kdrive_bridge ./
COPY --from=builder --chown=nobody:root /app/_build/${MIX_ENV}/rel/ksuite_middleware ./

USER nobody

Expand Down
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# KdriveBridge
# Ksuite-middleware

The goal of this project is to provide access to the content of your files saved in kDrive without the KSuite layer by calling the URL:
Project providing a single endpoint with a single API configuration to access to many resources from a KSuite environment.

## Kdrive

```
http://localhost:4000/files/<your-kdrive-file-id>
Expand All @@ -11,29 +13,29 @@ http://localhost:4000/files/<your-kdrive-file-id>
| Environment variables | Description |
|-----------------------|------------------------------------------------------------------------------------------------------------------------------|
| KDRIVE_ID | The identifier of your KDrive. |
| KDRIVE_API_TOKEN | The API token to use the KDrive API. |
| KSUITE_API_TOKEN | The API token to use the KDrive API. |
| PHX_HOST | The host the web server. (default: example.com) |
| PORT | The port for the web server. (default: 4000) |
| SECRET_KEY_BASE | Secret key used by the [phoenix framework](https://hexdocs.pm/phoenix/deployment.html#handling-of-your-application-secrets). |

```yaml
version: '3'
services:
kdrive-bridge:
image: minidfx/kdrive-bridge:v0.3.0
ksuite-middleware:
image: minidfx/ksuite-middleware:v0.4.0
environment:
- SECRET_KEY_BASE=<secret>
- PHX_HOST=<host>
- PORT=<port>
- KDRIVE_ID=<id>
- KDRIVE_API_TOKEN=<token>
- KSUITE_API_TOKEN=<token>
ports:
- 4000:4000
```
## Running with Docker (recommended way)
With Docker, running the application is a breeze. Simply update the environment variables SECRET_KEY_BASE, PHX_HOST, KDRIVE_ID, and KDRIVE_API_TOKEN. Then, run it to kickstart the application.
With Docker, running the application is a breeze. Simply update the environment variables SECRET_KEY_BASE, PHX_HOST, KDRIVE_ID, and KSUITE_API_TOKEN. Then, run it to kickstart the application.
```bash
docker-compose up
Expand Down
8 changes: 4 additions & 4 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@
# General application configuration
import Config

config :kdrive_bridge,
config :ksuite_middleware,
generators: [timestamp_type: :utc_datetime]

# Configures the endpoint
config :kdrive_bridge, KdriveBridgeWeb.Endpoint,
config :ksuite_middleware, KsuiteMiddlewareWeb.Endpoint,
url: [host: "localhost"],
adapter: Phoenix.Endpoint.Cowboy2Adapter,
render_errors: [
formats: [json: KdriveBridgeWeb.ErrorJSON],
formats: [json: KsuiteMiddlewareWeb.ErrorJSON],
layout: false
],
pubsub_server: KdriveBridge.PubSub,
pubsub_server: KsuiteMiddleware.PubSub,
live_view: [signing_salt: "7nfCI6sX"]

# Configures Elixir's Logger
Expand Down
4 changes: 2 additions & 2 deletions config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Config
# The watchers configuration can be used to run external
# watchers to your application. For example, we can use it
# to bundle .js and .css sources.
config :kdrive_bridge, KdriveBridgeWeb.Endpoint,
config :ksuite_middleware, KsuiteMiddlewareWeb.Endpoint,
# Binding to loopback ipv4 address prevents access from other machines.
# Change to `ip: {0, 0, 0, 0}` to allow access from other machines.
http: [ip: {0, 0, 0, 0}, port: 4000],
Expand Down Expand Up @@ -40,7 +40,7 @@ config :kdrive_bridge, KdriveBridgeWeb.Endpoint,
# different ports.

# Enable dev routes for dashboard and mailbox
config :kdrive_bridge, dev_routes: true
config :ksuite_middleware, dev_routes: true

# Do not include metadata nor timestamps in development logs
config :logger, :console, format: "[$level] $message\n"
Expand Down
16 changes: 8 additions & 8 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import Config
# If you use `mix release`, you need to explicitly enable the server
# by passing the PHX_SERVER=true when you start it:
#
# PHX_SERVER=true bin/kdrive_bridge start
# PHX_SERVER=true bin/ksuite_middleware start
#
# Alternatively, you can use `mix phx.gen.release` to generate a `bin/server`
# script that automatically sets the env var above.
if System.get_env("PHX_SERVER") do
config :kdrive_bridge, KdriveBridgeWeb.Endpoint, server: true
config :ksuite_middleware, KsuiteMiddlewareWeb.Endpoint, server: true
end

if config_env() == :prod do
Expand All @@ -36,9 +36,9 @@ if config_env() == :prod do
host = System.get_env("PHX_HOST") || "example.com"
port = String.to_integer(System.get_env("PORT") || "4000")

config :kdrive_bridge, :dns_cluster_query, System.get_env("DNS_CLUSTER_QUERY")
config :ksuite_middleware, :dns_cluster_query, System.get_env("DNS_CLUSTER_QUERY")

config :kdrive_bridge, KdriveBridgeWeb.Endpoint,
config :ksuite_middleware, KsuiteMiddlewareWeb.Endpoint,
url: [host: host, port: 443, scheme: "http"],
http: [
# Enable IPv6 and bind on all interfaces.
Expand All @@ -50,16 +50,16 @@ if config_env() == :prod do
],
secret_key_base: secret_key_base

config :kdrive_bridge,
config :ksuite_middleware,
kdrive_id: System.get_env("KDRIVE_ID") || raise("The KDRIVE_ID variable was missing"),
kdrive_api_token: System.get_env("KDRIVE_API_TOKEN") || raise("The KDRIVE_API_TOKEN variable was missing")
ksuite_api_token: System.get_env("KSUITE_API_TOKEN") || raise("The KSUITE_API_TOKEN variable was missing")

# ## SSL Support
#
# To get SSL working, you will need to add the `https` key
# to your endpoint configuration:
#
# config :kdrive_bridge, KdriveBridgeWeb.Endpoint,
# config :ksuite_middleware, KsuiteMiddlewareWeb.Endpoint,
# https: [
# ...,
# port: 443,
Expand All @@ -81,7 +81,7 @@ if config_env() == :prod do
# We also recommend setting `force_ssl` in your endpoint, ensuring
# no data is ever sent via http, always redirecting to https:
#
# config :kdrive_bridge, KdriveBridgeWeb.Endpoint,
# config :ksuite_middleware, KsuiteMiddlewareWeb.Endpoint,
# force_ssl: [hsts: true]
#
# Check `Plug.SSL` for all available options in `force_ssl`.
Expand Down
2 changes: 1 addition & 1 deletion config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Config

# We don't run a server during test. If one is required,
# you can enable the server option below.
config :kdrive_bridge, KdriveBridgeWeb.Endpoint,
config :ksuite_middleware, KsuiteMiddlewareWeb.Endpoint,
http: [ip: {127, 0, 0, 1}, port: 4002],
secret_key_base: "BQY9JpU3sNCk28WVxTGzJMma6M4DIiMD+4L/OskOMPSgOXp08t6OBveo2ccdxYxr",
server: false
Expand Down
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
version: '3'
services:
kdrive-bridge:
ksuite-middleware:
build: .
image: minidfx/kdrive-bridge:v0.3.0
image: minidfx/ksuite-middleware:v0.4.0
environment:
- SECRET_KEY_BASE=<secret>
- PHX_HOST=<host>
- PORT=<port>
- KDRIVE_ID=<id>
- KDRIVE_API_TOKEN=<token>
- KSUITE_API_TOKEN=<token>
ports:
- 4000:4000
4 changes: 2 additions & 2 deletions lib/kdrive_bridge.ex → lib/ksuite_middleware.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule KdriveBridge do
defmodule KsuiteMiddleware do
@moduledoc """
KdriveBridge keeps the contexts that define your domain
KsuiteMiddleware keeps the contexts that define your domain
and business logic.
Contexts are also responsible for managing your data, regardless
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule KdriveBridge.Application do
defmodule KsuiteMiddleware.Application do
# See https://hexdocs.pm/elixir/Application.html
# for more information on OTP Applications
@moduledoc false
Expand All @@ -8,26 +8,26 @@ defmodule KdriveBridge.Application do
@impl true
def start(_type, _args) do
children = [
KdriveBridgeWeb.Telemetry,
{DNSCluster, query: Application.get_env(:kdrive_bridge, :dns_cluster_query) || :ignore},
{Phoenix.PubSub, name: KdriveBridge.PubSub},
# Start a worker by calling: KdriveBridge.Worker.start_link(arg)
# {KdriveBridge.Worker, arg},
KsuiteMiddlewareWeb.Telemetry,
{DNSCluster, query: Application.get_env(:ksuite_middleware, :dns_cluster_query) || :ignore},
{Phoenix.PubSub, name: KsuiteMiddleware.PubSub},
# Start a worker by calling: KsuiteMiddleware.Worker.start_link(arg)
# {KsuiteMiddleware.Worker, arg},
# Start to serve requests, typically the last entry
KdriveBridgeWeb.Endpoint
KsuiteMiddlewareWeb.Endpoint
]

# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: KdriveBridge.Supervisor]
opts = [strategy: :one_for_one, name: KsuiteMiddleware.Supervisor]
Supervisor.start_link(children, opts)
end

# Tell Phoenix to update the endpoint configuration
# whenever the application is updated.
@impl true
def config_change(changed, _new, removed) do
KdriveBridgeWeb.Endpoint.config_change(changed, removed)
KsuiteMiddlewareWeb.Endpoint.config_change(changed, removed)
:ok
end
end
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
defmodule KdriveBridge.KdriveClient do
defmodule KsuiteMiddleware.KsuiteClient do
require Logger
use Tesla

plug Tesla.Middleware.BaseUrl, "https://api.infomaniak.com"
plug Tesla.Middleware.Logger, debug: false
plug Tesla.Middleware.Headers, [{"User-Agent", "kdrive-bridge"}]
plug Tesla.Middleware.Headers, [{"Authorization", "Bearer #{Application.get_env(:kdrive_bridge, :kdrive_api_token)}"}]
plug Tesla.Middleware.Headers, [{"User-Agent", "ksuite-middleware"}]
plug Tesla.Middleware.Headers, [{"Authorization", "Bearer #{Application.get_env(:ksuite_middleware, :ksuite_api_token)}"}]
plug Tesla.Middleware.Headers, [{"Content-Type", "application/json"}]
plug Tesla.Middleware.FollowRedirects, max_redirects: 1

@spec download(integer()) :: {:error, any()} | {:ok, Tesla.Env.t()}
def download(file_id) when is_integer(file_id) do
kdrive_id = Application.get_env(:kdrive_bridge, :kdrive_id)
kdrive_id = Application.get_env(:ksuite_middleware, :kdrive_id)
get("/2/drive/#{kdrive_id}/files/#{file_id}/download")
end

@spec download_as(integer(), bitstring()) :: {:error, any()} | {:ok, Tesla.Env.t()}
def download_as(file_id, as \\ "pdf") when is_integer(file_id) and is_bitstring(as) do
kdrive_id = Application.get_env(:kdrive_bridge, :kdrive_id)
kdrive_id = Application.get_env(:ksuite_middleware, :kdrive_id)
get("/2/drive/#{kdrive_id}/files/#{file_id}/download", query: [as: as])
end
end
14 changes: 7 additions & 7 deletions lib/kdrive_bridge_web.ex → lib/ksuite_middleware_web.ex
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
defmodule KdriveBridgeWeb do
defmodule KsuiteMiddlewareWeb do
@moduledoc """
The entrypoint for defining your web interface, such
as controllers, components, channels, and so on.
This can be used in your application as:
use KdriveBridgeWeb, :controller
use KdriveBridgeWeb, :html
use KsuiteMiddlewareWeb, :controller
use KsuiteMiddlewareWeb, :html
The definitions below will be executed for every controller,
component, etc, so keep them short and clean, focused
Expand Down Expand Up @@ -39,7 +39,7 @@ defmodule KdriveBridgeWeb do
quote do
use Phoenix.Controller,
formats: [:html, :json],
layouts: [html: KdriveBridgeWeb.Layouts]
layouts: [html: KsuiteMiddlewareWeb.Layouts]

import Plug.Conn

Expand All @@ -50,9 +50,9 @@ defmodule KdriveBridgeWeb do
def verified_routes do
quote do
use Phoenix.VerifiedRoutes,
endpoint: KdriveBridgeWeb.Endpoint,
router: KdriveBridgeWeb.Router,
statics: KdriveBridgeWeb.static_paths()
endpoint: KsuiteMiddlewareWeb.Endpoint,
router: KsuiteMiddlewareWeb.Router,
statics: KsuiteMiddlewareWeb.static_paths()
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule KdriveBridgeWeb.ErrorJSON do
defmodule KsuiteMiddlewareWeb.ErrorJSON do
# If you want to customize a particular status code,
# you may add your own clauses, such as:
#
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
defmodule KdriveBridgeWeb.MainController do
use KdriveBridgeWeb, :controller
defmodule KsuiteMiddlewareWeb.MainController do
use KsuiteMiddlewareWeb, :controller

alias KdriveBridge.KdriveClient
alias KsuiteMiddleware.KsuiteClient

def index(conn, _params),
do:
conn
|> put_resp_header("content-type", "text/html; charset=utf-8")
|> send_file(200, Application.app_dir(:kdrive_bridge, "priv/static/index.html"))
|> send_file(200, Application.app_dir(:ksuite_middleware, "priv/static/index.html"))

def not_found(conn, _params),
do:
conn
|> put_resp_header("content-type", "text/html; charset=utf-8")
|> send_file(404, Application.app_dir(:kdrive_bridge, "priv/static/not-found.html"))
|> send_file(404, Application.app_dir(:ksuite_middleware, "priv/static/not-found.html"))

def pass_thru(conn, %{"file_id" => id}) when is_integer(id) do
with {:ok, response} <- KdriveClient.download(id) do
with {:ok, response} <- KsuiteClient.download(id) do
conn |> put_tesla_response(response)
else
_ -> conn |> resp(500, "an unknown error occurred.")
Expand All @@ -25,7 +25,7 @@ defmodule KdriveBridgeWeb.MainController do

def pass_thru(conn, %{"file_id" => raw_id}) do
with {file_id, _} <- Integer.parse(raw_id),
{:ok, response} <- KdriveClient.download(file_id) do
{:ok, response} <- KsuiteClient.download(file_id) do
conn |> put_tesla_response(response)
else
_ ->
Expand All @@ -40,7 +40,7 @@ defmodule KdriveBridgeWeb.MainController do
do:
conn
|> put_resp_header("content-type", "text/html; charset=utf-8")
|> send_file(404, Application.app_dir(:kdrive_bridge, "priv/static/bad-api-key.html"))
|> send_file(404, Application.app_dir(:ksuite_middleware, "priv/static/bad-api-key.html"))

defp put_tesla_response(%Plug.Conn{} = conn, %Tesla.Env{} = response) do
%Tesla.Env{status: status, body: body} = response
Expand Down
Loading

0 comments on commit f3bd838

Please sign in to comment.