/
main.go
80 lines (65 loc) · 2.13 KB
/
main.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
80
package main
import (
"flag"
"fmt"
"log"
"net/http"
"sync"
"github.com/threefoldtech/rivine/modules"
"github.com/threefoldtech/rivine/pkg/api"
"github.com/threefoldtech/rivine/pkg/daemon"
"github.com/threefoldtech/rivine/types"
)
type faucet struct {
// cts is a cached version of daemon constants
// caching here avoids requiring a call to the daemon even if it is local
cts *modules.DaemonConstants
// coinsToGive is the amount of coins given in a single transaction
coinsToGive types.Currency
// lock to protect the fund endpoints. This ensures the wallet
// we talk to only has 1 tx in progress at the same time
mu sync.Mutex
}
var (
websitePort int
httpClient = &api.HTTPClient{
RootURL: "http://localhost:23110",
Password: "",
UserAgent: daemon.RivineUserAgent,
}
coinsToGive uint64 = 300
)
func getDaemonConstants() (*modules.DaemonConstants, error) {
var constants modules.DaemonConstants
err := httpClient.GetWithResponse("/daemon/constants", &constants)
if err != nil {
return nil, err
}
return &constants, nil
}
func main() {
log.Println("[INFO] Starting faucet")
log.Println("[INFO] Loading daemon constants")
cts, err := getDaemonConstants()
if err != nil {
panic(err)
}
f := faucet{
cts: cts,
coinsToGive: cts.OneCoin.Mul64(coinsToGive),
}
log.Println("[INFO] Faucet listening on port", websitePort)
http.HandleFunc("/", f.requestFormHandler)
http.HandleFunc("/request/tokens", f.requestTokensHandler)
// register API endpoint
http.HandleFunc("/api/v1/coins", f.requestCoins)
log.Println("[INFO] Faucet ready to serve")
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", websitePort), nil))
}
func init() {
flag.IntVar(&websitePort, "port", 2020, "local port to expose this web faucet on")
flag.StringVar(&httpClient.Password, "daemon-password", httpClient.Password, "optional password, should the used daemon require it")
flag.StringVar(&httpClient.RootURL, "daemon-address", httpClient.RootURL, "address of the daemon (with unlocked wallet) to talk to")
flag.Uint64Var(&coinsToGive, "fund-amount", coinsToGive, "amount of coins to give per drip of the faucet")
flag.Parse()
}