コード例 #1
0
	//}
}

var requiredHeaders = falcore.NewResponseFilter(func(req *falcore.Request, res *http.Response) {
	res.Header.Set("Server", "falcore")
	res.Header.Set("Date", time.Now().Format(time.RFC1123))
})

var applicationJson = http.Header{"Content-Type": []string{"application/json"}}
var textPlain = http.Header{"Content-Type": []string{"text/plain"}}
var textHtml = http.Header{"Content-Type": []string{"text/html"}}

// Test 1: JSON serialization
var jsonFilter = falcore.NewRequestFilter(func(req *falcore.Request) *http.Response {
	if req.HttpRequest.URL.Path == "/json" {
		resp, _ := responder.JSONResponse(req.HttpRequest, 200, applicationJson, &Message{helloWorldString})
		return resp
	}
	return nil
})

// Test 2: Single database query
var dbFilter = falcore.NewRequestFilter(func(req *falcore.Request) *http.Response {
	if req.HttpRequest.URL.Path == "/db" {
		var world World
		err := worldStatement.QueryRow(rand.Intn(worldRowCount)+1).Scan(&world.Id, &world.RandomNumber)
		if err != nil {
			log.Fatalf("Error scanning world row: %s", err.Error())
		}

		resp, _ := responder.JSONResponse(req.HttpRequest, 200, applicationJson, &world)
コード例 #2
0
		log.Println("Could not start server:", err)
	}
	//if err := http.ListenAndServe(":8080", server); err != nil {
	//      log.Println("Could not start server:", err)
	//}
}

var requiredHeaders = falcore.NewResponseFilter(func(req *falcore.Request, res *http.Response) {
	res.Header.Set("Server", "falcore")
	res.Header.Set("Date", time.Now().Format(time.RFC1123))
})

// Test 1: JSON serialization
var jsonFilter = falcore.NewRequestFilter(func(req *falcore.Request) *http.Response {
	if req.HttpRequest.URL.Path == "/json" {
		resp, _ := responder.JSONResponse(req.HttpRequest, 200, nil, &Message{helloWorldString})
		return resp
	}
	return nil
})

// Test 2: Single database query
var dbFilter = falcore.NewRequestFilter(func(req *falcore.Request) *http.Response {
	if req.HttpRequest.URL.Path == "/db" {
		var world World
		err := worldStatement.QueryRow(rand.Intn(worldRowCount)+1).Scan(&world.Id, &world.RandomNumber)
		if err != nil {
			log.Fatalf("Error scanning world row: %s", err.Error())
		}

		resp, _ := responder.JSONResponse(req.HttpRequest, 200, nil, &world)