-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
101 lines (87 loc) · 2.22 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
package main
import (
"errors"
"flag"
"fmt"
"github.com/gomodule/redigo/redis"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"log"
"time"
"upstash-redis-local/internal"
)
var Version = "development"
type Cmd struct {
RedisAddr string
Addr string
ApiToken string
}
func (c *Cmd) Validate() error {
if c.ApiToken == "" {
return errors.New("API Token empty")
}
if c.RedisAddr == "" {
return errors.New("redis Addr empty")
}
if c.Addr == "" {
return errors.New("webserver addr empty")
}
return nil
}
func main() {
setupFlags(flag.CommandLine)
redisAddr := flag.String("redis", ":6379", "Local Redis String")
addr := flag.String("addr", ":8000", "Local webserver string")
apiToken := flag.String("token", "upstash", "API token set by user")
help := flag.Bool("help", false, "")
flag.Parse()
cmd := Cmd{
RedisAddr: *redisAddr,
ApiToken: *apiToken,
Addr: *addr,
}
if *help {
printHelp()
return
}
if cmd.Validate() != nil {
}
config := zap.NewProductionConfig()
config.EncoderConfig.EncodeTime = zapcore.TimeEncoderOfLayout(time.RFC3339)
logger, err := config.Build()
if err != nil {
log.Fatalf("err: %v", err)
}
defer logger.Sync()
server := internal.Server{Address: cmd.Addr, APIToken: cmd.ApiToken, RedisConn: connectToRedis(cmd.RedisAddr), Logger: logger}
defer server.Serve()
}
func setupFlags(f *flag.FlagSet) {
f.Usage = func() {
printHelp()
}
}
func printHelp() {
fmt.Printf(`
upstash-redis-local %s
A local server that mimics upstash-redis for local testing purposes!
* Connect to any local redis of your choice for testing
* Comlpetely mimics the upstash REST API https://docs.upstash.com/redis/features/restapi
Developed by Hemanth Krishna (https://github.com/DarthBenro008)
USAGE:
upstash-redis-local
upstash-redis-local --token upstash --addr :8000 --redis :6379
ARGUMENTS:
--token TOKEN The API token to accept as authorised (default: upstash)
--addr ADDR Address for the server to listen on (default: :8000)
--redis ADDR Address to your redids server (default: :6379)
--help Prints this message
`, Version)
}
func connectToRedis(addr string) redis.Conn {
conn, err := redis.Dial("tcp", addr)
if err != nil {
log.Fatalf("err: %v", err)
}
return conn
}