package main

import (
	"log"
	"net/http"
	"time"

	"code.revolvingcow.com/revolvingcow/loop/environment"
	"code.revolvingcow.com/revolvingcow/loop/handlers"
	"github.com/gorilla/mux"
)

func main() {
	log.Print("Configuring environment")
	time.Local = time.UTC
	environment.ConfigureEnvironment()

	r := mux.NewRouter().StrictSlash(false)
	r.HandleFunc("/", handlers.DefaultHandler)

	auth := r.PathPrefix("/logon").Subrouter()
	auth.Methods("POST").HandlerFunc(handlers.LogonHandler)

	content := r.PathPrefix("/{username}").Subrouter()
	content.Methods("GET").HandlerFunc(handlers.ContentHandler)

	article := r.PathPrefix("/{username}/blog/{article}").Subrouter()
	article.Methods("GET").HandlerFunc(handlers.BlogArticleHandler)

	blog := r.PathPrefix("/{username}/blog").Subrouter()
	blog.Methods("GET").HandlerFunc(handlers.BlogHandler)

	log.Print("Serving content on ", environment.Address())
	log.Fatal(http.ListenAndServe(environment.Address(), r))
}
