Jan 7, 2015

Fixing website styles with GreaseMonkey / TamperMonkey

There may be a case that you visit a website with bad-looking design, but you can’t change it. You do not own that website.
Take a look at this www.runscope.com which provide API testing service. Here is the screenshot in Chrome 39 under Windows 7 64-bit. Their default font is “Source Sans Pro” which somehow look bad:

Then with just a little effort: install TamperMonkey then add the script below, the problem is solved.
// ==UserScript==
// @name         Change font style of RunScope
// @namespace    http://diophung.com/
// @version      0.1
// @description  Replace RunScope ugly font
// @author       Dio Phung
// @match        https://www.runscope.com/*
// @grant        none
// ==/UserScript==

//Change the ugly font of RunScope
document.body.style.fontFamily = "PT Sans";



Here is how the new www.runscope.com looks like: the font is much easier to read.

Jan 5, 2015

Script to restore MS SQL database from a physical path

In case we want to automate the deployment of a MS SQL database, here is the script.

It will restore a database from a backup file, and store into the folder of your choice.

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Dio Phung
-- Create date: 05/01/2015
-- Description: Restore DB from a physical path
-- =============================================
USE [master]
IF EXISTS(SELECT * from sys.[objects] AS o WHERE o.[type]='P' AND o.[name]='sp_RestoreDbFromPath')
BEGIN
DROP PROCEDURE sp_RestoreDbFromPath
END

GO

CREATE PROCEDURE sp_RestoreDbFromPath(
@db_name nvarchar(max), -- The name of the db
@db_source_fullpath NVARCHAR(max), -- path to the backup
@db_dest_folder nvarchar(max) -- folder to store db)
AS
BEGIN

SET NOCOUNT ON;
DECLARE @Table TABLE
(
LogicalName VARCHAR(128)
,[PhysicalName] VARCHAR(128)
,[Type] VARCHAR
,[FileGroupName] VARCHAR(128)
,[Size] VARCHAR(128)
,[MaxSize] VARCHAR(128)
,[FileId] VARCHAR(128)
,[CreateLSN] VARCHAR(128)
,[DropLSN] VARCHAR(128)
,[UniqueId] VARCHAR(128)
,[ReadOnlyLSN] VARCHAR(128)
,[ReadWriteLSN] VARCHAR(128)
,[BackupSizeInBytes] VARCHAR(128)
,[SourceBlockSize] VARCHAR(128)
,[FileGroupId] VARCHAR(128)
,[LogGroupGUID] VARCHAR(128)
,[DifferentialBaseLSN] VARCHAR(128)
,[DifferentialBaseGUID] VARCHAR(128)
,[IsReadOnly] VARCHAR(128)
,[IsPresent] VARCHAR(128)
,[TDEThumbprint] VARCHAR(128)
)

