예제 #1
0
// return response message with encoded json data and http status code 200 (OK)
func SuccessResponse(request *falcore.Request, jsonData interface{}) *http.Response {
	response, jsonError := falcore.JSONResponse(request.HttpRequest, http.StatusOK, nil, jsonData)
	if jsonError != nil {
		response = falcore.StringResponse(request.HttpRequest, http.StatusInternalServerError, nil, fmt.Sprintf("JSON error: %s", jsonError))
	}
	return response
}
예제 #2
0
// return response message with encoded json error message and a custom error code
// if logMessage is nil, do not write to log
func ErrorResponse(request *falcore.Request, status int, error string, errorDescription string, logMessage error) *http.Response {
	if logMessage != nil {
		log.Println(fmt.Sprintf("%s: %s, %s", error, errorDescription, logMessage))
	}

	type Json struct {
		Error            string
		ErrorDescription string
	}
	json := Json{error, errorDescription}

	response, jsonError := falcore.JSONResponse(request.HttpRequest, status, nil, json)
	if jsonError != nil {
		response = falcore.StringResponse(request.HttpRequest, http.StatusInternalServerError, nil, fmt.Sprintf("JSON error: %s", jsonError))
		log.Println(fmt.Sprintf("%s: %s, %s, json error:", error, errorDescription, logMessage, jsonError))
	}
	return response
}
예제 #3
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, _ := falcore.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, _ := falcore.JSONResponse(req.HttpRequest, 200, applicationJson, &world)