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
38 lines
873 B
Go
38 lines
873 B
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"github.com/gorilla/handlers"
|
|
"github.com/gorilla/mux"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
const gracefulShutdownTimeout = 30 * time.Second
|
|
|
|
// Start starts the HTTP REST server and returns a
|
|
// function to gracefully shutdown it.
|
|
func Start(listenAddr string) func() {
|
|
router := mux.NewRouter()
|
|
router.Use(addRequestMetadataMiddleware)
|
|
router.Use(recoveryMiddleware)
|
|
router.Use(loggingMiddleware)
|
|
router.Use(setJSONMiddleware)
|
|
addRoutes(router)
|
|
httpServer := &http.Server{
|
|
Addr: listenAddr,
|
|
Handler: handlers.CORS()(router),
|
|
}
|
|
spawn(func() {
|
|
log.Errorf("%s", httpServer.ListenAndServe())
|
|
})
|
|
return func() {
|
|
ctx, cancel := context.WithTimeout(context.Background(), gracefulShutdownTimeout)
|
|
defer cancel()
|
|
err := httpServer.Shutdown(ctx)
|
|
if err != nil {
|
|
log.Errorf("Error shutting down HTTP server: %s", err)
|
|
}
|
|
}
|
|
}
|