예제 #1
0
func main() {
	flag.Parse()
	h := web.SetErrorHandler(coreErrorHandler,
		web.ProcessForm(10000, true, web.NewHostRouter(nil).
			Register("www.example.com", web.NewRouter().
				Register("/", "GET", homeHandler).
				Register("/chat", "GET", chatFrameHandler).
				Register("/chat/ws", "GET", chatWsHandler).
				Register("/core/", "GET", coreHandler).
				Register("/core/a/<a>/", "GET", coreHandler).
				Register("/core/b/<b>/c/<c>", "GET", coreHandler).
				Register("/core/c", "POST", coreHandler))))

	err := server.ListenAndServe("localhost:8080", ":8080", h)
	if err != nil {
		log.Exit("ListenAndServe:", err)
	}
}
예제 #2
0
func main() {
	submitRoute := "/ajax/submit.json"
	statusRoute := "/ajax/status/<id:[^.]+>.json"
	router := web.NewRouter().
		Register("/", "GET", homeHandler).
		Register("/static/bookmarklet.js", "GET", handleBookmarklet).
		Register("/static/<path:.*>", "GET", web.DirectoryHandler("static", nil)).
		Register("/<page:(faq|bugs|contact)>", "GET", pageHandler).
		Register("/<chunk:(firefox|safari|chrome|ie|ios|kindle-email)>", "GET", chunkHandler).
		Register(submitRoute, "GET", submitHandler).
		Register(statusRoute, "GET", statusHandler).
		Register("/debug.json", "GET", expvar.ServeWeb).
		Register("/debug/pprof/<:.*>", "*", pprof.ServeWeb)

	redirector := web.NewRouter().
		// These routes get matched in both places so they work everywhere.
		Register(submitRoute, "GET", submitHandler).
		Register(statusRoute, "GET", statusHandler).
		Register("/<splat:>", "GET", redirectHandler)

	hostRouter := web.NewHostRouter(redirector).
		Register(canonicalHost, router)

	listener, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%s", port))
	if err != nil {
		logger.Fatalf("Failed to listen: %s", err)
	}
	defer listener.Close()
	server := &server.Server{
		Listener: listener,
		Handler:  hostRouter,
		Logger:   server.LoggerFunc(ShortLogger),
	}
	err = server.Serve()
	if err != nil {
		logger.Fatalf("Failed to server: %s", err)
	}
}