mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-10-14 00:59:33 +00:00

* [NOD-303] Implement get transaction by id for api server * [NOD-303] Make routeParamTxID a constant * [NOD-303] Change database is not current error. * [NOD-303] Add ID to TransactionInput and TransactionOutput models * [NOD-303] Change transactions_outputs table name to transaction_outputs and transactions_inputs to transaction_inputs * [NOD-303] Add json annotations to transaction response types * [NOD-303] Split server package * [NOD-303] Add GetTransactionByHashHandler * [NOD-303] Add comments to exported functions and variables * [NOD-303] Put response types in a separate file * [NOD-303] Rename functions
49 lines
1.4 KiB
Go
49 lines
1.4 KiB
Go
package server
|
|
|
|
import (
|
|
"github.com/daglabs/btcd/apiserver/utils"
|
|
"net/http"
|
|
"runtime/debug"
|
|
)
|
|
|
|
var nextRequestID uint64 = 1
|
|
|
|
func addRequestMetadataMiddleware(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
rCtx := utils.ToAPIServerContext(r.Context()).SetRequestID(nextRequestID)
|
|
r.WithContext(rCtx)
|
|
nextRequestID++
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|
|
|
|
func loggingMiddleware(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
ctx := utils.ToAPIServerContext(r.Context())
|
|
ctx.Infof("Method: %s URI: %s", r.Method, r.RequestURI)
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|
|
|
|
func recoveryMiddleware(h http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
ctx := utils.ToAPIServerContext(r.Context())
|
|
defer func() {
|
|
recoveryErr := recover()
|
|
if recoveryErr != nil {
|
|
log.Criticalf("Fatal error: %s", recoveryErr)
|
|
log.Criticalf("Stack trace: %s", debug.Stack())
|
|
sendErr(ctx, w, utils.NewHandlerError(http.StatusInternalServerError, "A server error occurred."))
|
|
}
|
|
}()
|
|
h.ServeHTTP(w, r)
|
|
})
|
|
}
|
|
|
|
func setJSONMiddleware(h http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
h.ServeHTTP(w, r)
|
|
})
|
|
}
|