Tuesday, December 15, 2015

How to Set Up a Rails App with CenturyLink’s AppFog & Bare Metal

This article was sponsored by AppFog. Thank you for supporting the sponsors who make SitePoint possible.

There are many cloud platform providers out there. One of the most complete and vast offerings is the CenturyLink Cloud platform. For scope, just take a look at the menu on the CenturyLink site:

Century Link menu

CenturyLink constantly looks for new services to add to the platform, and recently released Bare Metal servers. Previously, I wrote a tutorial on provisioning a Bare Metal server and deploying a simple Rails app on that server. Bare Metal servers fit into the gap between shared virtual machines and dedicated servers. They have the isolation of a physical box, but scale and provision more like a shared VM. I recommend you read that article to get an idea of how Bare Metal servers fit into the picture.

In this article, I am going to take the application from the previous article to the next architectural step, namely, separate repository and application servers. CenturyLink offers AppFog, which is a Platform as a Service (PaaS) like Heroku, so it makes sense to move the Rails application there. The application database will be moved from SQLite to PostgreSQL. The PostgreSQL instance will be created on a Bare Metal server, so this application is going to grow up, just a bit.

Here's a simple image that shows the new architecture:

AppFog new architecture

CenturyLink Setup

Refer to the first post to sign up with CenturyLink and make sure Bare Metal is enabled.

Bare Metal Database Server: PostgreSQL

If you've followed the first tutorial, then you know how to deploy a Bare Metal server. Here, I’m going to deploy a new one for our PostgreSQL database. I want to add the new server to the existing VA1 - US East deployment (created in the first article), so I click the hamburger icon for that region:

PostgreSQL menu

Which then shows:

PostgreSQL menu

From this point forward, it's really just a matter of picking some basic options:

  • Choose 'Bare Metal' for the server type
  • I went with the smallest configuration (4GB)
  • For the OS, I chose Ubuntu 14

It takes a few minutes for the provisioning to complete. Again, this is the same process described in the first tutorial, so refer to it if needed.
Looking at the dashboard, I now have a new Bare Metal server called VA1SPGGSPPG202. Your server will have a different name. Let's get PostgreSQL installed on it. We'll need to SSH into the box for the installation, which means assigning a public IP address and opening the appropriate ports. The default port for SSH is 22, as we all know. Remember that PostgreSQL is going to be on this machine, which uses port 5432 by default.

PostgreSQL set public IP address

The provisioning of the public IP takes a few minutes. Adding a public IP is not a security best practice. Do not do this in a real production scenario. You'd at least want to restrict the source IP to your application servers for this box. More likely, a private virtual network is merited where only your servers can see your servers and the application server is the single point of entry. Even when following tutorials, you should get in the habit of using the VPN that is provided for you when you create the server. For the sake of keeping the tutorial succinct, I’ll refer you to CenturyLink’s instructions on setting up a client VPN.

CenturyLink offers many options for securing network access, so check the documentation for more or contact CenturyLink support for assistance with these options.

Once the public IP address has been added, SSH into the box using the username and password (available on the dashboard entry of the server).


ssh root@<your public ip>

