blob: ba009b8fa2e6a8b7932a2e0f01c97fd516befcab [file] [log] [blame]
Akrone2161562022-12-19 17:05:39 +01001package main
2
3import (
4 "log"
5 "net/http"
6
7 badger "github.com/dgraph-io/badger/v3"
8 "github.com/gin-gonic/gin"
9)
10
11var db *badger.DB
12
13func CheckSaleUrl(c *gin.Context) {
14
15 corpusID := c.Param("corpus_id")
16 docID := c.Param("doc_id")
17 textID := c.Param("text_id")
18
19 err := db.View(func(txn *badger.Txn) error {
20
21 item, err := txn.Get([]byte(corpusID + "/" + docID + "/" + textID))
22
23 if err != nil {
24 c.String(http.StatusNotFound, "No entry found")
25 }
26
27 err = item.Value(func(v []byte) error {
28 c.String(http.StatusOK, string(v))
29 return nil
30 })
31
32 if err != nil {
33 c.String(http.StatusNotFound, err.Error())
34 }
35
36 return nil
37 })
38
39 if err != nil {
40 c.String(http.StatusNotFound, err.Error())
41 }
42
43 c.String(http.StatusExpectationFailed, err.Error())
44}
45
46func add(corpusID, docID, textID string, url string) error {
47 err := db.Update(func(txn *badger.Txn) error {
48 err := txn.Set([]byte(corpusID+"/"+docID+"/"+textID), []byte(url))
49 return err
50 })
51 return err
52}
53
54func initDB(dir string) {
55 if db != nil {
56 return
57 }
58 var err error
59 db, err = badger.Open(badger.DefaultOptions(dir))
60 if err != nil {
61 log.Fatal(err)
62 }
63}
64
65func closeDB() {
66 db.Close()
67}
68
69func setupRouter() *gin.Engine {
70 r := gin.Default()
71 r.GET("/:corpus_id/:doc_id/:text_id", CheckSaleUrl)
72 return r
73}
74
75func main() {
76 initDB("db")
77 r := setupRouter()
78 r.Run(":8080")
79 defer closeDB()
80}