This repository has been archived by the owner on Sep 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.go
79 lines (72 loc) · 2.33 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"context"
"fmt"
"net/http"
"time"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/kaspanet/faucet/config"
"github.com/kaspanet/faucet/httpserverutils"
"github.com/kaspanet/kaspad/util"
"github.com/pkg/errors"
)
const gracefulShutdownTimeout = 30 * time.Second
// startHTTPServer starts the HTTP REST server and returns a
// function to gracefully shutdown it.
func startHTTPServer(listenAddr string) func() {
router := mux.NewRouter()
router.Use(httpserverutils.AddRequestMetadataMiddleware)
router.Use(httpserverutils.RecoveryMiddleware)
router.Use(httpserverutils.LoggingMiddleware)
router.Use(httpserverutils.SetJSONMiddleware)
router.HandleFunc(
"/request_money",
httpserverutils.MakeHandler(requestMoneyHandler)).
Methods("GET")
httpServer := &http.Server{
Addr: listenAddr,
Handler: handlers.CORS()(router),
}
spawn("startHTTPServer-httpServer.ListenAndServe", 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)
}
}
}
func requestMoneyHandler(_ *httpserverutils.ServerContext, request *http.Request,
_ map[string]string, queryParams map[string]string, _ []byte) (interface{}, error) {
hErr := validateIPUsage(request)
if hErr != nil {
return nil, hErr
}
addressString, ok := queryParams["address"]
if !ok {
return nil, httpserverutils.NewHandlerErrorWithCustomClientMessage(http.StatusUnprocessableEntity,
errors.Errorf("address not found"),
"The address parameter is either missing or empty")
}
address, err := util.DecodeAddress(addressString, config.ActiveNetParams().Prefix)
if err != nil {
return nil, httpserverutils.NewHandlerErrorWithCustomClientMessage(http.StatusUnprocessableEntity,
errors.Wrap(err, "Error decoding address"),
"Error decoding address")
}
transactionID, err := sendToAddress(address)
if err != nil {
return nil, httpserverutils.NewHandlerErrorWithCustomClientMessage(http.StatusUnprocessableEntity,
errors.Wrap(err, "Error sending to address"),
fmt.Sprintf("Error sending Kaspa: %s", err))
}
hErr = updateIPUsage(request)
if hErr != nil {
return nil, hErr
}
return transactionID, nil
}