Skip to content

Instantly share code, notes, and snippets.

@aleung
aleung / HttpServer.java
Created November 8, 2011 13:47
Tiny HTTP Server by Java, only supports GET method.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
@aleung
aleung / applicationContext.xml
Created June 15, 2012 04:00
Spring bean reference injection definition by external configuration.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
default-autowire="byType">
<context:property-placeholder location="file:bin/leoliang/spring/test1/bean.properties"/>
@aleung
aleung / ipaddr.es
Last active November 5, 2015 03:43
Get local IP address of specific NIC in node.js
var _ = require('lodash');
var os = require('os');
function getIpAddr(nicName) {
let ifaces = os.networkInterfaces();
let iface = _.find(ifaces[nicName], iface => { return iface.family === 'IPv4' });
return _.result(iface, 'address');
}
module.exports = _.memoize(getIpAddr);
@aleung
aleung / remove_conflict_non_unique_snapshots.rb
Created March 28, 2013 04:05
I have Maven (Artifactory) repository which was configured to store non-unique snapshots. When the configure was changed to store unique (with time-stamp) snapshots, if a new snapshot is deployed, there will be both unique snapshot and non-unique snapshot artifact of same version exists. If it happens, the snapshot artifact will be unable to dow…
#!/bin/env ruby
# --- Configuration ---------------------------------
$age_days = 3
# The repository to be cleaned.
$repo = 'repo-name'
$user = 'repo-admin'
@aleung
aleung / Scheduler
Created May 27, 2013 10:48
A simple Java scheduler, which execute scheduled tasks in current thread.
package leoliang.common;
import com.google.common.base.Preconditions;
/**
* Usage example: Run 15 times in every 250ms, begins at 5 seconds later.
*
* <pre>
Scheduler.every(250, Scheduler.TimeUnit.MILLISECOND)
.beginsAt(System.currentTimeMillis() + 5000)
@aleung
aleung / mergelog.js
Created November 2, 2016 11:30
Merge consul agent logs which are fetched by Ansible
#!/usr/bin/env node
"use strict";
const fs = require('fs');
const readline = require('readline');
const logLineRegexp = /\s+(.+?)\[(.+?)\](.*)/;
function processFile(file, host) {
@aleung
aleung / du.sh
Last active March 13, 2017 03:34
One line shell scripts
# size of immediate sub-folders
du -d1 -BM | sort -g
@aleung
aleung / clean_aged_artifacts.rb
Last active April 24, 2017 18:07
Clean up unused (long time no download) artifacts from Artifactory repository. See: http://aleung.github.com/blog/2013/03/22/clean-aged-artifacts-from-artifactory/
#!/bin/env ruby
# --- Configuration ---------------------------------
# Remove artifacts which were created before $age_days ago and haven't been downloaded in recent $age_days.
$age_days = 730
# The repository to be cleaned.
$repo = 'repository-key'
@aleung
aleung / user.js
Last active June 8, 2017 09:26
User script to slideshow a serial of webpages in loop. https://greasyfork.org/en/scripts/30426-webpages-slideshow
// ==UserScript==
// @name Webpages slideshow
// @namespace https://github.com/aleung/
// @version 1.1.0
// @description Loop display a serial of webpages. Display time for each page can be set.
// @author Leo Liang
// @license MIT License
// @include *
// @noframes
// @require https://openuserjs.org/src/libs/sizzle/GM_config.js
@aleung
aleung / console-output
Created June 13, 2017 08:06
Test of [async-local-storage](https://github.com/vicanso/async-local-storage) on Node.js 8.x
Trace of ID 1
1497340609369: Received request. ID: 1
1497340609369: Step 1 1
1497340610599: Step 2 1
1497340610602: Response sent 1
-----
Trace of ID 2
1497340606575: Received request. ID: 2
1497340606575: Step 1 2
1497340612700: Step 2 2