Exemplo n.º 1
0
func main() {
	flag.Parse()
	h := web.SetErrorHandler(coreErrorHandler,
		web.ProxyHeaderHandler("X-Real-Ip", "X-Scheme",
			web.NewRouter().
				Register("/debug/<:.*>", "*", web.NewRouter().
					Register("/debug/expvar", "GET", expvar.ServeWeb).
					Register("/debug/pprof/<:.*>", "*", pprof.ServeWeb)).
				Register("/<:.*>", "*", web.FormHandler(10000, true, web.NewRouter().
					Register("/", "GET", homeHandler).
					Register("/core/file", "GET", web.FileHandler("static/file.txt", nil)).
					Register("/static/<path:.*>", "GET", web.DirectoryHandler("static/", nil)).
					Register("/chat", "GET", chatFrameHandler).
					Register("/chat/ws", "GET", chatWsHandler).
					Register("/mp", "GET", mpGetHandler, "POST", mpPostHandler).
					Register("/debug/pprof/<command>", "*", web.HandlerFunc(pprof.ServeWeb)).
					Register("/core/", "GET", coreHandler).
					Register("/core/a/<a>/", "GET", coreHandler).
					Register("/core/b/<b>/c/<c>", "GET", coreHandler).
					Register("/core/c", "POST", coreHandler)))))

	listener, err := net.Listen("tcp", ":8080")
	if err != nil {
		log.Fatal("Listen", err)
		return
	}
	defer listener.Close()
	err = (&server.Server{Listener: listener, Handler: h, Logger: server.LoggerFunc(server.VerboseLogger)}).Serve()
	if err != nil {
		log.Fatal("Server", err)
	}
}
Exemplo n.º 2
0
func main() {
	flag.Parse()
	r := web.NewRouter().
		Register("/", "*", web.FormHandler(-1, false, web.HandlerFunc(handler))).
		Register("/static/<path:.*>", "GET", web.DirectoryHandler("static/", nil)).
		Register("/example/file.txt", "GET", web.FileHandler("static/file.txt", nil)).
		Register("/urlparam/<a>/<b>", "GET", handler).
		Register("/panic/before", "GET", web.HandlerFunc(panicBeforeResponse)).
		Register("/panic/after", "GET", web.HandlerFunc(panicAfterResponse)).
		Register("/limit", "POST", web.FormHandler(16, false, web.HandlerFunc(handler))).
		Register("/xsrf", "*", web.FormHandler(-1, true, web.HandlerFunc(handler))).
		Register("/multipart", "POST", multipartHandler).
		Register("/debug/expvar", "GET", expvar.ServeWeb).
		Register("/debug/pprof/<:.*>", "*", pprof.ServeWeb).
		Register("/proxy", "GET", web.ProxyHeaderHandler("X-Real-Ip", "X-Scheme", web.HandlerFunc(handler)))

	h := web.SetErrorHandler(errorHandler, r)

	if *useAdapter {
		log.Print("Running with adapter.")
		if err := http.ListenAndServe(*addr, adapter.HTTPHandler{h}); err != nil {
			log.Fatal(err)
		}
	} else {
		listener, err := net.Listen("tcp", *addr)
		if err != nil {
			log.Fatal("Listen", err)
		}
		defer listener.Close()
		if err := (&server.Server{Listener: listener, Handler: h, Logger: server.LoggerFunc(server.VerboseLogger)}).Serve(); err != nil {
			log.Fatal(err)
		}
	}
}
Exemplo n.º 3
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)
	}
}