Skip to content

Commit

Permalink
refactor: 🔨 general refactoring
Browse files Browse the repository at this point in the history
Move the views directory into the resources/ directory; trim app config and create a cors and a view
config from the trimmed parts; add more documentation to app config; use framework functions to
unify resource and storage paths
  • Loading branch information
simplymichael committed Aug 27, 2024
1 parent ec6121c commit ea92826
Show file tree
Hide file tree
Showing 39 changed files with 201 additions and 131 deletions.
74 changes: 31 additions & 43 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,67 +1,55 @@
NAME=Simplicity
HOST=http://localhost
PORT=8800
URL_SCHEME=http
API_VERSION=1
APP_NAME=Simplicity
APP_URL=http://localhost
APP_PORT=8800
NODE_ENV=production

DEBUG=false

## Timezone to use for date/time operations
# Timezone to use for date and time operations
TIMEZONE=UTC

# CONNECTION PARAMETERS (Database, Cache, Notifications)

## Database connection settings
## See the config/database.js configuration file.

### The default connection to use for database operations.
### Default is sqlite.
## The default connection to use for database operations.
## sqlite is used by default if none is set here.
DB_CONNECTION=sqlite

### Either set the full URL here...
## Either set the full URL here...
DB_URL=

### ... Or use environment variables
DB_HOST="0.0.0.0"
## ... Or use environment variables
DB_HOST="localhost"
DB_USERNAME=
DB_PASSWORD=
DB_DBNAME=
DB_DBNAME=simplicity_db
DB_PORT=27017

## The storage location for sqlite database data,
## relative to the storage/ directory. Default is ".sqlite".
DB_STORAGE_PATH=.sqlite
# View configuration

## Views configuration
### Views Directory (relative to the src/ directory)
VIEWS_DIR=views
## View template engine (default is pug)
## If you specify a different engine, make sure to install the appropriate package.
VIEW_ENGINE=pug

### View template engine (default is pug)
### If you specify a different engine, make sure to install the appropriate package.
VIEW_TEMPLATES_ENGINE=pug
# Cache and temporary storage

## Cache and temporary storage

### Default storage to use for caching
### Possible values: file|memory|redis.
### Default is memory.
## Default storage to use for caching
## Possible values: file|memory|redis.
## Default is memory.
CACHE_STORE=memory

### There might be other applications using the same cache.
### This can happen, for example, When using the Redis cache store.
### To avoid collisions, you may prefix every cache key.
## There might be other applications using the same cache.
## This can happen, for example, When using the Redis cache store.
## To avoid collisions, you may prefix every cache key.
CACHE_KEY_PREFIX=

### Whether (true) or not (false) to compress data before caching
## Whether (true) or not (false) to compress cached data
CACHE_COMPRESS_DATA=false

### The cache storage location when the CACHE_STORE is file,
### relative to the storage/ directory. Default is ".cache".
CACHE_STORAGE_PATH=.cache

### Either set the Redis URL or set the individual Redis connection options.
### If we set the Redis URL, the individual Redis connection options are ignored.
## Either set the Redis URL or set the individual Redis connection options.
## If we set the Redis URL, the individual Redis connection options are ignored.
REDIS_URL=
REDIS_HOST=localhost
REDIS_PORT=6379
Expand All @@ -79,7 +67,6 @@ SESSION_EXPIRY=0
SESSION_SECRET="secret string"

## whether to serve secure cookies (i.e., only transmit cookie over https)
## 0, "0", false, "false", and "" all evaluate to boolean false.
## Every other value evaluates to true
SESSION_SECURE=false

Expand All @@ -102,17 +89,18 @@ ALLOWED_HEADERS="Origin, X-Requested-With, Content-Type, Accept, Authorization"
## (separate methods with spaces, comma, semicolon, or pipe (|)).
## You can specify the methods in either upper or lower case or
## even a mix of upper and lower cases.
ALLOWED_METHODS="GET, POST put|DelEte"
ALLOWED_METHODS="GET, POST PATCH;put|DelEte OPTIONS"

## Allowed origins
## (separate multiple origins by spaces, comma, semicolon, or pipe(|))
ALLOWED_ORIGINS="http://localhost http://localhost:3000"
## To allow all origins, set the value to *
ALLOWED_ORIGINS="http://localhost http://localhost:8800"

## Logging
## Enable/disable CORS Credentials
USE_CREDENTIALS=false

