Esempio n. 1
0
func init() {
	router = traffic.New()

	router.Get("/", RootHandler)

	// match (width)x(height) format
	// we cannot choose which character set the named routes can take
	// they only stop matching on / # ? ( ) . \
	// we should be able to do something like
	// (:width=[\d+])(x(:height=[\d+]))?
	// or something even simplier like
	// (:width=:digits)(x(:height=:digits))(.(:format=json|xml|atom))
	router.Get(`/(?P<width>\d+)(x(?P<height>\d+)?)?/?`, ImageHandler).
		AddBeforeFilter(RequireValidImageParameters).
		AddBeforeFilter(GenerateImageCache)

	router.Get(`/:width/?(:height)?/?`, ImageHandler).
		AddBeforeFilter(RequireValidImageParameters).
		AddBeforeFilter(GenerateImageCache)

	// Executed before all handlers
	router.AddBeforeFilter(PoweredByHandler)

	// Custom not found handler
	router.NotFoundHandler = NotFoundHandler

	// if not in development, add the static handler
	if traffic.Env() == "production" {
		router.Use(traffic.NewStaticMiddleware(traffic.PublicPath()))
	}

}
Esempio n. 2
0
func main() {
	router := traffic.New()
	router.Get("/", rootHandler)
	router.Get("/server_side/", serverSideHandler).AddBeforeFilter(addLocationHeader)
	router.Get("/facetime/", facetimeHandler)
	router.Get("/third_party/", thirdPartyHandler)
	router.NotFoundHandler = customNotFoundHandler
	router.Run()
}
Esempio n. 3
0
func init() {
	initDatabase()

	router = traffic.New()
	router.NotFoundHandler = NotFoundHandler
	router.ErrorHandler = ErrorHandler
	router.AddBeforeFilter(SetDefaultHeaders)

	router.Get("/", RootHandler)
	// Artist:
	//   /artists/056e4f3e-d505-4dad-8ec1-d04f521cbb56
	router.Get("/artists/:id.json", ArtistHandler)

	// Release Group:
	//   /release-groups/aa997ea0-2936-40bd-884d-3af8a0e064dc
	//   /artists/056e4f3e-d505-4dad-8ec1-d04f521cbb56/release-groups/aa997ea0-2936-40bd-884d-3af8a0e064dc
	router.Get("(/artists/:artist_id)?/release-groups/:id.json", ReleaseGroupHandler).
		AddBeforeFilter(CheckArtistFilter)

	// Release Groups:
	//   /artists/056e4f3e-d505-4dad-8ec1-d04f521cbb56/release-groups
	router.Get("/artists/:artist_id/release-groups.json", ReleaseGroupsHandler).
		AddBeforeFilter(CheckArtistFilter)

	// Release:
	//   /releases/79215cdf-4764-4dee-b0b9-fec1643df7c5
	//   /artists/056e4f3e-d505-4dad-8ec1-d04f521cbb56/releases/79215cdf-4764-4dee-b0b9-fec1643df7c5
	router.Get("(/artists/:artist_id)?/releases/:id.json", ReleaseHandler).
		AddBeforeFilter(CheckArtistFilter)

	// Releases:
	//   /artists/056e4f3e-d505-4dad-8ec1-d04f521cbb56/releases
	router.Get("/artists/:artist_id/releases.json", ReleasesHandler).
		AddBeforeFilter(CheckArtistFilter)

	// Releases:
	//   /release-groups/aa997ea0-2936-40bd-884d-3af8a0e064dc/releases
	//   /artists/056e4f3e-d505-4dad-8ec1-d04f521cbb56/release-groups/aa997ea0-2936-40bd-884d-3af8a0e064dc/releases
	router.Get("(/artists/:artist_id)?/release-groups/:release_group_id/releases.json", ReleasesHandler).
		AddBeforeFilter(CheckArtistFilter).
		AddBeforeFilter(CheckReleaseGroupFilter)

	// Recordings:
	//   /releases/79215cdf-4764-4dee-b0b9-fec1643df7c5/recordings
	//   /artists/056e4f3e-d505-4dad-8ec1-d04f521cbb56/releases/79215cdf-4764-4dee-b0b9-fec1643df7c5/recordings
	router.Get("(/artists/:artist_id)?/releases/:release_id/recordings.json", RecordingsHandler).
		AddBeforeFilter(CheckArtistFilter).
		AddBeforeFilter(CheckReleaseFilter)

	// Recording:
	//   /recordings/0c871a4a-efdf-47f8-98c2-cc277f806d2f
	//   /releases/79215cdf-4764-4dee-b0b9-fec1643df7c5/recordings/0c871a4a-efdf-47f8-98c2-cc277f806d2f
	//   /artists/056e4f3e-d505-4dad-8ec1-d04f521cbb56/releases/79215cdf-4764-4dee-b0b9-fec1643df7c5/recordings/833f00e1-781f-4edd-90e4-e52712618862
	router.Get("((/artists/:artist_id)?/releases/:release_id)?/recordings/:id.json", RecordingHandler).
		AddBeforeFilter(CheckArtistFilter).
		AddBeforeFilter(CheckReleaseFilter)
}
Esempio n. 4
0
func main() {
	traffic.TemplateFunc("upcase", strings.ToUpper)
	traffic.TemplateFunc("downcase", strings.ToLower)

	router := traffic.New()
	router.Get("/", indexHandler)
	router.Get("/about/?", aboutHandler)
	router.Run()
}
Esempio n. 5
0
func main() {
	traffic.SetVar("env", "production")
	router := traffic.New()
	router.Use(airbrake.New(os.Getenv("AIRBRAKE_API_KEY")))

	// Routes
	router.Get("/", rootHandler)
	router.Run()
}
Esempio n. 6
0
func main() {
	router := traffic.New()
	// Routes
	router.Get("/", rootHandler)
	router.Get("/json", jsonTestHandler)
	router.Get("/xml", xmlTestHandler)
	router.Get("/categories/:category_id/pages/:id", pageHandler)

	router.Run()
}
func BenchmarkPiluTraffic_Simple(b *testing.B) {
	traffic.SetVar("env", "production")
	router := traffic.New()
	router.Get("/action", piluTrafficHandler)
	rw, r := testRequest("GET", "/action")
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		router.ServeHTTP(rw, r)
	}
}
Esempio n. 8
0
func main() {
	t := traffic.New()
	// Add PingMiddleware
	t.Use(&PingMiddleware{})
	// Set router var "foo"
	t.SetVar("foo", "bar")
	// Add root handler
	t.Get("/", root)

	t.Run()
}
Esempio n. 9
0
func main() {
	router := traffic.New()

	// Routes
	router.Get("/", rootHandler)
	router.Get("/categories/:category_id/pages/:id", pageHandler)

	// Custom not found handler
	router.NotFoundHandler = customNotFoundHandler

	router.Run()
}
Esempio n. 10
0
func main() {
	// Setting env to `production`.
	// Otherwise the ShoeErrors Middleware used in development
	// will recover from the panics.

	traffic.SetVar("env", "production")

	router := traffic.New()
	router.ErrorHandler = errorHandler
	router.Get("/", rootHandler)
	router.Run()
}
Esempio n. 11
0
func (api *ImgServerApi) Start() {

	traffic.SetHost(api.host)
	traffic.SetPort(api.port)
	router := traffic.New()
	router.Get("/", api.listHandler)
	router.Put("/", api.statsHandler)
	router.Get("/:image", api.imageHandler)
	router.Post("/:image", api.uploadHandler)
	router.Put("/:src/:dst", api.copyHandler)
	router.Delete("/:image", api.deleteHandler)
	router.Run()
}
Esempio n. 12
0
func piluTrafficRouterFor(namespaces []string, resources []string) http.Handler {
	traffic.SetVar("env", "production")
	router := traffic.New()
	for _, ns := range namespaces {
		for _, res := range resources {
			router.Get("/"+ns+"/"+res, piluTrafficHandler)
			router.Post("/"+ns+"/"+res, piluTrafficHandler)
			router.Get("/"+ns+"/"+res+"/:id", piluTrafficHandler)
			router.Put("/"+ns+"/"+res+"/:id", piluTrafficHandler)
			router.Delete("/"+ns+"/"+res+"/:id", piluTrafficHandler)
		}
	}
	return router
}
Esempio n. 13
0
func init() {
	var err error
	dbString := os.Getenv("DB")
	DB, err = sql.Open("postgres", dbString)
	if err != nil {
		log.Fatal(err)
	}
	lyricfindClient = lyricfind.NewClient()

	router = traffic.New()
	router.AddBeforeFilter(SetResponseHeaders)
	router.Get("/", RootHandler)
	router.Get("/recordings/:gid", RecordingHandler)
	router.NotFoundHandler = NotFoundHandler
}
Esempio n. 14
0
func main() {
	router := traffic.New()

	// Routes
	router.Get("/", rootHandler)
	router.Get("/categories/:category_id/pages/:id", pageHandler)
	// "/private" has one more before filter that checks for a second api key (private_api_key)
	router.Get("/private", privatePageHandler).
		AddBeforeFilter(checkPrivatePageApiKey)

	// Executed before all handlers
	router.AddBeforeFilter(checkApiKey, addAppNameHeader, addTimeHeader)

	router.Run()
}
func loadTrafficSingle(method, path string, handler traffic.HttpHandleFunc) http.Handler {
	router := traffic.New()
	switch method {
	case "GET":
		router.Get(path, handler)
	case "POST":
		router.Post(path, handler)
	case "PUT":
		router.Put(path, handler)
	case "PATCH":
		router.Patch(path, handler)
	case "DELETE":
		router.Delete(path, handler)
	default:
		panic("Unknow HTTP method: " + method)
	}
	return router
}
func loadTraffic(routes []route) http.Handler {
	router := traffic.New()
	for _, route := range routes {
		switch route.method {
		case "GET":
			router.Get(route.path, trafficHandler)
		case "POST":
			router.Post(route.path, trafficHandler)
		case "PUT":
			router.Put(route.path, trafficHandler)
		case "PATCH":
			router.Patch(route.path, trafficHandler)
		case "DELETE":
			router.Delete(route.path, trafficHandler)
		default:
			panic("Unknow HTTP method: " + route.method)
		}
	}
	return router
}
Esempio n. 17
0
func BenchmarkPiluTraffic_Middleware(b *testing.B) {
	traffic.SetVar("env", "production")
	router := traffic.New()
	router.Use(&trafficMiddleware{})
	router.Use(&trafficMiddleware{})
	router.Use(&trafficMiddleware{})
	router.Use(&trafficMiddleware{})
	router.Use(&trafficMiddleware{})
	router.Use(&trafficMiddleware{})
	router.Get("/action", piluTrafficHandler)

	rw, req := testRequest("GET", "/action")

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		router.ServeHTTP(rw, req)
		if rw.Code != 200 {
			panic("no good")
		}
	}
}
Esempio n. 18
0
func BenchmarkPiluTraffic_Composite(b *testing.B) {
	namespaces, resources, requests := resourceSetup(10)

	traffic.SetVar("env", "production")
	router := traffic.New()
	router.Use(&trafficCompositeMiddleware{})
	router.Use(&trafficMiddleware{})
	router.Use(&trafficMiddleware{})
	router.Use(&trafficMiddleware{})
	router.Use(&trafficMiddleware{})
	router.Use(&trafficMiddleware{})

	for _, ns := range namespaces {
		for _, res := range resources {
			router.Get("/"+ns+"/"+res, piluTrafficCompositeHandler)
			router.Post("/"+ns+"/"+res, piluTrafficCompositeHandler)
			router.Get("/"+ns+"/"+res+"/:id", piluTrafficCompositeHandler)
			router.Put("/"+ns+"/"+res+"/:id", piluTrafficCompositeHandler)
			router.Delete("/"+ns+"/"+res+"/:id", piluTrafficCompositeHandler)
		}
	}
	benchmarkRoutes(b, router, requests)
}
Esempio n. 19
0
func main() {
	router := traffic.New()
	router.Use(chromelogger.New())
	router.Get("/", rootHandler)
	router.Run()
}
Esempio n. 20
0
func main() {
	router := traffic.New()
	router.Get("/", rootHandler)
	router.Run()
}
Esempio n. 21
0
func main() {
	router := traffic.New()
	router.Get("/:page_path*?", pageHandler)
	router.Run()
}
Esempio n. 22
0
func startTraffic() {
	traffic.SetVar("env", "bench")
	mux := traffic.New()
	mux.Get("/hello", trafficHandler)
	http.ListenAndServe(":"+strconv.Itoa(port), mux)
}
Esempio n. 23
0
func init() {
	router = traffic.New()
	router.Get("/", RootHandler)
}
Esempio n. 24
0
func init() {
	app = traffic.New()
	app.Get("/", rootHandler)
	app.Get("/:code", codesHandler)
}