Skip to content

Commit

Permalink
Move filesystem config into .env instead of Settings
Browse files Browse the repository at this point in the history
  • Loading branch information
samwilson committed Sep 6, 2021
1 parent b64a175 commit 65d0f22
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 111 deletions.
9 changes: 9 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ MAILER_DSN=smtp://localhost
APP_MAIN_CONTACT=1
APP_REQUIRE_2FA=true

APP_FS_DATA_STORE=local
APP_FS_DATA_DIR=
APP_FS_TEMP_DIR=
APP_FS_AWS_REGION=
APP_FS_AWS_ENDPOINT=
APP_FS_AWS_BUCKET=
APP_FS_AWS_KEY=
APP_FS_AWS_SECRET=

APP_MAIL_SENDER=user@example.org
APP_LOG_RECIPIENT=user@example.org

Expand Down
13 changes: 12 additions & 1 deletion config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,21 @@ services:

App\Settings:
arguments:
$projectDir: '%kernel.project_dir%'
$mailFrom: '%env(APP_MAIL_SENDER)%'
$mainContactId: '%env(APP_MAIN_CONTACT)%'

App\Filesystems:
arguments:
$projectDir: '%kernel.project_dir%'
$dataStore: '%env(APP_FS_DATA_STORE)%'
$dataDir: '%env(APP_FS_DATA_DIR)%'
$tempDir: '%env(APP_FS_TEMP_DIR)%'
$awsRegion: '%env(APP_FS_AWS_REGION)%'
$awsEndpoint: '%env(APP_FS_AWS_ENDPOINT)%'
$awsBucket: '%env(APP_FS_AWS_BUCKET)%'
$awsKey: '%env(APP_FS_AWS_KEY)%'
$awsSecret: '%env(APP_FS_AWS_SECRET)%'

App\WebRequestProcessor:
tags: { name: monolog.processor }
autowire: true
Expand Down
55 changes: 49 additions & 6 deletions docs/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,55 @@ Configuration
=============

All the important parts of configuration of a Twyne site
happen in the ``.env.local`` file::
happen in the ``.env.local`` file.
The minimum that should be set are as follows:

APP_SECRET=a-long-random-string
APP_MAIL_SENDER=admin@example.org
APP_LOG_RECIPIENT=admin@example.org
MAILER_DSN=smtp://username:password@smtp.gmail.com
.. code-block:: shell
Other configuration happens on the Settings page,
APP_SECRET=a-long-random-string
APP_MAIL_SENDER=admin@example.org
APP_LOG_RECIPIENT=admin@example.org
MAILER_DSN=smtp://username:password@smtp.gmail.com
Other configuration
(that is likely to be changed occasionally during the normal operation of the site)
happens on the Settings page,
accessible via the main menu.

Uploaded files
--------------

There are two options for file storage: the local filesystem, and S3_-compatible object stores
(such as those from AWS, Digital Ocean, OVH, Dreamhost, etc.).

.. _S3: https://en.wikipedia.org/wiki/Amazon_S3

The local filesystem is the default, and files will be stored in the ``var/app_data/`` directory.
This location can be changed via ``.env.local``:

.. code-block:: shell
APP_FS_DATA_DIR=/path/to/your/data_dir/
To use S3-compatible storage, set the following environment variables:

.. code-block:: shell
APP_FS_DATA_STORE=aws
APP_FS_AWS_REGION=
APP_FS_AWS_ENDPOINT=
APP_FS_AWS_BUCKET=
APP_FS_AWS_KEY=
APP_FS_AWS_SECRET=
Uploaded files will have various derivative files created for them, such as thumbnails of images.
These files will be stored in the ``var/app_temp/`` directory by default.
This directory can be changed in ``.env.local``:

.. code-block:: shell
APP_FS_TEMP_DIR=/path/to/your/tmp_dir/
The temporary directory contains only files that will be regenerated as required
(hence the name 'temporary'; these are however long-lived files).
There is no need to back up this directory.
67 changes: 52 additions & 15 deletions src/Filesystems.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,49 @@
class Filesystems
{

/** @var Settings */
private $settings;

public function __construct(Settings $settings)
{
$this->settings = $settings;
/** @var string */
private $dataStore;

/** @var string */
private $dataDir;

/** @var string */
private $tempDir;

/** @var string */
private $awsRegion;

/** @var string */
private $awsEndpoint;

/** @var string */
private $awsBucket;

/** @var string */
private $awsKey;

/** @var string */
private $awsSecret;

public function __construct(
string $projectDir,
string $dataStore,
string $dataDir,
string $tempDir,
string $awsRegion,
string $awsEndpoint,
string $awsBucket,
string $awsKey,
string $awsSecret
) {
$this->dataStore = $dataStore;
$this->dataDir = rtrim(!empty($dataDir) ? $dataDir : $projectDir . '/var/app_data', '/') . '/';
$this->tempDir = rtrim(!empty($tempDir) ? $tempDir : $projectDir . '/var/app_tmp/', '/') . '/';
$this->awsRegion = $awsRegion;
$this->awsEndpoint = $awsEndpoint;
$this->awsBucket = $awsBucket;
$this->awsKey = $awsKey;
$this->awsSecret = $awsSecret;
}

private function getDataStoragePath(File $file): string
Expand Down Expand Up @@ -109,31 +146,31 @@ public function read(File $file, string $size)

public function tempRoot(): string
{
return $this->settings->tempDir();
return $this->tempDir;
}

public function temp(): Filesystem
{
$adapter = new Local($this->settings->tempDir());
$adapter = new Local($this->tempRoot());
return new Filesystem($adapter);
}

public function data(): Filesystem
{
if ($this->settings->dataStore() === 'aws') {
if ($this->dataStore === 'aws') {
$options = [
'version' => '2006-03-01',
'credentials' => [
'key' => $this->settings->awsKey(),
'secret' => $this->settings->awsSecret(),
'key' => $this->awsKey,
'secret' => $this->awsSecret,
],
'endpoint' => $this->settings->awsEndpoint(),
'region' => $this->settings->awsRegion(),
'endpoint' => $this->awsEndpoint,
'region' => $this->awsRegion,
];
$client = new S3Client($options);
$adapter = new AwsS3Adapter($client, $this->settings->awsBucketName());
$adapter = new AwsS3Adapter($client, $this->awsBucket);
} else {
$adapter = new Local($this->settings->dataDir());
$adapter = new Local($this->dataDir);
}
return new Filesystem($adapter);
}
Expand Down
45 changes: 0 additions & 45 deletions src/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ class Settings
/** @var EntityManagerInterface */
private $entityManager;

/** @var string */
private $projectDir;

/** @var string */
private $mailFrom;

Expand All @@ -40,14 +37,12 @@ public function __construct(
ContactRepository $contactRepository,
int $mainContactId,
EntityManagerInterface $entityManager,
string $projectDir,
string $mailFrom
) {
$this->settingRepository = $settingRepository;
$this->contactRepository = $contactRepository;
$this->mainContactId = $mainContactId;
$this->entityManager = $entityManager;
$this->projectDir = $projectDir;
$this->mailFrom = $mailFrom;
}

Expand Down Expand Up @@ -99,51 +94,11 @@ public function getMailFrom()
return $this->mailFrom;
}

public function dataStore(): string
{
return $this->getData()['data_store'] ?? 'local';
}

public function dataDir(): string
{
return rtrim($this->getData()['data_dir'] ?? $this->projectDir . '/var/app_data', '/') . '/';
}

public function tempDir(): string
{
return rtrim($this->getData()['temp_dir'] ?? $this->projectDir . '/var/app_tmp/', '/') . '/';
}

public function apiKey(): string
{
return $this->getData()['api_key'] ?? '';
}

public function awsKey(): string
{
return $this->getData()['aws_key'] ?? '';
}

public function awsSecret(): string
{
return $this->getData()['aws_secret'] ?? '';
}

public function awsRegion(): string
{
return $this->getData()['aws_region'] ?? '';
}

public function awsEndpoint(): string
{
return $this->getData()['aws_endpoint'] ?? '';
}

public function awsBucketName(): string
{
return $this->getData()['aws_bucket_name'] ?? '';
}

public function flickrApiKey(): string
{
return $this->getData()['flickr_api_key'] ?? '';
Expand Down
44 changes: 0 additions & 44 deletions templates/setting/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -25,50 +25,6 @@
<input id="api_key" type="text" name="settings[api_key]" value="{{ settings.apiKey }}" />
</p>

<fieldset>
<legend>File storage</legend>
<p class="fields">
<label><input type="radio" name="settings[data_store]" value="local" {% if settings.dataStore == 'local' %}checked{% endif %} /> Local</label>
<label><input type="radio" name="settings[data_store]" value="aws" {% if settings.dataStore == 'aws' %}checked{% endif %} /> AWS</label>
</p>
<p class="fields">
<span class="field size-1">
<label for="data_dir">Data directory:</label>
<input id="data_dir" type="text" value="{{ settings.dataDir }}" name="settings[data_dir]" />
</span>
</p>
<p class="fields">
<span class="field size-1">
<label for="temp_dir">Temporary directory:</label>
<input id="temp_dir" type="text" value="{{ settings.tempDir }}" name="settings[temp_dir]" />
</span>
</p>
<p class="fields">
<span class="field size-1">
<label for="aws_key">AWS Key:</label>
<input id="aws_key" type="text" value="{{ settings.awsKey }}" name="settings[aws_key]" />
</span>
<span class="field size-1">
<label for="aws_secret">AWS Secret:</label>
<input id="aws_secret" type="text" value="{{ settings.awsSecret }}" name="settings[aws_secret]" />
</span>
</p>
<p class="fields">
<span class="field size-1">
<label for="aws_endpoint">AWS Endpoint:</label>
<input id="aws_endpoint" type="text" value="{{ settings.awsEndpoint }}" name="settings[aws_endpoint]" />
</span>
<span class="field size-1">
<label for="aws_region">AWS Region:</label>
<input id="aws_region" type="text" value="{{ settings.awsRegion }}" name="settings[aws_region]" />
</span>
<span class="field size-1">
<label for="aws_bucket_name">AWS Bucket Name:</label>
<input id="aws_bucket_name" type="text" value="{{ settings.awsBucketName }}" name="settings[aws_bucket_name]" />
</span>
</p>
</fieldset>

<fieldset>
<legend>Flickr</legend>
<p class="fields">
Expand Down

0 comments on commit 65d0f22

Please sign in to comment.