Skip to content

Commit

Permalink
Merge pull request #10 from OrcaPracticas/add/tools
Browse files Browse the repository at this point in the history
⚙️ Integración de Tools
  • Loading branch information
konami12 authored Jun 21, 2020
2 parents 7e589f5 + 22e88b1 commit d410041
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 4 deletions.
1 change: 1 addition & 0 deletions .babelrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module.exports = {
presets: ["@babel/preset-env"],
plugins: [
"inline-json-import",
"@babel/plugin-proposal-class-properties",
[
"module-resolver",
{
Expand Down
24 changes: 20 additions & 4 deletions app/src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import Helmet, { frameguard } from "helmet";
import Path from "path";

import ApiRouter from "./router";

import Tools from "Tools/Helpers";
// ======================== CONSTANTES ======================== //

const APP_PORT = process.env.PORT;
const ROOT_PATH = Path.join(__dirname, "../../");
const Helpers = Tools.instance;
const Server = Express();

// ==================== COMNFIGURACIONES ==================== //
Expand All @@ -29,18 +30,33 @@ Server.use(BodyParser.json());

// ==================== ARCHIVOS STATICOS ==================== //

Server.use("/", Statics(`${ROOT_PATH}/public/`));
Server.use("/", Statics(`${ROOT_PATH}/public/`, {
setHeaders(response) {
Helpers.getTimeToLive(response, 31536000, "assets");
}
}));

// ===================== MANEJO DE RUTAS ====================== //

Server.use(ApiRouter(Router));

// ====================== ESTABLECIENDO CONEXIÓN ====================== //

/**
* Inicializacion del servidor.
*
* @param {Number} PORT Puerto por el que estara escuhcando el server.
* @param {Function} Callback Permite identificar el estado del proceso.
*
* return void.
*/
Server.listen(APP_PORT, (error) => {
Helpers.messages("Iniciando el Servidor", "i");
if (error) {
console.log(error);
Helpers.messages("Problemas al inicar el servidor", "e");
console.log(error); // eslint-disable-line
process.exit(1);
} else {
console.log(`Servidro listo ${APP_PORT}`);
Helpers.messages(`🚀 Servidor listo en el puerto ${APP_PORT}`, "s");
}
});
117 changes: 117 additions & 0 deletions app/src/tools/Helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import Colors from "cli-color";

class Helper {
// Propiedad privada
static #instanceClass = null;

/**
* Permite limpiar y formatear una cadena
*
* @param {string} string Cadena a limpiar.
* @param {string} union Cararter con el que sera unida.
*
* @retur {string}
*/
formatString(string = "", union = "") {
let ascii = 0;
let char = "";
let newString = "";
const STRING = string.toLowerCase();
const SPECIAL = {
á: "a",
é: "e",
í: "i",
ó: "o",
ú: "u",
ñ: "n",
"♀": "f",
"♂": "m",
};

for (let i = 0; i < STRING.length; i += 1) {
char = STRING[i];
char = (typeof SPECIAL[char] !== "undefined") ? SPECIAL[char] : char;
if (typeof char !== "function") {
ascii = char.charCodeAt();
newString += (ascii === 32 || (ascii >= 48 && ascii <= 57) || (ascii >= 97 && ascii <= 122)) ? char : "";
}
}
newString = newString.split(" ").filter(Boolean);
return newString.join(union);
}

/**
* Permite el seteo de las metas de cache.
*
* @param {express} response Respuesta de express.
* @param {Number} ttl Tiempo de vida de elemento.
* @param {String} type Tipo de elemento.
*
* @return {void}
*/
getTimeToLive(response, ttl = 100, type = "generic") {
const EDGE_VALUE = `API_${type}`.toUpperCase();
response.setHeader("Edge-Control", `!no-store, cache-maxage=${ttl}s`);
response.setHeader("Edge-Cache-Tag", EDGE_VALUE);
response.setHeader("Cache-Control", `max-age=${ttl}`);
}

/**
* Permite el envio de mensajes
*
* @param String text Mensaje que se desea mostrar.
* @param String type (Optional) Tipo de mensaje
*
* e = error
* s = succses
* w = warning
* t = tracking
* default = info
*
* @return void.
*/
messages(text, type = "") {
let log = "";
let msg = "";
let auxText = typeof text === "object" ? JSON.stringify(text) : text;
const lon = (auxText.length < 90) ? (100 - auxText.length) : 0;
switch (type) {
case "e":
log = Colors.xterm(15).bgXterm(124).bold;
msg = " 🚨 ERROR ";
break;
case "s":
log = Colors.xterm(15).bgXterm(34).bold;
msg = " 🙌 SUCCESS ";
break;
case "w":
log = Colors.xterm(232).bgXterm(214).bold;
msg = " ⚠️ WARNING ";
break;
case "t":
log = Colors.xterm(15).bgXterm(90).bold;
msg = " 🔎 TRACKING ";
break;
default:
log = Colors.xterm(15).bgXterm(12).bold;
msg = " ℹ️ INFO ";
break;
}
for (let i = 0; i < lon; i += 1) { auxText += " "; }
console.log(log(` [${msg}] => ${auxText}`));
}

/**
* Permite conseguir una instancia de la clase.
*
* @returns {Helper}
*/
static get instance() {
if (!Helper.#instanceClass) {
Helper.#instanceClass = new Helper();
}
return Helper.#instanceClass;
}
}

export default Helper;
1 change: 1 addition & 0 deletions app/src/tools/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "./Helpers";
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@babel/cli": "7.10.3",
"@babel/core": "7.10.3",
"@babel/node": "7.10.3",
"@babel/plugin-proposal-class-properties": "^7.10.1",
"@babel/preset-env": "7.10.3",
"@babel/register": "7.10.3",
"babel-eslint": "10.1.0",
Expand Down

0 comments on commit d410041

Please sign in to comment.