root@<public ip>'s password:
Welcome to Ubuntu 14.04.3 LTS (GNU/Linux 3.13.0-68-generic x86_64)

 * Documentation:  http://ift.tt/ABdZxn
 *
 * The programs included with the Ubuntu system are free software;
 * the exact distribution terms for each program are described in the
 * individual files in /usr/share/doc/*/copyright.
 *
 * Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
 * applicable law.

root@VA1SPGGSPPG202:~#

Excellent, we're in. Installing PostgreSQL really is pretty simple:


apt-get update
...lots of updates...

sudo apt-get install postgresql postgresql-contrib
....lots of installation fodder...

You can verify that PostgreSQL is working by switching to the postgres user and running psql:


root@VA1SPGGSPPG202:~# sudo -i -u postgres
postgres@VA1SPGGSPPG202:~$ psql
psql (9.3.10)
Type "help" for help.

postgres=#

Yup, all good. However, the postgres role is associated with the postgres Linux user (this is called "peer authentication") and is somewhat privileged. As such, let's create a role that we can use for our application. Exit psql by typing \q so you're back at the Ubuntu command prompt and then type:

vi /etc/postgresql/9.3/main/pg_hba.conf

This opens the file in vi. By default, PostgreSQL is configured to use Peer Authentication only, but we want to use Password Authentication. Find the following line:

local  all  postgres  peer

Change it to:

local  all  postgres  md5

Also, add the following line to the file:

host  all  all  all  md5

This enables PostgreSQL to accept incoming connections from remote hosts.

Save and exit the file (:wq).

Next, PostgreSQL needs to be configured to listen for remote connections. This is done in the main configuration file:

vi /etc/postgresql/9.3/main/postgresql.conf

Find the line with listen_addresses, uncomment it, and change it to:

listen_addresses='*'

Note: These are not recommended security practices for a production PostgreSQL instance. This is intended purely as an illustration of a dev PostgreSQL environment.

Back at the command prompt:


postgres@VA1SPGGSPPG202:~$ createuser --interactive
Enter name of role to add: thingsuser
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) y
Shall the new role be allowed to create more new roles? (y/n) n
Password: 
postgres@VA1SPGGSPPG202:~$

You should now be able to login using that user and psql:


psql -U thingsuser -d postgres
Password for user thingsuser:
psql (9.3.10)
Type "help" for help.

postgres=>

We’re ready to set up our Rails application on AppFog.

Continue reading %How to Set Up a Rails App with CenturyLink’s AppFog & Bare Metal%


by Glenn Goodrich via SitePoint

Google Maps Made Easy with GMaps.js

Google Maps has proved to be a highly successful Google service, incorporating a range of invaluable tools such as Street View, Route Planning and Google Traffic. Many companies and organizations rely on Google Maps to provide their services – and it’s thanks to the Google Maps API that they’re able to do so. The Google […]

Continue reading %Google Maps Made Easy with GMaps.js%


by Shivam Mamgain via SitePoint

Microm – Convert Browser Microphone to MP3 in JavaScript

Microm is a beautiful library to convert browser microphone to mp3 in Javascript.

Microm goal it's to make trivial to play and convert audio in the browser.


by via jQuery-Plugins.net RSS Feed

A Rundown of the Best New Features in ES2015

This article is part of a web development series from Microsoft. Thank you for supporting the partners who make SitePoint possible.

The arrow and spread operators, the new class syntax, the let and const keywords are very visible additions to JavaScript, and their usage has spread like wildfire. These new language features reduce the amount of boilerplate code developers write, simplify JavaScript code and make developer intentions clearer. And browser support is particularly good including most modern browsers including Microsoft Edge. This article will give you a rundown of the best new features in ES2015.

ECMAScript is the scripting language standardized by ECMA International – a standards setting body - as ECMA-262. This organization has the very difficult task of setting the standards for JavaScript implementations. The ECMAScript standard has gone through several editions. A new version of the standard has been published this summer. You may have heard it called ECMAScript 6 (ES6), ECMAScript 2015(ES2015), Harmony or even JavaScript 2015. I will use the term ES2015 for the rest of this article.

The new language features introduced in ES2015 improve JavaScript by making it simpler, more readable and less error-prone. This article will introduce block-level scoping and the new let and const keywords which simplifies scoping, making code easier to understand, the new class syntax that reduces the amount of boilerplate code you have to write to create object-oriented style inheritance, and the new arrow and spread operators which reduce the amount of keystrokes developers have to do.

Block Scoping - Let's Talk About Scoping Prior to ES2015

New and even seasoned JavaScript developers have fallen prey to the scoping system in JavaScript. Even expert developers coming from other languages have also fallen prey. Until only very recently, JavaScript variable scoping could only have been bound at the function-level. In other words, every time you declared a variable, whether it was declared in an if statement or a for-loop, the variable scope was set to the function-level.


var foo = 'Canadian Developer Connection';
console.log(foo); // Prints 'Canadian Developer Connection'
if(true){
  var foo = 'BAR';
  console.log(foo); // Prints 'BAR'
}
console.log(foo); // Prints 'BAR'

Function-level variable scoping was particularly troublesome if you have picked up the habit of reusing variable names. If you were a Java, C# or C++ developer, this was probably already ingrained in your coding habits especially when it came to placeholder variables such as the index variable in for-loops or the name of an object re-instantiated in different parts of the function.

JavaScript hoists variables to the top of the function no matter where they were declared in the function enforcing a function-level variable scoping. This phenomenon is called “hoisting” and is frequently an interview question for candidates for front-end web developer positions. The following code:


var foo = 'JS';
if(!bar){
  console.log(foo +' '+ bar);
  // Prints 'JS undefined'
}
var bar = '2015';
console.log(foo +' '+ bar);
// Prints ‘JS 2015'

is really executed like this:


var foo, bar;
foo = 'JS';
if(!bar){
  console.log(foo +' '+ bar);
  // Prints 'JS undefined'
}
bar = '2015';
console.log(foo +' '+ bar);
// Prints 'JS 2015'

ES2015 introduces block-level scoping using two new syntax keywords; let & const.

Block Scoping - Introducing let & const

ES2015 allows you to scope at the block-level using two new keywords. Variables declared in different blocks e.g. if blocks, for-loop blocks, while-loop blocks, anything with a curly braces {} surrounding them, even naked curly braces {}, will be bound only to that scope. To declare a variable as scoped to the block-level, you use the let keyword instead of var.


letfoo = 'JS';
console.log(foo); // Prints 'JS'
if(true){
  letfoo = 'BAR';
  console.log(foo); // Prints 'BAR'
}
console.log(foo); // Prints 'JS'

ES2015 also introduces a construct for single-assignment variables scoped to the block-level. This is done through the new const keyword. Variables declared with const can only be assigned once and is very useful for declaring “magic” variables such as Pi or the Boltzsmann constant or other variables that should never be modified past the initial assignment.


const foo = 'JS';
console.log(foo); // Prints 'JS'
if(true){
  letfoo = 'BAR';
  console.log(foo); // Prints 'BAR'
}
// foo = 'BAR';
// The above line causes an error due to the variable being const.
console.log(foo); // Prints 'JS'

Introducing let and const in ES2015 allows developers to be more explicit in declaring variables and reduces the potential for scoping errors. In most cases, especially if you were already aware of ‘hoisting’ and function-level scoping, you can replace your usage of var with let without any other significant change to your code. You’ll save yourself serious headaches and make it easier to bring in developers who know other languages.

Continue reading %A Rundown of the Best New Features in ES2015%


by Rami Sayar via SitePoint

Building Custom Web Components with X-Tag

After Google and Mozilla's solutions for web components, it is now Microsoft’s turn to enter this space with their public release of the X-Tag library. The official website defines it as,

X-Tag is a Microsoft supported, open source, JavaScript library that wraps the W3C standard Web Components family of APIs to provide a compact, feature-rich interface for rapid component development. While X-Tag offers feature hooks for all Web Component APIs (Custom Elements, Shadow DOM, Templates, and HTML Imports), it only requires Custom Element support to operate. In the absence of native Custom Element support, X-Tag uses a set of polyfills shared with Google’s Polymer framework.

In other words, X-Tag is to Microsoft what Polymer is to Google. The only thing common to both of them is the underlying polyfill that enables support for web components in non-supporting browsers.

How is X-Tag Different than Polymer?

Polymer combines all four web component technologies, namely, HTML Imports, Custom Elements, Shadow DOM and HTML Templates into a single package. It provides the developer with an easy-to-use API for building custom web components. On the other hand, X-Tag only provides the Custom Element API to build custom web components. Of course, you could also still use other web component APIs in conjunction with the X-Tag library.

Isn’t X-Tag, a Mozilla Project?

Yes it was, but it now no longer is. After some digging, I managed to find out that the original developer of the X-Tag project, Daniel used to work at Mozilla, when he created the library. But since then, he has moved to Microsoft and continued the project. Moreover, he was the sole contributor to the library with help from an ex-Mozillian. Hence, it is now a Microsoft-backed project founded by an ex-Mozillian.

Getting Started with X-Tag

In this article, we will build a custom element using the X-Tag library to embed a Google map street view using the following markup:

[code language="html"]
<google-map latitude="40.7553231" longitude="-73.9752458"></google-map>
[/code]

The <google-map> custom element will have two attributes, latitude and longitude, to specify the coordinates of a location. Since these attributes are optional, we will also assign default values for each one of them in case the developer fails to define them in the markup.

Let’s start by including the X-Tag JavaScript library in the <head> of our document.

[code language="html"]
<!DOCTYPE html>
<html>
<head>
<!-- X-Tag library including the polyfill -->
<script src="http://ift.tt/1P5w6Fl;
</head>
<body>
<!-- Custom element markup will appear here -->

<script>
<!-- The behavior of the custom element will be defined here -->
</script>
</body>
</html>
[/code]

It is important to include the X-Tag library in the <head> of the document. This is so that it is downloaded and parsed completely before the rendering engine encounters the custom element markup that we will use inside the <body>.

Defining the Custom Element

Unlike Polymer, the process of creating a custom element with X-Tag is completely JS driven. The X-Tag library provides a bunch of useful methods, callbacks and properties to define a custom behavior for our element. A typical skeleton of creating a custom element with X-Tag looks like the following,

[code language="html"]
<script>
xtag.register('google-map', {

content: '',

lifecycle: {
created : function(){
/* Called when the custom element is created */
},
inserted : function(){
/* Called when the custom element is inserted into the DOM */
},
removed : function(){
/* Called when the custom element is removed from the DOM */
},
attributeChanged: function(attrName, oldValue, newValue){
/* Called when the attribute of the custom element is changed */
}
},

accessors : {},
methods : {},
events : {}
});
</script>
[/code]

Continue reading %Building Custom Web Components with X-Tag%


by Pankaj Parashar via SitePoint

Pay What You Want for 12 Microsoft Office Courses

Think keeping a candy jar on your desk will make you the office MVP? Well, it will. But there’s another way, too—mastering Microsoft office with this 12-course bundle. Pay what you want for it at SitePoint Shop. This bundle contains $990 worth of Microsoft Office courses that’ll help you work faster and smarter. You’ll learn […]

Continue reading %Pay What You Want for 12 Microsoft Office Courses%


by SitePoint Offers via SitePoint

Using Socket.IO and Cordova to Create a Real Time Chat App

In this tutorial we’re going to build a chat application using Cordova and Socket.io. To make things easier we’re using the Ionic framework. I’m going to assume that you have already setup all the relevant SDK’s in your machine. And have installed Cordova and Ionic as we won’t be going through those steps in this tutorial.

Continue reading %Using Socket.IO and Cordova to Create a Real Time Chat App%


by Wern Ancheta via SitePoint