Saturday, June 13, 2015

Enable Jetty request log in Maven

For development I use Jetty locally via jetty-maven-plugin. One of the nice features it has WebSocket support so there is no need to use standalone application servers like Wildfly.

If you check jetty documentation you'll see that request logging is configured in jetty's context xml file. Fortunately if you don't want to add another xml to your project there is another option.

Add the following configuration to your project's pom.xml:

The implementation attribute value of the requestLog property can be one of these: AsyncNCSARequestLog, NCSARequestLog or Slf4jRequestLog. Note that options vary depending on the implementation. I choose NCSARequestLog because it produces logs in NCSA common log format

The log file will be created in the root directory of your project when jetty is started.

Tuesday, June 9, 2015

Lets try that golang hype

Recently I've decided to take a look at golang. I didn't know where to start because I didn't have any problem to solve. So I briefly outlined the first steps to cover. I want to create a simple web service or a couple of services for some SPA application. But first I need to get more familiar with golang environment and tools.

Installation

I opened the golang home page and quickly viewed its content.

It happened that my laptop has funtoo linux installed. I quickly typed in the console the following to install the golang package:

$ emerge -av dev-lang/go

I wasn't quite sure funtoo has golang in its repository. Fortunately there was golang version 1.4.2 which is the most recent stable. There is a lot of files installed but I was interested in binaries first:

$ equery f dev-lang/go | grep /usr/bin/
/usr/bin/go
/usr/bin/gofmt

Not much but lets run them and see its output:

$ go

The usage section briefly explains the commands available. I noticed few commands that do not belong to a compiler usually: clean, run, test, and get. Looks like it's a compiler, a runtime, a package manager and a test runner altogether which is not following the UNIX-way. I decided to figure out it later.

Lets see what is gofmt:

$ gofmt

...which outputs nothing but just waiting for input. Running with -h gives a bit more info. As far as I see it is a some kind of a linter for golang sources. I wonder why it is in a separate executable and not included into the go. Leaving it for now.

Write some code

There is nothing special about it just create a .go file and paste the example from golang site. I skipped the formatting for a purpose:

$ vim /tmp/hello_world.go

Now lets get back to gofmt and see how it works, the -w option overwrites file's content instead of writing formatted code to stdout:

$ gofmt -w hello_world.go

Works as expected but I noticed that it does not add a new line to the end of file:

By the way if you use vim you can add a hook for formatting with gofmt after buffer write:

:set autoread | autocmd BufWritePost *.go :silent !gofmt -w %

Build and run it

I've seen earlier from the go executable output that I can run the source file right away by using run command option:

$ go run hello_world.go
Hello, World!

Pretty straightforward but I noticed a small delay before program output probably caused by intermediate building or interpreting step of the run command. Also there was no binary file created.

Running with the build command option finally produced a binary:

$ go build hello_world.go

The size of the binary is about 2M:

$ ll hello_world
-rwxr-xr-x 1 user users 1.9M Jun 9 13:29 ./hello_world

Also it's not linked dynamically to anything:

$ ldd hello_world
not a dynamic executable

and is a 64-bit executable:

$ file hello_world
hello_world: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped

Summary

As a conclusion I think golang tools are friendly and simple to start with. Except a few things I noticed. These are camel notation for functions and a single executable for building, testing, running and managing packages for your golang programs.

If you like you could try golang online without having it installed locally.