Showing posts with label Go. Show all posts
Showing posts with label Go. Show all posts

Tuesday, 17 June 2014

A Dummy REST API with Go Lang

Sometimes I just want to try out a few ideas in a HTML/JavaScript client without having to scaffold a server to provide a bunch of RESTful APIs so I've built myself a dummy REST API to do just that.

Here's how I did it!

My requirements were:
  • Serve up the contents of a file based on a route for example \Customers would serve a file in the \Customers directory
  • Allow Cross-origin resource sharing and/or JSONP 
  • Allow me to change the JSON being served without having to restart anything

I decided to write the server in Go Lang since it's fun to scaffold up http web services with and the resultant utility will be cross platform so I can tinker away on my Mac with few issues. I've used Martini framework to get up and running quickly.

So the file directory will contain the files that I want to serve up as per:




The code to scan the file directory for the files is straightforward:

The code to serve up the file based on the matching route:

So with the GET file populated with some appropriate JSON:



So now when we run the servicestub application:



And if we choose a specific item:




Now to write some JavaScript!

Code here:

Monday, 1 July 2013

Setting up a Go Development Environment on a Samsung Chromebook

Since I've become an owner of a Samsung Chromebook, setting up a comfortable offline dev environment has been an ongoing goal.

I've been tinkering with various configurations for a while and have come up with this as as a starter for getting a Go Development environment up and running.

Part 1 - Installing the Go compiler

From start to finish this step should take about 40 minutes.

1. Boot the Samsung Chromebook into Developer Mode

The quick way to do this is to hold down escape + refresh + the power button.

Then press Control + D at the next page and over the course of the next 10-15 minutes the chromebook will wipe itself and install a developer mode enabled version. This brings a few things to the table like a command line shell.


2. Install Ubuntu with the help of Crouton 

I've tried dual booting Ubuntu on my Samsung Chromebook with ChrUbuntu which is fairly awesome but I had serious issues with the track pad, sound and general performance. I'm sure these issues will be ironed out over time and that being said - the wireless connectivity was much better on the Ubuntu boot than in Chrome Os - I didn't have to do the turn it off and on again song and dance with my router to get the Chromebook to connect which I frequently have to do with the Chrome Os boot.

Then I discovered Crouton and quickly I came to the conclusion that it is amazing. Ubuntu runs really smooth, the hardware works as per my Chromebook and whats more - I can switch between my linux distro and Chrome os at the touch of a button. Well four buttons (ctrl + alt + shift + Forward\Backward), but you get my point.

Installing Crouton is a piece of cake:

(i) Download the latest release from here: http://goo.gl/fd3zc
(ii) Open a shell (ctrl + alt + t) then type "shell" at the command prompt
(iii) type: sudo sh -e ~/Downloads/crouton -t unity

I've chosen unity over xfce which is used on the Crouton github page, no real reason here other than I preferred the look and feel.

Once it's finished installing it can be fired up with:

sudo enter-chroot startunity


3. Go

Go Lang is also very easy to install with apt-get and works beautifully:

sudo apt-get install golang


now check that Go is installed by typing: go version



Great, the Go compiler is in place.

Part 2 - Installing LiteIDE

This step takes about 20 minutes to complete.

Now compiling from the command line is one thing but I'm from the camp of developers that likes thumping out code in an IDE.

There are a couple of IDEs for Go that could be used - for example Eclipsegoclipse.

However I've chosen to go with LiteIDE for the time being as I like its simplicity.

There are a number of tools that need installing first that are missing from the crouton unity install:

1. make

sudo apt-get install make

2. g++

sudo apt-get install build-essential g++

3. GDB

sudo apt-get install gdb

4. QT4

sudo apt-get install libqt4-*

5. Git

sudo apt-get install git

6. LiteIDE

The only difference I had here from the instructions on the LiteIDE project page was with the QTDIR path which differs from the default path used by apt-get.

One further tip here - don't use sudo for the next few lines or you'll end up making work for yourself.

The build takes about 10 - 15 minutes. Once finished, we have a working LiteIDE in \litelide\bin

Opening the liteide application and confirming that traditional "hello world" code compiles and runs:


Excellent!