package main

import (
	_ "expvar"
	"fmt"
	"github.com/codegangsta/negroni"
	"net/http"
)

func main() {
	Run()
}

func Run() {
	m := http.DefaultServeMux

	m.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
		fmt.Fprintf(w, "Welcome to the home page!")
	})

	n := negroni.Classic()
	n.UseHandler(m)
	n.Run(":3000")
}

package main

import (
	_ "expvar"
	"fmt"
	"github.com/codegangsta/negroni"
	"github.com/meatballhat/negroni-logrus"
	"net/http"
)

func main() {
	Run()
}

func Run() {
	m := http.DefaultServeMux

	m.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
		fmt.Fprintf(w, "Welcome to the home page!")
	})

	n := negroni.New(negroni.NewRecovery())
	l := negronilogrus.NewMiddleware()

	n.Use(l)
	n.UseHandler(m)

	addr := ":3000"
	l.Logger.Infof("Listening on %s", addr)
	l.Logger.Fatal(http.ListenAndServe(addr, n))
}

Add logrus middleware for better logging
Add logrus middleware for better logging

Logrus can provide us with:

- JSON log output
- Log levels
- Airbrake/Errbit integration

...but for now, it simply provides prettier output on stdout.

Note the changes to the way I invoke Negroni:

- I use `negroni.New()` rather than `negroni.Classic()` (which acts as a
  wrapper for `New()`), as I've no need for serving static files (not
  yet at least) and I don't want to use the built-in logger. I've kept
  the recovery middleware as that is useful. The recovery middleware
  still uses Negroni's default  logger, but we can improve on that
  later.

- I've replaced `negroni.Run()` with `http.ListenAndServe()`, which
  `Run()` was wrapping. I prefer this because:

    a) It exposes what Negroni's doing under the hood, which feels
    less magic

    b) I can have logrus output the "Listening on :3000" line rather
    than the standard logger
