-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
145 lines (113 loc) · 2.85 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
145
package main
import (
badger "github.com/dgraph-io/badger/v3"
"github.com/gofiber/fiber/v2"
"github.com/joho/godotenv"
"log"
"os"
)
func main() {
loadError := godotenv.Load()
if loadError != nil {
log.Fatal("Error loading .env file")
}
db, openError := badger.Open(badger.DefaultOptions(os.Getenv("BADGER_DB_DATA_PATH")))
if openError != nil {
log.Fatal(openError)
}
defer db.Close()
app := fiber.New()
app.Get("/", func(c *fiber.Ctx) error {
return c.JSON(fiber.Map{
"service": "badger-db",
"context": os.Getenv("BADGER_DB_CONTEXT"),
"version": "0.0.1",
"database": fiber.Map{
"storage": "data/badger",
"version": "v3.2103.5",
},
})
})
app.Head("/items/:key", func(c *fiber.Ctx) error {
viewError := db.View(func(txn *badger.Txn) error {
_, getError := txn.Get([]byte(c.Params("key")))
if getError != nil {
return getError
}
return nil
})
if viewError != nil {
if viewError == badger.ErrKeyNotFound {
return c.Status(fiber.StatusNotFound).Send(nil)
} else {
return viewError
}
}
return c.Status(fiber.StatusNoContent).Send(nil)
})
app.Get("/items/:key", func(c *fiber.Ctx) error {
var value []byte
viewError := db.View(func(txn *badger.Txn) error {
item, getError := txn.Get([]byte(c.Params("key")))
if getError != nil {
return getError
}
var valueCopyError error
value, valueCopyError = item.ValueCopy(nil)
if valueCopyError != nil {
return valueCopyError
}
return nil
})
if viewError != nil {
if viewError == badger.ErrKeyNotFound {
return c.Status(fiber.StatusNotFound).Send(nil)
} else {
return viewError
}
}
return c.SendString(string(value))
})
app.Put("/items/:key", func(c *fiber.Ctx) error {
exists := true
updateError := db.Update(func(txn *badger.Txn) error {
_, getError := txn.Get([]byte(c.Params("key")))
if getError == badger.ErrKeyNotFound {
exists = false
} else if getError != nil {
return getError
}
return txn.Set([]byte(c.Params("key")), c.Body())
})
if updateError != nil {
return updateError
}
if exists {
return c.Status(fiber.StatusNoContent).Send(nil)
} else {
return c.Status(fiber.StatusCreated).Send(nil)
}
})
app.Delete("/items/:key", func(c *fiber.Ctx) error {
updateError := db.Update(func(txn *badger.Txn) error {
_, getError := txn.Get([]byte(c.Params("key")))
if getError != nil {
return getError
}
deleteError := txn.Delete([]byte(c.Params("key")))
if deleteError != nil {
return deleteError
}
return nil
})
if updateError != nil {
if updateError == badger.ErrKeyNotFound {
return c.Status(fiber.StatusNotFound).Send(nil)
} else {
return updateError
}
}
return c.Status(fiber.StatusNoContent).Send(nil)
})
app.Listen(os.Getenv("BADGER_DB_HOST") + ":" + os.Getenv("BADGER_DB_PORT"))
}