Some refactorings

Split all the application logic into a separate file so that `main.go` is only concerned with starting up the application - listening on addresses, setting up http serving.

I'm planning to introduce changes to allow zero-downtime restarts, and this will add to the amount of logic in main, hence wanting to split out what can be.