Esempio n. 1
0
func main() {
	// Create a new empty YARF server
	y := yarf.New()

	// Create resource
	hello := new(resource.Hello)

	// Add resource to multiple routes
	y.Add("/", hello)
	y.Add("/hello/:name", hello)

	// Create /extra route group
	e := yarf.RouteGroup("/extra")

	// Add custom middleware to /extra
	e.Insert(new(middleware.Hello))

	// Add same routes to /extra group
	e.Add("/", hello)
	e.Add("/hello/:name", hello)

	// Save group
	y.AddGroup(e)

	// Add logger middleware at the end of the chain
	y.Insert(new(logger.Logger))

	// Start server listening on port 8080
	y.Start(":8080")
}
Esempio n. 2
0
// Entry point of the executable application
// It runs a default server listening on http://localhost:8080
func main() {
	// Create a new empty YARF server
	y := yarf.New()

	// Add route/resource
	y.Add("/", new(Hello))

	// Start server listening on port 8080
	y.Start(":8080")
}
Esempio n. 3
0
func New(path, prefix string) *yarf.Yarf {
	// Init server
	y := yarf.New()

	// Set follow to file server
	y.Follow = http.StripPrefix(prefix, http.FileServer(http.Dir(path)))

	// Return without routes.
	return y
}
Esempio n. 4
0
// Benchmarks
func BenchmarkParamYarf(b *testing.B) {
	y := yarf.New()
	y.Add("/hello/:name", new(YarfParam))

	req, _ := http.NewRequest("GET", "http://localhost:8080/hello/Joe", nil)
	res := httptest.NewRecorder()

	for i := 0; i < b.N; i++ {
		y.ServeHTTP(res, req)
	}
}
Esempio n. 5
0
// Benchmarks
func BenchmarkSimpleYarf(b *testing.B) {
	y := yarf.New()
	y.Add("/", new(YarfHello))

	req, _ := http.NewRequest("GET", "http://localhost:8080/", nil)
	res := httptest.NewRecorder()

	for i := 0; i < b.N; i++ {
		y.ServeHTTP(res, req)
	}
}
Esempio n. 6
0
// Entry point of the executable application
// It runs a default server listening on http://localhost:8080
func main() {
	// Create a new empty YARF server
	y := yarf.New()

	// Add routes/resources
	y.Add("/", StaticDir("/tmp"))
	y.Add("/test", StaticDir("/var/www/test"))

	// Start server listening on port 8080
	y.Start(":8080")
}
Esempio n. 7
0
// Benchmarks
func BenchmarkMultiYarf(b *testing.B) {
	y := yarf.New()
	y.UseCache = false
	y.Add("/hello/:name1/:name2/:name3/:name4", new(YarfMulti))

	responses, requests := generateMultiRequests(b)
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		y.ServeHTTP(responses[i], requests[i])
	}
}
Esempio n. 8
0
// Benchmarks
func BenchmarkParamYarf(b *testing.B) {
	y := yarf.New()
	y.UseCache = false
	y.Add("/hello/:name", new(YarfParam))

	responses, requests := generateParamRequests(b)
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		y.ServeHTTP(responses[i], requests[i])
	}
}
Esempio n. 9
0
// Benchmarks
func BenchmarkSimpleYarf(b *testing.B) {
	y := yarf.New()
	y.UseCache = false
	y.Add("/", new(YarfHello))

	responses, requests := generateSimpleRequests(b)
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		y.ServeHTTP(responses[i], requests[i])
	}
}
Esempio n. 10
0
// Entry point of the executable application
// It runs a default server listening on http://localhost:8080
func main() {
	// Create a new empty YARF server
	y := yarf.New()

	// Add route/resource
	y.Add("/", new(Panic))

	// Set our custom panic handler
	y.PanicHandler = PanicHandler

	// Start server listening on port 8080
	y.Start(":8080")
}
Esempio n. 11
0
// Entry point of the executable application
// This time we setup a custom Go http server and use YARF as a router.
func main() {
	// Create a new empty YARF server
	y := yarf.New()

	// Add route/resource
	y.Add("/", new(Hello))

	// Configure custom http server and set the yarf object as Handler.
	s := &http.Server{
		Addr:           ":8080",
		Handler:        y,
		ReadTimeout:    10 * time.Second,
		WriteTimeout:   10 * time.Second,
		MaxHeaderBytes: 1 << 20,
	}
	s.ListenAndServe()
}
Esempio n. 12
0
// Entry point of the executable application
// It runs a default server listening on http://localhost:8080
func main() {
	// Create a new empty YARF server
	y := yarf.New()

	// Add middleware
	y.Insert(new(HelloMiddleware))

	// Create resource
	hello := new(Hello)

	// Add resource to multiple routes
	y.Add("/", hello)
	y.Add("/hello/:name", hello)

	// Start server listening on port 8080
	y.Start(":8080")
}
Esempio n. 13
0
// Benchmarks
func BenchmarkMultiYarf(b *testing.B) {
	y := yarf.New()
	y.Add("/hello/:name1/:name2/:name3/:name4", new(YarfMulti))

	req, _ := http.NewRequest("GET", "http://localhost:8080/hello/", nil)
	res := httptest.NewRecorder()

	for i := 0; i < b.N; i++ {
		for a := 0; a < 10; a++ {
			for b := 0; b < 10; b++ {
				for c := 0; c < 10; c++ {
					for d := 0; d < 10; d++ {
						req.URL, _ = url.Parse("http://localhost:8080/hello/" + strconv.Itoa(a) + "/" + strconv.Itoa(b) + "/" + strconv.Itoa(c) + "/" + strconv.Itoa(d))
						y.ServeHTTP(res, req)
					}
				}
			}
		}
	}
}
Esempio n. 14
0
// Entry point of the executable application
// It runs a default server listening on http://localhost:8080
//
// URLs available:
// http://localhost:8080
// http://localhost:8080/hello/:name
// http://localhost:8080/v2
// http://localhost:8080/v2/hello/:name
// http://localhost:8080/extra/v2
// http://localhost:8080/extra/v2/hello/:name
//
func main() {
	// Create a new empty YARF server
	y := yarf.New()

	// Create resources
	hello := new(Hello)
	hellov2 := new(HelloV2)

	// Add main resource to multiple routes
	y.Add("/", hello)
	y.Add("/hello/:name", hello)

	// Create /v2 route group
	g := yarf.RouteGroup("/v2")

	// Add v2 routes to the group
	g.Add("/", hellov2)
	g.Add("/hello/:name", hellov2)

	// Use middleware only on the group
	g.Insert(new(HelloMiddleware))

	// Add group to Yarf routes
	y.AddGroup(g)

	// Create another group for nesting
	n := yarf.RouteGroup("/extra")

	// Nest /v2 group into /extra
	n.AddGroup(g)

	// Use another middleware for this group
	n.Insert(new(ExtraMiddleware))

	// Add group to Yarf
	y.AddGroup(n)

	// Start server listening on port 8080
	y.Start(":8080")
}