mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-03-30 15:08:33 +00:00

* [NOD-350] Implement testnet faucet * [NOD-350] Add JSON annotations to api server response types * [NOD-350] Fix IP check query, update IP usage with upsert, and make IP a primary key * [NOD-377] Remove redundant float conversion * [NOD-377] Change not current database error message * [NOD-377] change API route from /money_request to /request_money * [NOD-377] Add a constant for 24 hours * [NOD-377] Remove redundant call for getWalletUTXOSet() * [NOD-377] Condition refactoring * [NOD-377] Fix POST request to API server content type * [NOD-350] Rename day -> timeBetweenRequests * [NOD-377] Rename timeBetweenRequests -> minRequestInterval, timeBefore24Hours -> minRequestInterval * [NOD-350] Rename file responsetypes -> response_types * [NOD-350] Rename convertTxModelToTxResponse -> convertTxDBModelToTxResponse * [NOD-350] Explicitly select blue_score in fetchSelectedTipBlueScore * [NOD-350] Refactor and add comments * [NOD-350] Make calcFee use MassPerTxByte * [NOD-350] Convert IP column to varchar(39) to allow ipv6 addresses * [NOD-350] Add comments to isFundedAndIsChangeOutputRequired * [NOD-350] Remove approximateConfirmationsForCoinbaseMaturity * [NOD-350] Fix comments
59 lines
1.7 KiB
Go
59 lines
1.7 KiB
Go
package httpserverutils
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/gorilla/mux"
|
|
"io/ioutil"
|
|
"net/http"
|
|
)
|
|
|
|
// HandlerFunc is a handler function that is passed to the
|
|
// MakeHandler wrapper and gets the relevant request fields
|
|
// from it.
|
|
type HandlerFunc func(ctx *ServerContext, r *http.Request, routeParams map[string]string, queryParams map[string]string, requestBody []byte) (
|
|
interface{}, *HandlerError)
|
|
|
|
// MakeHandler is a wrapper function that takes a handler in the form of HandlerFunc
|
|
// and returns a function that can be used as a handler in mux.Router.HandleFunc.
|
|
func MakeHandler(handler HandlerFunc) func(http.ResponseWriter, *http.Request) {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
ctx := ToServerContext(r.Context())
|
|
|
|
var requestBody []byte
|
|
if r.Method == "POST" {
|
|
var err error
|
|
requestBody, err = ioutil.ReadAll(r.Body)
|
|
if err != nil {
|
|
SendErr(ctx, w, NewInternalServerHandlerError("Error reading POST data"))
|
|
}
|
|
}
|
|
|
|
flattenedQueryParams, hErr := flattenQueryParams(r.URL.Query())
|
|
if hErr != nil {
|
|
SendErr(ctx, w, hErr)
|
|
return
|
|
}
|
|
|
|
response, hErr := handler(ctx, r, mux.Vars(r), flattenedQueryParams, requestBody)
|
|
if hErr != nil {
|
|
SendErr(ctx, w, hErr)
|
|
return
|
|
}
|
|
if response != nil {
|
|
SendJSONResponse(w, response)
|
|
}
|
|
}
|
|
}
|
|
|
|
func flattenQueryParams(queryParams map[string][]string) (map[string]string, *HandlerError) {
|
|
flattenedMap := make(map[string]string)
|
|
for param, valuesSlice := range queryParams {
|
|
if len(valuesSlice) > 1 {
|
|
return nil, NewHandlerError(http.StatusUnprocessableEntity, fmt.Sprintf("Couldn't parse the '%s' query parameter:"+
|
|
" expected a single value but got multiple values", param))
|
|
}
|
|
flattenedMap[param] = valuesSlice[0]
|
|
}
|
|
return flattenedMap, nil
|
|
}
|