-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
79 lines (70 loc) · 1.8 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
package main
import (
. "cacheserver/storage"
"flag"
"fmt"
"github.com/julienschmidt/httprouter"
"os"
"strconv"
"log"
)
func config() (Storage, int) {
//command-line flags
var storage Storage
st := flag.String("storage", "", "what storage to use. ram|redis|memcached")
port := flag.Int("port", -1, "what port use to listen on")
storage_server_addr := flag.String("server", "", "socket of storage server ip:port")
password := *flag.String("password", "", "password for connection")
flag.Parse()
if *st == "" {
//no command line flag->fetch value from env variable
*st = os.Getenv("CACHESERVER_STORAGE")
}
if *port == -1 {
prt, err := strconv.ParseInt(os.Getenv("CACHESERVER_PORT"), 10, 32)
if err == nil {
*port = int(prt)
} else {
panic("No port specified")
}
}
switch *st {
case "ram":
storage= NewRamStorage()
//storage = tmp
default:
if *storage_server_addr == "" {
*storage_server_addr = os.Getenv("CACHSERVER_STORAGE_SOCKET")
if *storage_server_addr == "" {
panic("No storage socket provided")
}
}
switch *st {
case "redis":
storage= NewRedisStorage(*storage_server_addr, password, 0)
//storage = &tmp
case "memcached":
storage= NewMCStorage(*storage_server_addr)
//storage = &tmp
default:
panic("Wrong or empty storage specified: " + *st)
}
}
fmt.Println(*st)
fmt.Println(*port)
return storage, *port
}
var storage Storage
func main() {
var port int
storage, port = config() //select storage
router := httprouter.New()
router.GET("/api/records/:id", getValue)
router.GET("/api/metrics/:metric", getMetric)
router.POST("/api/records", setValue)
router.PUT("/api/records/:id", updateValue)
router.DELETE("/api/records/:id", deleteValue)
server := NewServer(int32(port), router)
go refresher()
log.Fatal(server.ListenAndServe())
}