Skip to content

Instantly share code, notes, and snippets.

View andreciornavei's full-sized avatar
🔥
Working on fire.

André Ciornavei andreciornavei

🔥
Working on fire.
View GitHub Profile
@andreciornavei
andreciornavei / http_server.ts
Created May 14, 2022 17:23
NodeJS http ROUTE RESOLVER
import * as http from "http"
const PORT = 1337
type Request = http.IncomingMessage & {
params?: Map<String, String>
}
const routeResolver = async (routes: Map<string, Function> = new Map(), req: http.IncomingMessage, res: http.ServerResponse) => {
const requestPaths: string[] = req.url.split("/").filter(path => path.length > 0)
@andreciornavei
andreciornavei / auth_gcp.sh
Created May 13, 2022 12:22
Authenticate with GCP on shell through service account in Base64 variable.
#!/bin/bash
# TO CONVERT A SERVICEACCOUNT FILE IN BASE64 AND SAVE IT TO A VARIABLE
# RUN: "CI_GCLOUD_SERVICE_ACCOUNT_BASE64=$(base64 ./xapps-terraform.json)"
# TO CONVERT A SERVICEACCOUNT BASE64 TO FILE AND USE IT WITH CLI SDK
echo $CI_GCLOUD_SERVICE_ACCOUNT_BASE64 | base64 -d > ./serviceaccount.json
wget https://dl.google.com/dl/cloudsdk/release/google-cloud-sdk.tar.gz
@andreciornavei
andreciornavei / index.js
Last active May 14, 2022 19:58
Resilient NodeJS Server
import * as os from "os"
import * as cluster from "cluster"
import { bootstrap } from "./server"
const numCpus = os.cpus().length
const handleFork = () => {
const worker = cluster.fork()
worker.addListener("exit", (code) => {
if (code !== 0) handleFork()
})
@andreciornavei
andreciornavei / formatter.js
Created May 5, 2022 10:16
Formatting with Intl on Javascript
const language = "pt-BR"
// formatting lists
const vehicles = ["Car", "Bike", "Airplan"]
new Intl.ListFormat(language, { style: "long", type: "conjunction" }).format(vehicles)
// formatting units
const kmTraveled = 10000
new Intl.NumberFormat(language, { style: "unit", unit: "kilometer" }).format(kmTraveled)
@andreciornavei
andreciornavei / file.js
Created May 5, 2022 10:12
Write and Read Json File on Javascript
import { writeFile, readFile } from "fs/promises"
export const save = async (data) => {
const currentData = await read("./../database.json")
currentData.push(data)
await writeFile(databaseFile, JSON.stringify(currentData))
}
export const read = async (filename) => {
const { pathname: databaseFile } = new URL(filename, import.meta.url)
@andreciornavei
andreciornavei / index.js
Created March 16, 2022 13:33
AWS S3 STREAM
// IMPORT DEPENDENCIES
const stream = require("stream")
const AWS = require('aws-sdk');
const fs = require('fs');
const s3 = new AWS.S3();
// DOWNLOAD STREAM EXAMPLE
const s3download = (bucketName, keyName, localDest) => {
@andreciornavei
andreciornavei / docker-images.sh
Last active March 31, 2022 10:37
Examples of docker images installation
#instala uma imagem mysql na versão 5.7 atribuindo um volume existente
docker run -d --name mysql --restart always -p 3306:3306 -v mysql-volume:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=mysql-secret-pw mysql:5.7 --default-authentication-plugin "mysql_native_password"
#instala uma imagem redis na versão alpine atribuindo um volume existente
docker run -d --name redis --restart always -p 6379:6379 -v redis-volume:/var/lib/redis redis:alpine redis-server --requirepass "redis-secret-pw"
@andreciornavei
andreciornavei / deploy_docker_aws.sh
Created September 17, 2021 22:04
Deploy to AWS with docker
# 1) check if is everything ok on local machine using docker-compose
docker-compose up
# 2) run aws configure and set your credentials
aws configute
# or import your csv configuration
aws configure import --csv file://$PDW/aws-profile-credentials.csv
# 3) check if your credentials is currecly imported and configured
aws configure list
@andreciornavei
andreciornavei / strapi_buffer_upload.js
Created September 8, 2021 23:21
Strapi upload plugin from buffer
// The returned ID can be passed on image field on model
const uploadFileBinary = async (binary, uuid) => {
const fileInfo = { name: uuid }
const metas = {}
const { optimize } = strapi.plugins.upload.services['image-manipulation'];
const { buffer, info } = await optimize(binary);
const formattedFile = await strapi.plugins.upload.services.upload.formatFileInfo(
{
filename: uuid + '.png',
@andreciornavei
andreciornavei / equatable_entity.dart
Created October 25, 2020 16:35
Flutter / Equatable / Entity + Model Sample
import 'package:equatable/equatable.dart';
import 'package:meta/meta.dart';
class UserEntity extends Equatable {
final String id;
final String username;
final String password;
final List<LinkEntity> links;
UserEntity({
@required this.id,