Topics

Solutions

Easy solutions and ideas found after long googling or hard coding

AWS Client v1.0

AWS Client is an open source JavaFX GUI desktop application for Amazon EC2, providing monitoring and filtering controls over your EC2 server instances in different regions. It also allows you to easily interact with your instances via your SSH client. More functionality will be added soon so follow my blog and watch AWS Client code on GitHub

Screen Shot 2016-01-28 at 2.01.59 AM



Download :
Mac OSX
Windows
Source Code

Replacing URLs in HTML and JS with mod_proxy on apache 2.2

Simple way to replace URLs for proxied websites using mod filter and substitute

<VirtualHost *:80 >
ProxyPreserveHost On
ServerName proxy-test.example.com
FilterDeclare MYFILTER
FilterProvider MYFILTER SUBSTITUTE resp=Content-Type $text/
FilterProvider MYFILTER SUBSTITUTE resp=Content-Type $/xml
FilterProvider MYFILTER SUBSTITUTE resp=Content-Type $/json
FilterProvider MYFILTER SUBSTITUTE resp=Content-Type $/javascript
<Location />
#disable gzip
RequestHeader unset Accept-Encoding
FilterChain MYFILTER
Substitute "s!(images|static|test).example.com!proxy-$1.example.com!i"
</Location>
ProxyPass / http://test.example.com/
ProxyPassReverse / http://test.example.com/
</VirtualHost >
Modify on Gist

Warming up WordPress cache, HHVM and testing blog pages

If you are minifying scripts and css files using a caching plugin or using FastCGI cache then you might need to warmup your blog after purging your cache. This is a simple warm up cli script for WordPress to initiate cache or HHVM HHBC and making sure all pages/posts do not have errors. Additionally the script creates a urllist.txt file that you can use with siege to test load your server.

#!/usr/bin/env php
< ?php

if (PHP_SAPI != 'cli') {
    header('Status: 403 Forbidden');
    header('HTTP/1.1 403 Forbidden', true, 403);
    exit();
}


$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
define('BASE_PATH', "");
define('WP_USE_THEMES', false);
global $wp, $wp_query, $wp_the_query, $wp_rewrite, $wp_did_header;
require(BASE_PATH . 'wp-load.php');

//Disable cache
ob_end_clean();

$homeUrl = home_url() . "/";
$urlList = [$homeUrl];


$the_query = new WP_Query(['post_type' => 'any', 'post_status' => 'publish']);
//add homepage pages
warmup_pages_links($the_query->max_num_pages, $homeUrl);

//get all posts
$the_query = new WP_Query(['post_type' => 'any', 'posts_per_page' => '-1', 'post_status' => 'publish']);
$the_query->the_post();
while ($the_query->have_posts()) {
    $urlList[] = get_permalink();
    $the_query->the_post();
}

$categories = get_categories();
foreach ($categories as $category) {
    $catUrl = get_category_link($category->term_id);
    $urlList[] = $catUrl;
    $the_query = new WP_Query(['cat' => $category->term_id]);
    warmup_pages_links($the_query->max_num_pages, $catUrl);
}

$tags = get_tags();
foreach ($tags as $tag) {
    $tagUrl = get_tag_link($tag->term_id);
    $urlList[] = $tagUrl;
    $the_query = new WP_Query(['tag_id' => $tag->term_id]);
    warmup_pages_links($the_query->max_num_pages, $tagUrl);
}

$authors = get_users(array('who' => 'authors'));
foreach ($authors as $author) {
    $authorUrl = get_author_posts_url(false, $author->user_nicename);
    $urlList[] = $authorUrl;
    $the_query = new WP_Query(['author_id' => $author->term_id]);
    warmup_pages_links($the_query->max_num_pages, $authorUrl);
}

//test search page
$urlList[] = get_search_link('bala la lam');

file_put_contents('urllist.txt', implode("\n", $urlList));

echo "Found " . count($urlList) . " URLS and saved to urlList.txt \n";
echo "Testing URLs \n";
foreach ($urlList as $url) {
    warmup_getUrl($url);
}
echo "\nWarmup done \n";

function warmup_getUrl($url)
{
    if ($contents = wp_remote_get($url, ['timeout' => 15])) {
        if (is_wp_error($contents) || (isset($contents['response']['code']) && $contents['response']['code'] != 200)) {
            $subject = "[WP " . get_bloginfo('name') . "] Error on {$url} ";
            $error = is_wp_error($contents) ? $contents->get_error_message() : print_r($contents, true);
            wp_mail(get_bloginfo('admin_email'), $subject, $error);
            die("\n{$subject} :" . $error);
        }
        echo ".";
    }
    return $contents;
}

function warmup_pages_links($total, $url)
{
    for ($i = 2; $i < = $total; $i++) {
        $GLOBALS['urlList'][] = "{$url}page/{$i}/";
    }
}

// siege -c200 -d1 -r30 -f urllist.txt

Custom validation messages for sails js

//in api/models/User.js
function validationError(invalidAttributes, status, message) {
  var WLValidationError = require('../../node_modules/sails/node_modules/waterline/lib/waterline/error/WLValidationError.js');
  return new WLValidationError({
      invalidAttributes: invalidAttributes,
      status: status,
      message: message
    }
  );
}
var User = {
  attributes: {
    //...
  },
  ownValidate:: function (values, update, cb) {
    //example of not allowed param on update
    //if it is an update then do not allow email param
    if (update && values.email) {
      return cb(validationError({
        email: [
          {
            message: 'Email is not allowed for updates.'
          }
        ]
      }, 400 /*status*/));
    }
    sails.models['user'].findOne(values.email).exec(function (err, user) {
      if (err) return cb(err);
      if (user) {
        return cb(validationError({
          email: [
            {
              value: values.email,
              rule: 'E_UNIQUE'
              /* unique validation message is left for the default one here */
            }
          ]
        }, 409));
      }
    });
  },
  beforeCreate: function (values, cb) {
    return sails.models['user'].ownValidate(values, false, cb);
  },
  beforeUpdate: function (values, cb) {
    return sails.models['user'].ownValidate(values, true, cb);
  }
}

For blueprint custom messages validation