// Start serving the blog. func run() { var ( faviconPath = filepath.Join(PublicDir, "favicon.ico") faviconCache = 2 * 24 * time.Hour ) h := handlers.FaviconHandler( handlers.PanicHandler( handlers.LogHandler( handlers.GZIPHandler( http.FileServer(http.Dir(PublicDir)), nil), handlers.NewLogOptions(nil, handlers.Ldefault)), nil), faviconPath, faviconCache) // Assign the combined handler to the server. http.Handle("/", h) // Start it up. log.Printf("trofaf server listening on port %d", Options.Port) if err := http.ListenAndServe(fmt.Sprintf(":%d", Options.Port), nil); err != nil { log.Fatal("FATAL ", err) } }
// Prepare the web server and kick it off. func main() { // Blank the default logger's prefixes log.SetFlags(0) // Compile the dynamic templates (native Go templates and Amber // templates are both registered via the for-side-effects-only imports) err := templates.CompileDir("./templates/") if err != nil { panic(err) } // Set the simple routes for static files mux := pat.New() mux.Get("/", handlers.StaticFileHandler("./index.html")) mux.Get("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir("./public/")))) // Set the more complex routes for session handling and dynamic page (same // handler is used for both GET and POST). ssnOpts := handlers.NewSessionOptions(memStore, secret) ssnOpts.CookieTemplate.MaxAge = sessionExpiration hSsn := handlers.SessionHandler( handlers.ContextHandlerFunc( handlers.GhostHandlerFunc(sessionPageRenderer), 1), ssnOpts) mux.Get("/session", hSsn) mux.Post("/session", hSsn) hAuthSsn := handlers.BasicAuthHandler(hSsn, authenticate, "") mux.Get("/session/auth", hAuthSsn) mux.Post("/session/auth", hAuthSsn) // Set the handler for the chained context route mux.Get("/context", handlers.ContextHandler(handlers.ChainHandlerFuncs( handlers.GhostHandlerFunc(setContext), handlers.GhostHandlerFunc(renderContextPage)), 1)) // Set the panic route, which simply panics mux.Get("/panic", http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { panic("explicit panic") })) // Combine the top level handlers, that wrap around the muxer. // Panic is the outermost, so that any panic is caught and responded to with a code 500. // Log is next, so that every request is logged along with the URL, status code and response time. // GZIP is then applied, so that content is compressed. // Finally, the muxer finds the specific handler that applies to the route. h := handlers.FaviconHandler( handlers.PanicHandler( handlers.LogHandler( handlers.GZIPHandler( mux, nil), handlers.NewLogOptions(nil, handlers.Ltiny)), nil), "./public/favicon.ico", 48*time.Hour) // Assign the combined handler to the server. http.Handle("/", h) // Start it up. if err := http.ListenAndServe(":9000", nil); err != nil { panic(err) } }