DECLARE @LogicalNameData VARCHAR(128),@LogicalNameLog VARCHAR(128)
INSERT INTO @table EXEC ('RESTORE FILELISTONLY FROM DISK=''' + @db_source_fullpath + ''' ')

SET @LogicalNameData = ( SELECT LogicalName FROM @Table WHERE Type = 'D' )
SET @LogicalNameLog = ( SELECT LogicalName FROM @Table WHERE Type = 'L')

DECLARE @RestoreSql nvarchar(max)
SET @RestoreSql = (
'RESTORE DATABASE [' + @db_name +'] FROM DISK = N'''+ @db_source_fullpath+ '''
WITH FILE = 1,
MOVE N'
''+ @LogicalNameData+ ''' TO N'''+ @db_dest_folder + '\'+@db_name+'.mdf'',
MOVE N'
''+ @LogicalNameLog+ ''' TO N'''+ @db_dest_folder + '\'+@db_name+'_log.ldf'',
NOUNLOAD, REPLACE, STATS = 10
'
)

SELECT @RestoreSql
EXEC(@RestoreSql)
END
GO

Dec 22, 2014

CSS specificity rules

If you’re reading this, you’re probably looking for how to know which CSS rules are applied. Rule of thumb:

W3C CSS2 specs : the most specific CSS rule is chosen by UA.

How to know the specificity of a CSS ? Refer to the table below
Excerpts from MDN

The specificity is calculated on the concatenation of the count of each selectors type. It is a weight that is applied to the corresponding matching expression.

Ascending order of specificity

Weight

Selectors

Examples

1

Universal selectors

*

2

Type selectors

div

3

Class selectors

· li.descriptor-link

4

Attributes selectors

li[attr]

5

Pseudo-classes

:nth-of-type

6

ID selectors

#id

7

Inline style

style=”color:green”

Notes:

  • In case of specificity equality, the latest declaration found in the CSS is applied to the element.
  • Proximity of elements in the document tree has no effect on the specificity.

Nov 18, 2014

How to setup a Load Balancer via Application Request Routing (ARR) extension on IIS

This is the walk through on how to set up an Load Balancer on IIS 7.5 using Application Request Routing extension.

Requirements

Steps to create Load Balancer

1. Create a dummy web application to receive incoming requests.
image[12]
Note:
this web app is just a dummy web on port 80, which will acts as request router to our back-end web farm.

2. Configure the dummy web app
In the Create Web Site dialog, use port 80 and the host name as of your domain.
image[17]

The website physical path can just contains a very simple web.config as following:

    <?xml version="1.0" encoding="utf-8" ?>
    <
    configuration
    >
    <
    system.web
    >
    </
    system.web
    >
    </
    configuration>

3. Create the actual web application that serve your request:
In my example, I used port 8080.

Important notes: this port will be the server port while creating server farm.


image

4.Create the server farm
Next step is to create the server farms which will handle the request load balancing. You can choose any name for your server farm.
image


4. Adding servers to server farm
While adding servers to the farm, take note of the httpPort – this MUST be your web application port which you chose in step 3)
image
Once you add the server farm, IIS will ask you to add rules. If you have existing rules, it may popup this message. Just go ahead and click Yes to create the rules. We’ll configure it later.


image


5. Configure the Request Routing rules
Highlight your server farm, then choose Routing Rules
image


Select URL Rewrite under Advance Routing


image


6. Configure Inbound Rule Routing Request:
- Logical Grouping
: Match All


- Conditions:



  • {HTTP_POST} : Matches The Pattern : *localhost* (or your domain name in step 2)
    AND
  • {SERVER_PORT}: Does Not Match The Pattern: *8080* (or your chosen port in step 3)

- Action Type: Route to Server Farm (choose the server farms where you added the web servers in step 4)


image


7. Configure your Load Balance algorithm
Similar to step 5, highlight your server farm and choose Load Balance


image

You will have a list of Load Balance Algorithm to choose from. Depends on your need, you can pick the corresponding algorithm


image



 


Use the list to select the algorithm to use to load balance your server. The available options are: (From MSDN):


  • Weighted round robin – Distributes traffic based on the number of incoming requests and their normalized weight. Each server can receive the same distribution of requests or a custom distribution.
  • Weighted total traffic – Distributes traffic based on the size of the requests and responses in bytes. Requests are routed so that the amount of data is load balanced. In an even distribution, the server with the least amount of data will receive the next request.
  • Least current request – Distributes traffic based on the current number of HTTP requests between ARR and each of the application servers. Requests are routed to the server with the least number of current HTTP requests.
  • Least response time – Distributes traffic based on the fastest response time from the servers, which enables the server to respond most quickly.
  • Server variable hash – Distributes traffic based on a hashed value of a server variable.
  • Query string hash – Distributes traffic based on the hashed value of the query string value. When more than one query string name is specified, the concatenated string of the corresponding query string value is used for the hash.
  • Request hash – Distributes traffic based on the hashed value of the configured server variable or URL. For example, if the server variable is QUERY_STRING, the hashed value is based on the names in the request query string.

8. Test your server farms to confirm it works
If your configurations are right, you should start to see your web applications running. You can also explore more on configure distributed disk-cache, changing server affinity (how server associate with user sessions) and so on.


Conclusion


The key concept is to create a dummy web application, then use ARR and Load Balance to route request to your server farms (based on the Inbound Rules).

ARR is a very good features which make IIS more useable and become more ready for production sites. Try it yourself and have fun Smile !

Oct 8, 2014

Useful services and resources for web developer & webmaster

Here is the list I came across inside a survey of MDN, which I think would be useful to know for every web developer and web master out there.
The list is definitely not complete and there will be more and more useful services. Stay tuned !
References:
  • Docs on GitHub
  • MDN
  • MSDN
  • ReadTheDocs
  • Stack Overflow
  • w3schools
  • WebPlatform.org
Collaboration:
  • GitHub
  • Basecamp
  • JIRA
  • Trello
Code hosting
  • GitHub 
  • Bitbucket
Library monitoring
  • Requires.io
Continous Integration
  • Travis CI
  • Bamboo
  • Circle CI
  • CodeShip
Report test coverage
  • Code Climate
  • Coveralls
Web hosting
  • Amazon/EC2
  • BlueHost
  • DigitalOcean
  • DreamHost
  • Google/AppEngine
  • Heroku
  • HostGator
  • Linode
  • Microsoft/Azure
  • Rackspace M
Site monitoring
  • AppDynamics
  • New Relic
  • Pingdom
  • Scout
Cross-browser test
  • BrowserStack
  • SauceLabs
Accessibility scan
  • WAVE/WebAIM
Traffic monitoring
  • Flurry
  • Google Analytics
  • Piwik
  • MixPanel
  • KISSMetrics
  • Adobe Analytics
Performance monitoring
  • Google Analytics
  • WebPageTest.org
Content Optimization
  • Google Analytics (+Content Experiment)
  • Optimizely