blob: 130b1f4a2c0350fea6e6cb562896cc4cd1e4b1f6 [file] [log] [blame]
Akrone2161562022-12-19 17:05:39 +01001package main
2
3import (
Akron3890fcc2022-12-20 15:26:36 +01004 "encoding/csv"
5 "io"
Akrone2161562022-12-19 17:05:39 +01006 "log"
7 "net/http"
Akron3890fcc2022-12-20 15:26:36 +01008 "os"
Akrone2161562022-12-19 17:05:39 +01009
10 badger "github.com/dgraph-io/badger/v3"
11 "github.com/gin-gonic/gin"
12)
13
14var db *badger.DB
15
16func CheckSaleUrl(c *gin.Context) {
17
18 corpusID := c.Param("corpus_id")
19 docID := c.Param("doc_id")
20 textID := c.Param("text_id")
21
22 err := db.View(func(txn *badger.Txn) error {
23
24 item, err := txn.Get([]byte(corpusID + "/" + docID + "/" + textID))
25
26 if err != nil {
27 c.String(http.StatusNotFound, "No entry found")
28 }
29
30 err = item.Value(func(v []byte) error {
31 c.String(http.StatusOK, string(v))
32 return nil
33 })
34
35 if err != nil {
36 c.String(http.StatusNotFound, err.Error())
37 }
38
39 return nil
40 })
41
42 if err != nil {
43 c.String(http.StatusNotFound, err.Error())
44 }
45
46 c.String(http.StatusExpectationFailed, err.Error())
47}
48
49func add(corpusID, docID, textID string, url string) error {
50 err := db.Update(func(txn *badger.Txn) error {
51 err := txn.Set([]byte(corpusID+"/"+docID+"/"+textID), []byte(url))
52 return err
53 })
54 return err
55}
56
57func initDB(dir string) {
58 if db != nil {
59 return
60 }
61 var err error
62 db, err = badger.Open(badger.DefaultOptions(dir))
63 if err != nil {
64 log.Fatal(err)
65 }
66}
67
68func closeDB() {
69 db.Close()
70}
71
72func setupRouter() *gin.Engine {
73 r := gin.Default()
Akron5e1252e2022-12-19 17:57:56 +010074 r.LoadHTMLGlob("templates/*")
75
Akron3890fcc2022-12-20 15:26:36 +010076 //
Akron5e1252e2022-12-19 17:57:56 +010077 r.GET("/index", func(c *gin.Context) {
78 c.HTML(http.StatusOK, "main.html", gin.H{
Akron5e1252e2022-12-19 17:57:56 +010079 "korapServer": "https://korap.ids-mannheim.de/",
80 })
81 })
82
Akrone2161562022-12-19 17:05:39 +010083 r.GET("/:corpus_id/:doc_id/:text_id", CheckSaleUrl)
Akron5e1252e2022-12-19 17:57:56 +010084 r.Static("/assets", "./assets")
Akrone2161562022-12-19 17:05:39 +010085 return r
86}
87
88func main() {
89 initDB("db")
Akron3890fcc2022-12-20 15:26:36 +010090 defer closeDB()
91
92 // Index csv file
93 if len(os.Args) > 1 {
94
95 file, err := os.Open(os.Args[1])
96 if err != nil {
97 log.Fatal(err)
98 }
99 r := csv.NewReader(file)
100
101 txn := db.NewTransaction(true)
102
103 i := 0
104
105 for {
106 record, err := r.Read()
107 if err == io.EOF {
108 break
109 }
110 if err != nil {
111 log.Fatal(err)
112 }
113
114 if err := txn.Set([]byte(record[0]), []byte(record[1]+","+record[2])); err == badger.ErrTxnTooBig {
115 log.Println("Commit", record[0], "after", i, "inserts")
116 i = 0
117 err = txn.Commit()
118 if err != nil {
119 log.Fatal("Unable to commit")
120 }
121 txn = db.NewTransaction(true)
122 _ = txn.Set([]byte(record[0]), []byte(record[1]+","+record[2]))
123 }
124 i++
125 }
126 err = txn.Commit()
127
128 if err != nil {
129 log.Fatal("Unable to commit")
130 }
131
132 return
133 }
Akrone2161562022-12-19 17:05:39 +0100134 r := setupRouter()
135 r.Run(":8080")
Akrone2161562022-12-19 17:05:39 +0100136}