Gin vs Phoenix
May 3, 2016
Gin vs Phoenix
I am currently evaluating web frameworks to decide what to use for a new project. Despite my fluency in Ruby, I have decide against using Rails or any other Ruby-based solution. The project, if successful, will handle heavy loads. And from what I have frequently read, Rails project require significant gymnastics or conversions, at least in part, to other languages, to handle it. I would look to preempt such eventualities.
After considerable consideration, I have narrowed my choices down to Gin which is a [Go]-based solution, and Phoenix which is an [Elixir]-based one.
The first things first, I need to create a “Hello World” application for both frameworks.
Drinking Gin for the First Time
Gin was the easiest to setup, which makes sense since it minimalist framework. Installation is basically three steps.
$ sudo apt-get install golang
$ export GOPATH="$HOME/.local/lib/golang"
$ go get github.com/gin-gonic/gin
Note on step two, I actually did a bit more by adding the following to my ~/.profile script.
export GOPATH="$HOME/.local/lib/golang"
export PATH="$PATH:${GOPATH//://bin:}/bin"
No doubt in the future, I will need to install other dependencies, e.g. [Polymer]. And though it will require some additional steps on my part, it does feel kind of nice to have complete freedom in deciding what these other components will be.
Speaking of which, I decided to use Goat to manage my project so I don’t have to use golang’s traditional global workspace (a design decision that boggles my mind actually).
$ go get github.com/openbohemians/goat
Then I create a project directory.
$ mkdir mygin
$ cd testgin
And add a .go.yaml file to my project.
---
path: github.com/trans/wow
deps:
- loc: github.com/gin-gonic/gin
Now I just need to create a little go script. The example given on the Gin website is amusingly
a JSON-based “Ping-Pong” example – a fun deviation from the usual Hello World. I’ll take it.
And I create a file called test.go as follows:
package main
import "github.com/gin-gonic/gin"
func main() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
r.Run() // listen and server on 0.0.0.0:8080
}
Then I compile the project.
$ goat build
An executable file called mygin appears and I simply run it.
$ ./mygin
Surf to http://localhost:8080/ping and the Gin server will happily serve up a JSON
message { message: "pong" }.
Ignition with Phoenix
Phoenix, unlike Gin, is a full-stack framework, and it shows. While it wasn’t much more difficult to install and get a test project up and running, the number of dependencies is rather eye-popping. It starts with Erlang, which sits behind Elixir. And then Elixir which sits behind Phoenix. And then all the Elixir modules Phoenix needs that it pulls down from the Hex.pm package manager. And on top of all that it uses (albeit optional if you really want) brunch.io which needs [node.js] with [npm] and a slew of npm modules too. All said, I’d estimate well over 100 new packages were installed.
So in order…
$ wget https://packages.erlang-solutions.com/erlang-solutions_1.0_all.deb
$ sudo dpkg -i erlang-solutions_1.0_all.deb
$ rm erlang-solutions_1.0_all.deb
$ sudo apt-get update
$ sudo apt-get install esl-erlang
$ sudo apt-get install elixir
$ sudo apt-get install postgresql postgresql-contrib
$ sudo apt-get install inotify-tools
$ sudo apt-get install nodejs-legacy
$ sudo apt-get install npm
$ mix local.hex
$ mix archive.install https://github.com/phoenixframework/archives/raw/master/phoenix_new.ez
$ mix phoenix.new hello_phoenix
$ cd hello_phoenix
$ npm install
Note, I was a little disappointed to see Hex.pm install modules into ~/.hex instead of ~/.local/lib/hex.
How long is it going to take for XDG Directory standards to finally be adhered to by all? Of course, npm
does the same damn thing in ~/.npm, but at least they don’t end up in ~/node_modules anymore.
Before going any further we need to make sure the database is accessible. The simplest way is just to
set the postgres database password to postgres. Obviously that is something to be change in the
future, but it will suffice for the moment. If you want to make the password something else you will
have to edit the config/dev.exs file too.
$ sudo -u postgres psql postgres
# \password postgres
postgres
postgres
# \q
Now, we are almost there (phew).
$ mix ecto.create
Wee! More packages. But finally…
$ mix phoenix.server
And the server reports:
[info] Running HelloPhoenix.Endpoint with Cowboy using http on port 4000
04 May 12:36:55 - info: compiled 5 files into 2 files, copied 3 in 545ms
Now just surf over to http://localhost:4000/ and a nice Phoenix intro screen appears.