multiple.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "github.com/gorilla/mux"
  7. "github.com/julienschmidt/httprouter"
  8. )
  9. func RegularRequest(w http.ResponseWriter, r *http.Request) {
  10. fmt.Fprint(w, "Request with averange amout of logic\n")
  11. }
  12. func FastRequest(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
  13. fmt.Fprint(w, "Request with high hitrate\n")
  14. }
  15. func ComplexRequest(w http.ResponseWriter, r *http.Request) {
  16. fmt.Fprint(w, "Request with complex routing logic\n")
  17. }
  18. func main() {
  19. fastApiHandler := httprouter.New()
  20. fastApiHandler.GET("/fast/:id", FastRequest)
  21. complexApiHandler := mux.NewRouter()
  22. complexApiHandler.HandleFunc("/complex/", ComplexRequest).
  23. Headers("X-Requested-With", "XMLHttpRequest") // ajax
  24. stdApiHandler := http.NewServeMux()
  25. stdApiHandler.HandleFunc("/std/", RegularRequest)
  26. siteMux := http.NewServeMux()
  27. siteMux.Handle("/fast/", fastApiHandler)
  28. siteMux.Handle("/complex/", complexApiHandler)
  29. siteMux.Handle("/std/", stdApiHandler)
  30. fmt.Println("starting server at :8080")
  31. log.Fatal(http.ListenAndServe(":8080", siteMux))
  32. }