-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
121 lines (104 loc) · 2.25 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
package main
import (
"fmt"
"github.com/aspacca/keyvaluestorage/http"
"github.com/aspacca/keyvaluestorage/storage"
"github.com/minio/cli"
)
var version = "0.1"
var helpTemplate = `NAME:
{{.Name}} - {{.Usage}}
DESCRIPTION:
{{.Description}}
USAGE:
{{.Name}} {{if .Flags}}[flags] {{end}}command{{if .Flags}}{{end}} [arguments...]
COMMANDS:
{{range .Commands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}}
{{end}}{{if .Flags}}
FLAGS:
{{range .Flags}}{{.}}
{{end}}{{end}}
VERSION:
` + version +
`{{ "\n"}}`
var globalFlags = []cli.Flag{
cli.StringFlag{
Name: "listener",
Usage: "0.0.0.0:8080",
Value: "0.0.0.0:8080",
},
cli.StringFlag{
Name: "basedir",
Usage: "path to storage",
Value: "",
},
cli.StringFlag{
Name: "provider",
Usage: "fs|memory",
Value: "",
},
}
type cmd struct {
*cli.App
}
func versionAction(c *cli.Context) {
fmt.Println("Key value storage server ver")
}
func newServer() *cmd {
app := cli.NewApp()
app.Name = "Key value storage server"
app.Version = version
app.Author = "Andrea Spacca"
app.Description = "Key value storage server"
app.Flags = globalFlags
app.CustomAppHelpTemplate = helpTemplate
app.Commands = []cli.Command{
{
Name: "version",
Action: versionAction,
},
}
app.Before = func(c *cli.Context) error {
return nil
}
app.Action = func(c *cli.Context) {
options := []http.OptionFn{}
if v := c.String("listener"); v != "" {
options = append(options, http.Listener(v))
}
switch provider := c.String("provider"); provider {
case "fs":
if v := c.String("basedir"); v == "" {
panic("basedir not set.")
} else if storage, err := storage.NewFileSystemStorage(v); err != nil {
panic(err)
} else {
options = append(options, http.UseStorage(storage))
}
case "memory":
if v := c.String("basedir"); v == "" {
panic("basedir not set.")
} else if storage, err := storage.NewMemoryStorage(v); err != nil {
panic(err)
} else {
options = append(options, http.UseStorage(storage))
}
default:
panic("Provider not set or invalid.")
}
s, err := http.New(
options...,
)
if err != nil {
panic(fmt.Sprintf("Error starting server: %s\n", err))
}
s.Run()
}
return &cmd{
App: app,
}
}
func main() {
app := newServer()
app.RunAndExitOnError()
}