From 05746e9e12062ca9d4bcf69b702dd8f9a6fe353a Mon Sep 17 00:00:00 2001 From: Nicholas Date: Mon, 22 Jan 2024 20:25:44 +0100 Subject: [PATCH] bug: move env qr code max upload length to runtime (#176) Co-authored-by: Jannik Streek --- config/config.exs | 4 ---- config/runtime.exs | 4 ++++ docker-compose.yml | 12 ++++++------ lib/qrstorage_web/endpoint.ex | 11 ++++++----- 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/config/config.exs b/config/config.exs index fdf9284..17402ac 100644 --- a/config/config.exs +++ b/config/config.exs @@ -28,10 +28,6 @@ config :qrstorage, QrstorageWeb.Endpoint, layout: false ] -# This is what will be used for the calculation on the client side. We add a buffer to account for deltas -# and overhead in the endpoint configuration, so the server actually allows for a bit more: -config :qrstorage, max_upload_length: System.get_env("QR_CODE_MAX_UPLOAD_LENGTH", "2666666") - # Configures Elixir's Logger config :logger, :console, format: "$time $metadata[$level] $message\n", diff --git a/config/runtime.exs b/config/runtime.exs index 1223eeb..daf1474 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -54,6 +54,10 @@ config :qrstorage, Qrstorage.Repo, ] ] +# This is what will be used for the calculation on the client side. We add a buffer to account for deltas +# and overhead in the endpoint configuration, so the server actually allows for a bit more: +config :qrstorage, max_upload_length: System.get_env("QR_CODE_MAX_UPLOAD_LENGTH", "2666666") + # Set possible translations default_locale = case config_env() do diff --git a/docker-compose.yml b/docker-compose.yml index 154dd0e..0ea25d1 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,9 +7,9 @@ services: target: development tty: true environment: - DATABASE_USER: ${POSTGRES_USER:-postgres} - DATABASE_USER_PASSWORD: ${POSTGRES_PASSWORD:-postgres} - DATABASE_NAME: ${POSTGRES_DB:-qrstorage_dev} + DATABASE_USER: ${POSTGRES_USER:-qrstorage-user} + DATABASE_USER_PASSWORD: ${POSTGRES_PASSWORD:-qrstorage-password} + DATABASE_NAME: ${POSTGRES_DB:-qrstorage-dev} DATABASE_PORT: 5432 DATABASE_HOST: postgres DATABASE_SSL: "false" @@ -80,9 +80,9 @@ services: postgres: image: postgres:15 environment: - POSTGRES_USER: ${POSTGRES_USER:-postgres} - POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-postgres} - POSTGRES_DB: ${POSTGRES_DB:-qrstorage_dev} + POSTGRES_USER: ${POSTGRES_USER:-qrstorage-user} + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-qrstorage-password} + POSTGRES_DB: ${POSTGRES_DB:-qrstorage-dev} PGDATA: /var/lib/postgresql/data/pgdata restart: always # Exposing the port is not needed unless you want to access this database instance from the host. diff --git a/lib/qrstorage_web/endpoint.ex b/lib/qrstorage_web/endpoint.ex index 70b67a9..fcf7060 100644 --- a/lib/qrstorage_web/endpoint.ex +++ b/lib/qrstorage_web/endpoint.ex @@ -1,5 +1,6 @@ defmodule QrstorageWeb.Endpoint do use Phoenix.Endpoint, otp_app: :qrstorage + require Logger # The session will be stored in the cookie and signed, # this means its contents can be read but not tampered with. @@ -36,16 +37,16 @@ defmodule QrstorageWeb.Endpoint do plug Plug.RequestId plug Plug.Telemetry, event_prefix: [:phoenix, :endpoint] - # account for deltas and overhead for the upload length. deltas are roughly the same size as the actual input, - # so * 2 for deltas, plus 0.2 buffer for overhead and text characters: upload_length_buffer = 2.2 - max_upload_length = String.to_integer(Application.compile_env(:qrstorage, :max_upload_length)) + max_upload_length = String.to_integer(System.get_env("QR_CODE_MAX_UPLOAD_LENGTH", "2666666")) + + Logger.info("Reading QR_CODE_MAX_UPLOAD_LENGTH as: #{max_upload_length}") plug Plug.Parsers, parsers: [:urlencoded, :multipart, :json], pass: ["*/*"], - json_decoder: Phoenix.json_library(), - length: ceil(max_upload_length * upload_length_buffer) + length: ceil(max_upload_length * upload_length_buffer), + json_decoder: Phoenix.json_library() plug Plug.MethodOverride plug Plug.Head