1
|
|
|
package main |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"flag" |
5
|
|
|
"fmt" |
6
|
|
|
"github.com/arazmj/gerdu/cache" |
7
|
|
|
"github.com/arazmj/gerdu/grpcserver" |
8
|
|
|
"github.com/arazmj/gerdu/httpserver" |
9
|
|
|
"github.com/arazmj/gerdu/lfucache" |
10
|
|
|
"github.com/arazmj/gerdu/lrucache" |
11
|
|
|
"github.com/arazmj/gerdu/weakcache" |
12
|
|
|
"github.com/inhies/go-bytesize" |
13
|
|
|
"os" |
14
|
|
|
"strings" |
15
|
|
|
"sync" |
16
|
|
|
) |
17
|
|
|
|
18
|
|
|
var gerdu cache.UnImplementedCache |
19
|
|
|
var wg = sync.WaitGroup{} |
20
|
|
|
|
21
|
|
|
var ( |
22
|
|
|
verbose = flag.Bool("verbose", false, "verbose logging") |
23
|
|
|
capacityStr = flag.String("capacity", "64MB", |
24
|
|
|
"The size of cache, once cache reached this capacity old values will evicted.\n"+ |
25
|
|
|
"Specify a numerical value followed by one of the following units (not case sensitive)"+ |
26
|
|
|
"\nK or KB: Kilobytes"+ |
27
|
|
|
"\nM or MB: Megabytes"+ |
28
|
|
|
"\nG or GB: Gigabytes"+ |
29
|
|
|
"\nT or TB: Terabytes") |
30
|
|
|
httpPort = flag.Int("httpport", 8080, "the http server port number") |
31
|
|
|
grpcPort = flag.Int("grpcport", 8081, "the grpc server port number") |
32
|
|
|
kind = flag.String("type", "lru", "type of cache, lru or lfu, weak") |
33
|
|
|
protocols = flag.String("protocols", "http", "protocol grpc or http, multiple values can be selected seperated by comma") |
34
|
|
|
tlsKey = flag.String("key", "", "SSL certificate private key") |
35
|
|
|
tlsCert = flag.String("cert", "", "SSL certificate public key") |
36
|
|
|
secure = len(*tlsCert) > 0 && len(*tlsKey) > 0 |
37
|
|
|
) |
38
|
|
|
|
39
|
|
|
func main() { |
40
|
|
|
flag.Parse() |
41
|
|
|
capacity, _ := bytesize.Parse(*capacityStr) |
42
|
|
|
|
43
|
|
|
if strings.ToLower(*kind) == "lru" { |
44
|
|
|
gerdu = lrucache.NewCache(capacity) |
45
|
|
|
} else if strings.ToLower(*kind) == "lfu" { |
46
|
|
|
gerdu = lfucache.NewCache(capacity) |
47
|
|
|
} else if strings.ToLower(*kind) == "weak" { |
48
|
|
|
gerdu = weakcache.NewWeakCache() |
49
|
|
|
} else { |
50
|
|
|
fmt.Println("Invalid value for type") |
51
|
|
|
os.Exit(1) |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
*protocols = strings.ToLower(*protocols) |
55
|
|
|
if strings.Contains(*protocols, "http") { |
56
|
|
|
wg.Add(1) |
57
|
|
|
go func() { |
58
|
|
|
defer wg.Done() |
59
|
|
|
if secure { |
60
|
|
|
httpserver.HttpServeTLS(*httpPort, *tlsCert, *tlsKey, gerdu, *verbose) |
61
|
|
|
} else { |
62
|
|
|
httpserver.HttpServe(*httpPort, gerdu, *verbose) |
63
|
|
|
} |
64
|
|
|
}() |
65
|
|
|
} |
66
|
|
|
if strings.Contains(*protocols, "grpc") { |
67
|
|
|
wg.Add(1) |
68
|
|
|
go func() { |
69
|
|
|
defer wg.Done() |
70
|
|
|
if secure { |
71
|
|
|
grpcserver.GrpcServeTLS(*grpcPort, *tlsCert, *tlsKey, gerdu, *verbose) |
72
|
|
|
} else { |
73
|
|
|
grpcserver.GrpcServe(*grpcPort, gerdu, *verbose) |
74
|
|
|
} |
75
|
|
|
}() |
76
|
|
|
} else { |
77
|
|
|
fmt.Println("Invalid value for protocol") |
78
|
|
|
os.Exit(1) |
79
|
|
|
} |
80
|
|
|
wg.Wait() |
81
|
|
|
} |
82
|
|
|
|