-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
122 lines (106 loc) · 3.11 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"context"
"fmt"
"github.com/gin-gonic/gin"
"github.com/go-redsync/redsync/v4"
"github.com/go-redsync/redsync/v4/redis/goredis/v8"
"github.com/joho/godotenv"
"github.com/urfave/cli/v2"
"github.com/go-redis/redis/v8"
"log"
"os"
)
func main() {
// Recovering after panics, trying to keep the turbo-ledger alive
defer func() {
if err := recover(); err != nil {
log.Printf("Unable to recover from panic in main - reason: %e", err)
}
}()
errEnv := godotenv.Load()
if errEnv != nil {
fmt.Println("Could not load .env File, getting values from environment...")
}
app := &cli.App{
Name: "turbo-ledger - a scalable ledger",
Usage: "Provides safe horizontally-scalable ledgers based on Redis",
Version: "naive-version",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "redisHost",
Aliases: []string{"rh"},
Usage: "Redis Host",
EnvVars: []string{"TURBO_LEDGER_REDIS_HOST"},
},
&cli.StringFlag{
Name: "redisUser",
Aliases: []string{"ru"},
Usage: "Redis User",
EnvVars: []string{"TURBO_LEDGER_REDIS_USER"},
},
&cli.StringFlag{
Name: "redisPassword",
Aliases: []string{"rp"},
Usage: "Redis Password",
EnvVars: []string{"TURBO_LEDGER_REDIS_PASSWORD"},
},
&cli.StringFlag{
Name: "listenAddress",
Aliases: []string{"la"},
Usage: "Listen address",
Value: ":12000",
EnvVars: []string{"TURBO_LEDGER_LISTEN_ADDRESS"},
},
},
Action: func(c *cli.Context) error {
// Connecting to Redis...
redisHost := c.String("redisHost")
redisUser := c.String("redisUser")
redisPassword := c.String("redisPassword")
listenAddress := c.String("listenAddress")
rdb, pong, err := connectToRedis(redisHost, redisUser, redisPassword)
if err != nil {
return err
}
log.Printf("Successfully connected to Redis %s", pong)
// Preparing redsync instance
pool := goredis.NewPool(rdb)
rs := redsync.New(pool)
mutexname := "global-wallets-mutex"
mutex := rs.NewMutex(mutexname)
// Running the Genesis process, the vault account
errGenesis := Genesis(rdb, "vault", 500)
if errGenesis != nil {
return errGenesis
}
// Running the API
router := gin.Default()
router.POST("wallets", PostWallet(rdb))
router.POST("transactions", PostTransaction(rdb, mutex))
router.GET("wallets/owner", SearchWallets(rdb, "idx:wallet:owner", `'@owner:(%s)'`))
router.GET("wallets/tags", SearchWallets(rdb, "idx:wallet:tags", `'@tags:{%s}'`))
router.Run(listenAddress)
return nil
},
}
err := app.Run(os.Args)
if err != nil {
log.Panic(err)
}
}
func connectToRedis(redisHost string, redisUser string, redisPassword string) (*redis.Client, string, error) {
ctx := context.Background()
log.Printf("Connecting to Redis on Host %s with User %s ... ", redisHost, redisUser)
rdb := redis.NewClient(&redis.Options{
Addr: redisHost,
Username: redisUser,
Password: redisPassword, // no password set
DB: 0, // use default DB
})
pong, errRedis := rdb.Ping(ctx).Result()
if errRedis != nil {
return nil, "", errRedis
}
return rdb, pong, nil
}