# Logging
LOG_UNCAUGHT_EXCEPTIONS=false
LOG_PROMISE_REJECTIONS=false
LOG_TO_CONSOLE=true
LOG_TO_FILE=true

## The log directory, relative to the root directory.
LOG_DIRECTORY=".logs"
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ storage
.logs
.log
.sqlite
*.logs
*.cache
*.sqlite
97 changes: 65 additions & 32 deletions src/config/app.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,72 @@
const path = require("node:path");
const env = require("@simplicityjs/framework/env");
const string = require("@simplicityjs/framework/lib/string");

const SPLIT_REGEX = /[\s+,;|]+/;
const APP_ROOT = string.convertBackSlashToForwardSlash(path.resolve(path.dirname(__dirname), ".."));
const APP_SRC_DIR = `${APP_ROOT}/src`;
const HTTP_REGEX = /^https?:\/\//i;

let host = env("HOST");
let port = env("PORT");
let scheme = env("URL_SCHEME", "http")?.toLowerCase();
module.exports = {
/*
* -------------------
* Application Name
* ------------------
*
* This the name of your application. It is used in cases when the
* framework needs to place the application's name in a notification or
* any other location as required by the application or its packages.
*/
name: env("APP_NAME"),

if(!(/^https?(:\/\/)?/.test(scheme))) {
scheme = "http://";
}
/*
* -------------------
* Application Port
* ------------------
*
* This is the port your application should run on.
* It is used when the port is not specified via any other CLI means.
*/
port: env("APP_PORT"),

scheme = scheme.split(/:\/\//)[0] + "://";
host = (HTTP_REGEX.test(host)) ? host: `${scheme}${host}`;
port = [80, 443].includes(Number(port)) ? "" : port;
/*
* -------------------
* Application URL
* ------------------
*
* This is your application's fully qualified URL.
*/
url: env("APP_URL", "http://localhost"),

module.exports = {
name : env("NAME"),
host : env("HOST"),
port : env("PORT"),
url : port ? `${host}:${port}` : host,
urlScheme : scheme,
environment : env("NODE_ENV", "production").toLowerCase(),
apiVersion : env("API_VERSION"),
debug : env("DEBUG"),
timezone : env("TIMEZONE", "UTC").toUpperCase(),
rootDir : APP_ROOT,
srcDir : APP_SRC_DIR,
viewsDir : `${APP_SRC_DIR}/${env("VIEWS_DIR", "views")}`,
allowedHeaders : env("ALLOWED_HEADERS").split(SPLIT_REGEX).map(s => s.trim()),
allowedMethods : env("ALLOWED_METHODS").split(SPLIT_REGEX).map(o => o.trim().toUpperCase()),
allowedOrigins : env("ALLOWED_ORIGINS").split(SPLIT_REGEX).map(o => o.trim()),
viewTemplatesEngine: env("VIEW_TEMPLATES_ENGINE"),
/*
* ------------------------
* Application Environment
* ------------------------
*
* This value determines the "environment" your application is currently
* running in. It helps to determine configuration preferences
* for application services.
*/
environment: env("NODE_ENV", "production").toLowerCase(),

debug: env("DEBUG"),

/*
* ------------------------
* Application Timezone
* ------------------------
*
* This is the default timezone for your application, which
* will be used by the date and time functions.
*/
timezone: env("TIMEZONE", "UTC").toUpperCase(),

/*
* ------------------------
* Maintenance Mode Driver
* ------------------------
*
* This option determines the driver used to determine and
* manage Simplicity's "maintenance mode" status. The "redis" driver
* makes it possible to control maintenance mode across multiple machines.
*
* Supported drivers include: "file" and "redis"
*/
maintenance: {
driver: "file",
},
};
12 changes: 4 additions & 8 deletions src/config/cache.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const path = require("node:path");
const env = require("@simplicityjs/framework/env");
const NodeCache = require( "node-cache" );
const storagePath = require("@simplicityjs/framework/storage-path");
const NodeCache = require("node-cache");

module.exports = {
/*
Expand All @@ -24,7 +24,7 @@ module.exports = {
* there might be other applications using the same cache. For
* To avoid collisions, you may prefix every cache key.
*/
prefix: env("CACHE_KEY_PREFIX", `${env("NAME").toLowerCase().replace(/[\s*,-]+/g, "_")}_cache_`),
prefix: env("CACHE_KEY_PREFIX", `${env("APP_NAME").toLowerCase().replace(/[\s*,-]+/g, "_")}_cache_`),

/*
* Whether to compress the data prior to caching.
Expand All @@ -44,11 +44,7 @@ module.exports = {
stores: {
file: {
driver: "file",
storagePath: path.resolve(
path.dirname(path.dirname(__dirname)),
"storage",
env("CACHE_STORAGE_PATH", ".cache")
),
storagePath: storagePath("framework/cache/data"),
},

memory: {
Expand Down
20 changes: 20 additions & 0 deletions src/config/cors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const env = require("@simplicityjs/framework/env");

const REGEX = /[\s+,;|]+/;
const split = str => str.split(REGEX).map(s => s.trim());


/*
* --------------------------------------------------------------------------
* Cross-Origin Resource Sharing (CORS) Configuration
* --------------------------------------------------------------------------
*
* Here you may configure your settings for cross-origin resource sharing (CORS).
* This determines what cross-origin operations may execute in web browsers.
*/
module.exports = {
allowedHeaders: split(env("ALLOWED_HEADERS")),
allowedMethods: split(env("ALLOWED_METHODS").toUpperCase()),
allowedOrigins: split(env("ALLOWED_ORIGINS")),
credentials: env("USE_CREDENTIALS", false),
};
14 changes: 5 additions & 9 deletions src/config/database.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const path = require("node:path");
const env = require("@simplicityjs/framework/env");
const storagePath = require("@simplicityjs/framework/storage-path");

let sequelizeLoggingOption = false;

Expand Down Expand Up @@ -36,7 +36,7 @@ module.exports = {
port : env("DB_PORT", 27017),
username : env("DB_USERNAME"),
password : env("DB_PASSWORD"),
dbName : env("DB_DBNAME"),
dbName : env("DB_DBNAME", "simplicity_db"),
exitOnConnectFail: true,
},

Expand All @@ -46,20 +46,16 @@ module.exports = {
port : env("DB_PORT", 3006),
username : env("DB_USERNAME"),
password : env("DB_PASSWORD"),
dbName : env("DB_DBNAME"),
dbName : env("DB_DBNAME", "simplicity_db"),
logging : sequelizeLoggingOption,
dbEngine : "mysql",
},

sqlite: {
dbEngine : "sqlite",
dbName : env("DB_DBNAME"),
dbName : env("DB_DBNAME", "simplicity_db"),
logging : sequelizeLoggingOption,
storagePath : path.resolve(
path.dirname(path.dirname(__dirname)),
"storage",
env("DB_STORAGE_PATH", ".sqlite")
),
storagePath : storagePath("app/database"),
},

/*
Expand Down
19 changes: 10 additions & 9 deletions src/config/logging.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
const env = require("@simplicityjs/framework/env");
const is = require("@simplicityjs/framework/lib/is");
const storagePath = require("@simplicityjs/framework/storage-path");


module.exports = {
logExceptions : is.falsy(env("LOG_UNCAUGHT_EXCEPTIONS")) ? false : true,
logRejections : is.falsy(env("LOG_PROMISE_REJECTIONS")) ? false : true,
logToConsole : env("LOG_TO_CONSOLE"),
logToFile : is.falsy(env("LOG_TO_FILE")) ? false : true,
logExceptions : env("LOG_UNCAUGHT_EXCEPTIONS", true),
logRejections : env("LOG_PROMISE_REJECTIONS", true),
logToConsole : env("LOG_TO_CONSOLE", true),
logToFile : env("LOG_TO_FILE", true),

// Required if the `logToFile` option is true.
// The directory (relative to the root directory)
// to place log files if logToFile is enabled.
logDir: env("LOG_DIRECTORY", ".logs"),
/*
* The location to place log files if logToFile is enabled.
* Required if the `logToFile` option is true.
*/
logDir: storagePath("app/logs"),


/*
Expand Down
29 changes: 29 additions & 0 deletions src/config/view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const env = require("@simplicityjs/framework/env");
const resourcePath = require("@simplicityjs/framework/resource-path");


module.exports = {
/*
* -------------------
* View Storage Paths
* -------------------
*
* Here you may specify an array of paths that should be checked
* when loading view templates from disk.
*/
paths: [
resourcePath("views"),
],

/*
* ----------------------
* View Template Engine
* ----------------------
*
* The templating engine to use for your views.
* You can specify any templating engine supported by Express.
* If you specify a template engine different from pug,
* make sure to install the appropriate package for the engine.
*/
engine: env("VIEW_ENGINE", "pug"),
};
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit ea92826

Please sign in to comment.