예제 #1
0
func main() {
	middle := interpose.New()

	// First call middleware that may manipulate HTTP headers, since
	// they must be called before the HTTP body is manipulated

	// Using Gorilla framework's combined logger
	middle.Use(middleware.GorillaLog())

	//Using Negroni's Gzip functionality
	middle.Use(middleware.NegroniGzip(gzip.DefaultCompression))

	// Now call middleware that can manipulate the HTTP body

	// Define the router. Note that we can add the router to our
	// middleware stack before we define the routes, if we want.
	router := mux.NewRouter()
	middle.UseHandler(router)

	// Configure our router
	router.HandleFunc("/{user}", func(w http.ResponseWriter, req *http.Request) {
		fmt.Fprintf(w, "Welcome to the home page, %s!", mux.Vars(req)["user"])
	})

	// Define middleware that will apply to only some routes
	greenMiddle := interpose.New()

	// Tell the main router to send /green requests to our subrouter.
	// Again, we can do this before defining the full middleware stack.
	router.Methods("GET").PathPrefix("/green").Handler(greenMiddle)

	// Within the secondary middleware, just like above, we want to call anything that
	// will modify the HTTP headers before anything that will modify the body
	greenMiddle.UseHandler(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
		rw.Header().Set("X-Favorite-Color", "green")
	}))

	// Finally, define a sub-router based on our love of the color green
	// When you call any url such as http://localhost:3001/green/man , you will
	// also see an HTTP header sent called X-Favorite-Color with value "green"
	greenRouter := mux.NewRouter().Methods("GET").PathPrefix("/green").Subrouter() //Headers("Accept", "application/json")
	greenMiddle.UseHandler(greenRouter)

	greenRouter.HandleFunc("/{user}", func(w http.ResponseWriter, req *http.Request) {
		fmt.Fprintf(w, "Welcome to the home page, green %s!", mux.Vars(req)["user"])
	})

	http.ListenAndServe(":3001", middle)
}
예제 #2
0
파일: tiers.go 프로젝트: haste/tiers
func main() {
	model.Init()

	middle := interpose.New()
	middle.Use(middleware.NegroniGzip(gzip.DefaultCompression))

	r := mux.NewRouter()

	middle.UseHandler(r)

	r.HandleFunc("/", page.ProfileHandler)
	r.HandleFunc("/profile", page.ProfileHandler)
	r.HandleFunc("/profile/{period}", page.ProfileHandler)

	r.HandleFunc("/badges", func(w http.ResponseWriter, r *http.Request) {
		http.Redirect(w, r, "/progress", 302)
	})
	r.HandleFunc("/progress", page.ProgressHandler)
	r.HandleFunc("/profiles", page.ProfilesHandler)

	r.HandleFunc("/admin/process", requireAdmin(page.ProcessIndexHandler))
	r.HandleFunc("/admin/process/{owner}", requireAdmin(page.ProcessListHandler))
	r.HandleFunc("/admin/process/{owner}/{faulty}/{previous}", requireAdmin(page.ProcessQueueHandler))
	r.HandleFunc("/admin/process/{owner}/{faulty}/{previous}/run", requireAdmin(page.ProcessRunHandler))

	r.HandleFunc("/signin", LoginHandler)
	r.HandleFunc("/logout", LogoutHandle)
	r.HandleFunc("/signup", page.SignupHandler).Methods("POST")
	r.HandleFunc("/reset_password/{token:[a-f0-9]+}", page.ResetPassViewHandler).Methods("GET")
	r.HandleFunc("/reset_password/{token:[a-f0-9]+}", page.ResetPassHandler).Methods("POST")
	r.HandleFunc("/reset_password", page.ResetPassMailHandler).Methods("POST")
	r.HandleFunc("/gplus", page.GPlusHandler)

	r.HandleFunc("/upload", page.UploadViewHandler).Methods("GET")
	r.HandleFunc("/upload", page.UploadHandler).Methods("POST")

	r.PathPrefix("/secret_cache/").Handler(http.StripPrefix("/secret_cache/", http.FileServer(http.Dir("cache"))))
	r.PathPrefix("/").Handler(http.FileServer(http.Dir("static")))

	http.Handle("/", middle)

	go queue.ProcessQueue()
	queue.Queue <- true

	log.Fatal(http.ListenAndServeTLS(conf.Config.Address, conf.Config.Cert, conf.Config.Key, nil))
}
예제 #3
0
func main() {
	middle := interpose.New()

	// First apply any middleware that will not write output to http body

	// Log to stdout. Taken from Gorilla
	middle.Use(middleware.GorillaLog())

	// Gzip output that follows. Taken from Negroni
	middle.Use(middleware.NegroniGzip(gzip.DefaultCompression))

	// Now apply any middleware that modify the http body.
	router := mux.NewRouter()
	middle.UseHandler(router)

	router.HandleFunc("/{user}", func(w http.ResponseWriter, req *http.Request) {
		fmt.Fprintf(w, "Welcome to the home page, %s!", mux.Vars(req)["user"])
	})

	http.ListenAndServe(":3001", middle)
}