Decouple routing rules from main func

The number of routes will increase and become more complex once we start adding more and more features. Best thing we can do now is to move them out of the main func. This also allows us to test the router (see main_test.go)
func main() { - router := http.NewServeMux() - router.Handle("/", &books{}) - err := http.ListenAndServe(":8080", router) + err := http.ListenAndServe(":8080", NewRouter()) if err != nil { log.Print(err) os.Exit(1) } } +func NewRouter() (router *http.ServeMux) { + router = http.NewServeMux() + router.Handle("/books", &books{}) + return +} + type books struct{} func (h *books) ServeHTTP(w http.ResponseWriter, r *http.Request) {
prev toc next