This repository has been archived by the owner on Jul 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
92 lines (78 loc) · 2.07 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
package main
import (
"fmt"
"github.com/gofiber/fiber/v2"
"whydb/config"
"whydb/filestore"
"whydb/postgresstore"
"whydb/types"
)
func getStore() types.Store {
switch config.StoreType {
case "file":
return filestore.NewFileStore()
case "postgres":
return postgresstore.NewPostgresStore()
default:
panic("invalid StoreType in config, please set a valid StoreType")
}
}
func start(domain string, port int) {
app := fiber.New()
store := getStore()
app.Get("/set/:cat/:key/*", func(c *fiber.Ctx) error {
cat, key, data := c.Params("cat"), c.Params("key"), c.Params("*")
err := store.Set(cat, key, data)
if err != nil {
return c.SendString(err.Error())
}
return c.SendString("done")
})
app.Post("/set/:cat/:key/", func(c *fiber.Ctx) error {
cat, key := c.Params("cat"), c.Params("key")
err := store.Set(cat, key, string(c.Body()))
if err != nil {
return c.SendString(err.Error())
}
return c.SendString("done")
})
app.Get("/get/:cat/:key/", func(c *fiber.Ctx) error {
cat, key := c.Params("cat"), c.Params("key")
text, err := store.Get(cat, key)
if err != nil {
return c.SendString(err.Error())
}
return c.SendString(string(text))
})
app.Get("/add/:cat/:key/*", func(c *fiber.Ctx) error {
cat, key, data := c.Params("cat"), c.Params("key"), c.Params("*")
err := store.Add(cat, key, data)
if err != nil {
return c.SendString(err.Error())
}
return c.SendString("done")
})
app.Post("/add/:cat/:key/", func(c *fiber.Ctx) error {
cat, key := c.Params("cat"), c.Params("key")
err := store.Add(cat, key, string(c.Body()))
if err != nil {
return c.SendString(err.Error())
}
return c.SendString("done")
})
app.Get("/del/:cat/:key/", func(c *fiber.Ctx) error {
cat, key := c.Params("cat"), c.Params("key")
err := store.Del(cat, key)
if err != nil {
return c.SendString(err.Error())
}
return c.SendString("done")
})
app.Get("/exists", func(c *fiber.Ctx) error {
return c.SendString("exists - why?db 0.2")
})
_ = app.Listen(domain + ":" + fmt.Sprint(port))
}
func main() {
start(config.Address, config.Port)
}