Add Two-Factor Authentication To A Golang RESTful API

When it comes to authenticating users for making use of your API, it is a good idea to add an extra step beyond standard username and password. This is called two-factor authentication (2FA) and it acts as a second layer of security for users making use of your application.

Not too long ago I had written about adding 2FA to a RESTful API created with Node.js and Express Framework, but what if we wanted to do it in Golang? The logic isn't any different, just a new syntax for a new language.

We're going to see how to add two-factor authentication to a Golang API that makes use of Json Web Tokens (JWT).

Using Network Sockets With The Go Programming Language

A few months back I wrote about using websockets in a Golang application for communication with an Angular client web application.  While very useful and simplistic, in many cases websockets won’t be the means for real-time communication between applications.  It is often easier or better to use standard TCP network sockets as an alternative.  For example, if you’re developing an online video game, it will likely communicate to the server using TCP sockets rather than websockets.

We’re going to see how to create a basic chat application using the Go programming language.  This chat application will have a server for listening and routing client communications and a client for sending and receiving messages from the server.

(more…)

Form Validation In An Angular Web Application With Custom Directives

When creating a web application that accepts user input, it is often a good idea to validate anything the user provides. While you should always validate this information via some backend server-side code, it often provides a good user experience to validate via the frontend as well. By doing form validation on the frontend, you set yourself up for the possibility to catch and display errors before form submission.

If you’ve been keeping up, I once demonstrated how to do form validation in AngularJS via an Ionic Framework application. As of now, AngularJS is ancient technology, so we’re going to see how to do the same with Angular. However, we’ll be seeing from the perspective of a web application, rather than a cross-platform hybrid mobile application.

(more…)

Create A Raspberry Pi Cluster With Docker Swarm And Raspbian Linux

Back when I was first learning about Docker, I had tried to use it on one of my Raspberry Pi devices.  This was when Docker had first become available officially for Raspbian.  When I was playing around with Docker and my Raspberry Pi, I had written a tutorial for deploying containers, or more specifically a Ghost container.  This was a single container on a single device or node.  Since then I started playing around with Docker Swarm and decided to create a Raspberry Pi cluster.

We’re going to see how to take several Raspberry Pi devices, cluster them using Docker Swarm, and deploying containers to Swarm with replica copies.

(more…)

Bulk Remove Unconfirmed Sendy Subscribers After A Certain Number Of Days

You may or may not know this, but I use Sendy to send out my monthly newsletters because it is incredibly cheap in comparison to competitors like Mailchimp. Anyone who has done list building before knows that over time you’re going to end up with a lot of stale subscribers that need to be cleaned in an effort to save space and keep things organized.

My email lists require a secondary opt-in to prevent SPAM subscriptions. The problem with this is I still get a lot of SPAM subscriptions even if they are never included in my newsletters. These subscriptions have no business being in my list, so it is best to remove them after so long.

We’re going to see how to use SQL to remove unconfirmed email subscribers in Sendy after so many days.

(more…)

Implement 2FA With Time-Based One-Time Passwords In A Node.js API

Not too long ago I wrote about authenticating within a Node.js API using Json Web Tokens (JWT). The basis of the example is around authenticating via a username and password and receiving a JWT for every future request against the API. While that example is incredibly useful and follows best practice, it doesn't cover the scenario where you'd like to have a two-factor authentication (2FA) option for your users. In case you're unfamiliar, 2FA is a second layer of protection for accounts made possible by a time-based token generated by a shared secret key.

We're going to see how to add a two-factor authentication option to our Node.js API while continuing to use Json Web Tokens.

Concurrent Golang Applications With Goroutines And Channels

One of the highlights of the Go programming language is its ability to handle concurrency with minimal effort using what are called goroutines and channels.  The beauty here versus other programming languages is that you don’t end up in a callback mess or locking hell.  In fact, you can even create far more goroutines in Go than you can in a language such as Java with the same hardware.

We’re going to see an example where we have an application that starts several worker goroutines and shares a channel for accessing data.

(more…)

Load Balancing A Docker Swarm Cluster With An NGINX Reverse Proxy

If you’ve been working towards containerizing your web applications like I have, you might be at a point where you’re ready to start clustering your containers.  Previously I had written about creating a container cluster with Docker Swarm and using NGINX as a reverse proxy for a few containers.  The catch here is that neither of these previous tutorials were meant to work together.  In the previous example we were using a reverse proxy for containers on a single server.  While Docker Swarm offers it’s own load balancing, you’ll find it makes sense to have NGINX as well because not every container can run on the host as port 80.

We’re going to see how to create two service containers that are replicated across several nodes.  These services will be a simple Apache and NGINX web applications.  Then we’re going to throw an NGINX reverse proxy into the mix that keeps track of the upstream nodes for its own load balancing.

(more…)

Cross Compiling Golang Applications For Use On A Raspberry Pi

I recently invested in yet another Raspberry Pi, this time the new Raspberry Pi Zero W, which has wireless and bluetooth.  I also made a leap and bought the camera module with it because the new official case by Raspberry Pi has a camera attachment.  Probably the most popular development technology for Raspberry Pi is Python, but I am not a fan at all.  Instead, I’ve been doing a lot of Go development and figured that would be my best bet when it comes to developing a camera application for the Raspberry Pi.  The problem with this is that if I were to compile a Go application on the Raspberry Pi Zero itself, it would probably take ten years (I joke).

Cross compiling is a thing and we’re going to see how to do this via a different operating system and architecture, yet have it be compatible on the Raspberry Pi.

(more…)

Using A SQLite Database For Local Data In A Golang Application

When developing an application with the Go programming language, you might find yourself needing to save data locally.  If you’ve been keeping up you’ll remember that I’ve written about storing data remotely with Golang in a Couchbase NoSQL database, but never anything locally.  Probably the easiest way to store data locally is with a SQLite database as it is a solid technology that has been around for a while.

We’re going to see how to use a SQLite database in our Golang application to read and write local data.

(more…)