Skip to content

Instantly share code, notes, and snippets.

View midnightcodr's full-sized avatar

Rico Chen midnightcodr

View GitHub Profile
@midnightcodr
midnightcodr / app.js
Created March 12, 2024 16:13
hapijs + pino + kafka
const pino = require('pino')
const pkafka = require('pino-kafka')
// this can be from .env, via JSON.parse
const kafkaLogConfig = {
"clientId":"some-producer1",
"defaultTopic": "some-log-topic",
"brokers":["some-host:9092"]
}
const streams = [{ stream: process.stdout, level: 'info' }]
@midnightcodr
midnightcodr / translate.php
Last active October 21, 2023 04:50
aws translate with php (curl) without sdk
<?php
function aws_translate($text, $sourceLang = 'zh-TW', $targetLang = 'en')
{
$aws_access_key_id = 'key_id';
$aws_secret_access_key = 'key';
// AWS region and Host Name (Host names are different for each AWS region)
// As an example these are set to us-east-1 (US Standard)
$aws_region = 'us-east-1';
$host_name = 'translate.us-east-1.amazonaws.com';
@midnightcodr
midnightcodr / main.js
Created December 9, 2017 18:06
node-fetch with retry
const fetch = require('node-fetch')
const delay = (ms) => {
return new Promise(resolve => {
setTimeout(() => {
resolve()
}, ms)
})
}
const retryFetch = (url, fetchOptions={}, retries=3, retryDelay=1000) => {
@midnightcodr
midnightcodr / write-two-millio-rows-of-csv-data-with-node-cluster.js
Last active September 3, 2023 03:55
How to write two million rows of csv data using faker.js and node cluster
const faker = require('faker')
// const N = 30
const N = 2000000
const fs = require('fs')
const record = () => {
// faker.fake(
// '{{name.lastName}},{{name.lastName}},{{address.city}},{{address.county}},{{address.zipCode}},{{hacker.adjective}}\n'
return [
faker.name.firstName(),
faker.name.lastName(),
@midnightcodr
midnightcodr / README.md
Last active August 15, 2023 20:30
websocket performance test using ws
npm install bluebird ws
node server.js
node client.js
@midnightcodr
midnightcodr / main.js
Created April 11, 2017 02:20
inquirer with async/await
const inquirer = require('inquirer')
const genList = (list) => {
const choices = list.map((item, index) => {
return {
key: index,
name: `${item.id}: ${item.quantity}@${item.price}`,
value: item.id
}
})
@midnightcodr
midnightcodr / sample_contact_us_form_bootstrap_styling.html
Created May 21, 2012 03:36
A sample contact form using twitter bootstrap styling
<form class="well span8">
<div class="row">
<div class="span3">
<label>First Name</label>
<input type="text" class="span3" placeholder="Your First Name">
<label>Last Name</label>
<input type="text" class="span3" placeholder="Your Last Name">
<label>Email Address</label>
<div class="input-prepend">
<span class="add-on"><i class="icon-envelope"></i></span><input type="text" id="inputIcon" class="span2" style="width:233px" placeholder="Your email address">
@midnightcodr
midnightcodr / rust_lambda_helpers.zsh
Created March 14, 2022 03:25
alternative al2build function using cross
RUST_TARGET="aarch64-unknown-linux-gnu"
RUST_VERSION="latest"
PROJECT_NAME=${PWD##*/}
zipRustLambda() {
cp ./target/${RUST_TARGET}/release/${PROJECT_NAME} ./bootstrap && zip lambda.zip bootstrap && rm bootstrap
}
al2build() {
cross build --release --target ${RUST_TARGET}
@midnightcodr
midnightcodr / cat_copycat_with_rust.rs
Created April 9, 2021 01:19
a simple cat implementation with rust, plus some customized error handling
// main.rs
use std::env;
use std::error::Error;
use std::fs;
use std::fmt;
#[derive(Debug)]
struct MyError(String);
const Hapi = require('@hapi/hapi')
;(async () => {
const server = Hapi.server({
port: 3000
})
server.events.on('log', logObject => {
console.log(JSON.stringify(logObject))
})