-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
144 lines (123 loc) · 2.98 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Package main serves as a user interface to the underlying index and api packages
package main
import (
"os"
"os/signal"
"syscall"
"github.com/jackyzha0/nanoDB/api"
"github.com/jackyzha0/nanoDB/index"
"github.com/jackyzha0/nanoDB/log"
"fmt"
"net/http"
"github.com/julienschmidt/httprouter"
"github.com/urfave/cli/v2"
)
func main() {
app := &cli.App{
Name: "nanodb",
Usage: "a simple, easy, and debuggable document database for prototyping and hackathons",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "dir",
Aliases: []string{"d"},
Value: "db",
Usage: "directory to look for keys",
DefaultText: "db",
},
},
Commands: []*cli.Command{
{
Name: "start",
Aliases: []string{"st"},
Usage: "start a nanodb server",
Flags: []cli.Flag{
&cli.IntFlag{
Name: "port",
Aliases: []string{"p"},
Value: 3000,
Usage: "port to run nanodb on",
DefaultText: "3000",
},
},
Action: func(c *cli.Context) error {
return serve(c.Int("port"), c.String("dir"))
},
}, {
Name: "shell",
Aliases: []string{"sh"},
Usage: "start an interactive nanodb shell",
Action: func(c *cli.Context) error {
return shell(c.String("dir"))
},
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
// serve defines all the endpoints and starts a new http server on :3000
func serve(port int, dir string) error {
log.SetLoggingLevel(log.INFO)
log.Info("initializing nanoDB")
setup(dir)
router := httprouter.New()
// define endpoints
router.GET("/", api.GetIndex)
router.POST("/", api.RegenerateIndex)
router.GET("/:key", api.GetKey)
router.GET("/:key/:field", api.GetKeyField)
router.PUT("/:key", api.UpdateKey)
router.DELETE("/:key", api.DeleteKey)
router.PATCH("/:key/:field", api.PatchKeyField)
// start server
log.Info("starting api server on port %d", port)
return http.ListenAndServe(fmt.Sprintf(":%d", port), router)
}
func getLockLocation(dir string) string {
base := "nanodb_lock"
if dir == "" || dir == "." {
return base
}
return dir + "/" + base
}
func acquireLock(dir string) error {
_, err := index.I.FileSystem.Stat(getLockLocation(dir))
if os.IsNotExist(err) {
_, err = index.I.FileSystem.Create(getLockLocation(dir))
return err
}
return fmt.Errorf("couldn't acquire lock on %s", dir)
}
func releaseLock(dir string) error {
lockdir := getLockLocation(dir)
return index.I.FileSystem.Remove(lockdir)
}
func setup(dir string) {
index.I = index.NewFileIndex(dir)
// create nanodb lock
err := acquireLock(dir)
if err != nil {
log.Fatal(err)
return
}
index.I.Regenerate()
// trap sigint
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
cleanup(dir)
os.Exit(1)
}()
}
func cleanup(dir string) {
log.Info("\ncaught term signal! cleaning up...")
err := releaseLock(dir)
if err != nil {
log.Warn("couldn't remove lock")
log.Fatal(err)
return
}
}