This commit is contained in:
Ian Morland
2021-05-27 18:45:59 +01:00
commit 504e73d3fb
14 changed files with 5456 additions and 0 deletions

4
.gitattributes vendored Normal file
View File

@@ -0,0 +1,4 @@
js/src export-ignore
.git* export-ignore
js/dist/*.js -diff

16
.github/workflows/build.yml vendored Normal file
View File

@@ -0,0 +1,16 @@
name: Build JavaScript assets
on:
push:
branches:
- master
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- uses: flarum/action-build@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

237
.gitignore vendored Normal file
View File

@@ -0,0 +1,237 @@
# Created by https://www.toptal.com/developers/gitignore/api/vim,yarn,node,linux,macos,windows,visualstudiocode,composer
# Edit at https://www.toptal.com/developers/gitignore?templates=vim,yarn,node,linux,macos,windows,visualstudiocode,composer
### Composer ###
composer.phar
/vendor/
# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
# composer.lock
### Linux ###
*~
# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*
# KDE directory preferences
.directory
# Linux trash folder which might appear on any partition or disk
.Trash-*
# .nfs files are created when an open file is removed but is still being accessed
.nfs*
### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# TypeScript v1 declaration files
typings/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
.env.test
# parcel-bundler cache (https://parceljs.org/)
.cache
# Next.js build output
.next
# Nuxt.js build / generate output
.nuxt
#dist
# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
# Stores VSCode versions used for testing VSCode extensions
.vscode-test
### Vim ###
# Swap
[._]*.s[a-v][a-z]
!*.svg # comment out if you don't need vector files
[._]*.sw[a-p]
[._]s[a-rt-v][a-z]
[._]ss[a-gi-z]
[._]sw[a-p]
# Session
Session.vim
Sessionx.vim
# Temporary
.netrwhist
# Auto-generated tag files
tags
# Persistent undo
[._]*.un~
### VisualStudioCode ###
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
*.code-workspace
### VisualStudioCode Patch ###
# Ignore all local history of files
.history
### Windows ###
# Windows thumbnail cache files
Thumbs.db
Thumbs.db:encryptable
ehthumbs.db
ehthumbs_vista.db
# Dump file
*.stackdump
# Folder config file
[Dd]esktop.ini
# Recycle Bin used on file shares
$RECYCLE.BIN/
# Windows Installer files
*.cab
*.msi
*.msix
*.msm
*.msp
# Windows shortcuts
*.lnk
### yarn ###
# https://yarnpkg.com/advanced/qa#which-files-should-be-gitignored
# .yarn/unplugged and .yarn/build-state.yml should likely always be ignored since
# they typically hold machine-specific build artifacts. Ignoring them might however
# prevent Zero-Installs from working (to prevent this, set enableScripts to false).
.yarn/unplugged
.yarn/build-state.yml
# .yarn/cache and .pnp.* may be safely ignored, but you'll need to run yarn install
# to regenerate them between each branch switch.
# Uncomment the following lines if you're not using Zero-Installs:
# .yarn/cache
# .pnp.*
# End of https://www.toptal.com/developers/gitignore/api/vim,yarn,node,linux,macos,windows,visualstudiocode,composer

7
LICENSE.md Normal file
View File

@@ -0,0 +1,7 @@
MIT License Copyright (c) 2021 Glowing Blue AG
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

9
README.md Normal file
View File

@@ -0,0 +1,9 @@
# GB Redis Setup
![License](https://img.shields.io/badge/license-MIT-blue.svg)
A [Flarum](http://flarum.org) extension. Redis Cache, Queue & Sessions setup (uses env variables for the Redis config)
## Setting up Redis Cache & Queue for a Flarum project

43
composer.json Normal file
View File

@@ -0,0 +1,43 @@
{
"name": "glowingblue/redis-setup",
"description": "Redis Cache, Queue & Session setup (uses env variables for the Redis config)",
"keywords": [
"flarum", "redis", "cache", "queue", "session"
],
"type": "flarum-extension",
"license": "MIT",
"support": {
"issues": "https://github.com/glowingblue/flarum-ext-redis-setup/issues",
"source": "https://github.com/glowingblue/flarum-ext-redis-setup",
"forum": "https://community.glowingblue.com/t/flarum-corner",
"email": "dev@glowingblue.com"
},
"homepage": "https://glowingblue.com",
"require": {
"flarum/core": "^1.0.0",
"blomstra/flarum-redis": "^0.4"
},
"authors": [
{
"name": "IanM",
"homepage": "https://discuss.flarum.org/u/ianm",
"role": "Developer"
}
],
"autoload": {
"psr-4": {
"GlowingBlue\\RedisSetup\\": "src/"
}
},
"extra": {
"flarum-extension": {
"title": "GB Redis Setup",
"category": "feature",
"icon": {
"name": "fas fa-tasks",
"backgroundColor": "#00a7e3",
"color": "#fff"
}
}
}
}

24
extend.php Normal file
View File

@@ -0,0 +1,24 @@
<?php
/*
* This file is part of glowingblue/redis-setup.
*
* Copyright (c) 2021 Ian Morland.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/
namespace GlowingBlue\RedisSetup;
use Flarum\Extend;
use GlowingBlue\RedisSetup\Extend\EnableRedis;
return [
(new Extend\Frontend('admin'))
->js(__DIR__.'/js/dist/admin.js'),
new Extend\Locales(__DIR__.'/resources/locale'),
new EnableRedis()
];

1
js/admin.js Normal file
View File

@@ -0,0 +1 @@
export * from './src/admin';

4953
js/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

18
js/package.json Normal file
View File

@@ -0,0 +1,18 @@
{
"name": "@glowingblue/redis-setup",
"version": "0.0.0",
"private": true,
"dependencies": {
"flarum-webpack-config": "^0.1.0-beta.10",
"webpack": "^4.26.0",
"webpack-cli": "^3.0.7"
},
"scripts": {
"dev": "webpack --mode development --watch",
"build": "webpack --mode production",
"lint": "prettier --single-quote --jsx-single-quote --trailing-comma es5 --print-width 100 --use-tabs --tab-width 4 --write src"
},
"devDependencies": {
"prettier": "^2.3.0"
}
}

23
js/src/admin/index.js Normal file
View File

@@ -0,0 +1,23 @@
import app from 'flarum/admin/app';
app.initializers.add('glowingblue-redis-setup', () => {
app.extensionData
.for('glowingblue-redis-setup')
.registerSetting({
setting: 'glowingblue-redis.enableCache',
type: 'boolean',
label: app.translator.trans('glowingblue-redis-setup.admin.settings.enable_cache'),
})
.registerSetting({
setting: 'glowingblue-redis.redisSessions',
type: 'boolean',
label: app.translator.trans(
'glowingblue-redis-setup.admin.settings.enable_redis_sessions'
),
})
.registerSetting({
setting: 'glowingblue-redis.enableQueue',
type: 'boolean',
label: app.translator.trans('glowingblue-redis-setup.admin.settings.enable_queue'),
});
});

1
js/webpack.config.js Normal file
View File

@@ -0,0 +1 @@
module.exports = require('flarum-webpack-config')();

6
resources/locale/en.yml Normal file
View File

@@ -0,0 +1,6 @@
glowingblue-redis-setup:
admin:
settings:
enable_cache: Enable Redis cache
enable_redis_sessions: Enable Redis sessions (all users will be logged out after changing this setting)
enable_queue: Enable Redis queue

114
src/Extend/EnableRedis.php Normal file
View File

@@ -0,0 +1,114 @@
<?php
namespace GlowingBlue\RedisSetup\Extend;
use Blomstra\Redis\Extend\Redis;
use Flarum\Extend\ExtenderInterface;
use Flarum\Extension\Extension;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Container\Container;
use Illuminate\Support\Arr;
class EnableRedis implements ExtenderInterface
{
const CACHE_KEY = 'connections.cache';
const QUEUE_KEY = 'connections.queue';
const SESSION_KEY = 'connections.session';
public function extend(Container $container, Extension $extension = null)
{
$config = $this->buildConfig();
if (Arr::hasAny($config, [self::CACHE_KEY, self::QUEUE_KEY, self::SESSION_KEY])) {
(new Redis($config))->disable($this->getDisabledServices())->extend($container, $extension);
}
}
private function getDisabledServices(): array
{
/** @var SettingsRepositoryInterface */
$settings = resolve(SettingsRepositoryInterface::class);
$disabled = [];
if (!(bool) $settings->get('glowingblue-redis.enableCache', false)) {
$disabled[] = 'cache';
}
if (!(bool) $settings->get('glowingblue-redis.enableQueue', false)) {
$disabled[] = 'queue';
}
if (!(bool) $settings->get('glowingblue-redis.redisSessions', false)) {
$disabled[] = 'session';
}
return $disabled;
}
private function buildConfig(): array
{
if ($this->getHost() === null) {
return [];
}
$config = [];
$cache = [
'host' => $this->getHost(),
'password' => $this->getPassword(),
'port' => $this->getPort(),
'database' => $this->getCacheDatabase(),
];
$queue = [
'host' => $this->getHost(),
'password' => $this->getPassword(),
'port' => $this->getPort(),
'database' => $this->getQueueDatabase(),
];
$session = [
'host' => $this->getHost(),
'password' => $this->getPassword(),
'port' => $this->getPort(),
'database' => $this->getSessionDatabase(),
];
$config = Arr::add($config, self::CACHE_KEY, $cache);
$config = Arr::add($config, self::QUEUE_KEY, $queue);
$config = Arr::add($config, self::SESSION_KEY, $session);
return $config;
}
private function getHost(): ?string
{
return getenv('REDIS_HOST') ? getenv('REDIS_HOST') : null;
}
private function getPassword(): ?string
{
return getenv('REDIS_PASSWORD') ? getenv('REDIS_PASSWORD') : null;
}
private function getPort(): string
{
return getenv('REDIS_PORT') ? getenv('REDIS_PORT') : '6379';
}
private function getCacheDatabase(): int
{
return (int) getenv('REDIS_DATABASE_CACHE') ? getenv('REDIS_DATABASE_CACHE') : 1;
}
private function getQueueDatabase(): int
{
return (int) getenv('REDIS_DATABASE_QUEUE') ? getenv('REDIS_DATABASE_QUEUE') : 2;
}
private function getSessionDatabase(): int
{
return (int) getenv('REDIS_DATABASE_SESSION') ? getenv('REDIS_DATABASE_SESSION') : 3;
}
}