mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-06 06:06:49 +00:00

* [NOD-319] Add query params to api server route handler * Temp commit * [NOD-322] Make database.DB a function * [NOD-322] Move context to be the first parameter in all functions * [NOD-322] Set db to nil on database.Close() * [NOD-322] Tidy go.mod/go.sum * [NOD-323] Move rpc-client to separate package * [NOD-309] Add controller for POST /transaction * [NOD-309] Added route for POST /transaction * [NOD-309] in POST /transaction: Forward reject errors to client * [NOD-309] Added custom client messages to errors in POST /transaction * [NOD-309] Use utils.NewInternalServerHandlerError where appropriate
40 lines
875 B
Go
40 lines
875 B
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/gorilla/handlers"
|
|
"github.com/gorilla/mux"
|
|
)
|
|
|
|
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)
|
|
}
|
|
}
|
|
}
|