blob: 3230b10d0829280d20a2c6f2be0133e222a71a32 [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
Akronf1adbe02022-12-21 10:32:21 +010022 /*
23 c.Header("Access-Control-Allow-Origin", "null")
24 c.Header("Access-Control-Allow-Credentials", "null")
25 c.Header("Vary", "Origin")*/
26
Akrone2161562022-12-19 17:05:39 +010027 err := db.View(func(txn *badger.Txn) error {
28
29 item, err := txn.Get([]byte(corpusID + "/" + docID + "/" + textID))
30
31 if err != nil {
32 c.String(http.StatusNotFound, "No entry found")
33 }
34
35 err = item.Value(func(v []byte) error {
36 c.String(http.StatusOK, string(v))
37 return nil
38 })
39
40 if err != nil {
41 c.String(http.StatusNotFound, err.Error())
42 }
43
44 return nil
45 })
46
47 if err != nil {
48 c.String(http.StatusNotFound, err.Error())
49 }
50
51 c.String(http.StatusExpectationFailed, err.Error())
52}
53
Akron6aee7fe2022-12-20 16:00:54 +010054func add(corpusID, docID, textID string, provider string, url string) error {
Akrone2161562022-12-19 17:05:39 +010055 err := db.Update(func(txn *badger.Txn) error {
Akron6aee7fe2022-12-20 16:00:54 +010056 err := txn.Set([]byte(corpusID+"/"+docID+"/"+textID), []byte(provider+","+url))
Akrone2161562022-12-19 17:05:39 +010057 return err
58 })
59 return err
60}
61
62func initDB(dir string) {
63 if db != nil {
64 return
65 }
66 var err error
67 db, err = badger.Open(badger.DefaultOptions(dir))
68 if err != nil {
69 log.Fatal(err)
70 }
71}
72
73func closeDB() {
74 db.Close()
75}
76
77func setupRouter() *gin.Engine {
78 r := gin.Default()
Akron5e1252e2022-12-19 17:57:56 +010079 r.LoadHTMLGlob("templates/*")
80
Akron3890fcc2022-12-20 15:26:36 +010081 //
Akron6aee7fe2022-12-20 16:00:54 +010082 r.GET("/", func(c *gin.Context) {
Akronf1adbe02022-12-21 10:32:21 +010083 /*
84 c.Header("Access-Control-Allow-Origin", "null")
85 c.Header("Access-Control-Allow-Credentials", "null")
86 c.Header("Vary", "Origin")
87 */
Akron5e1252e2022-12-19 17:57:56 +010088 c.HTML(http.StatusOK, "main.html", gin.H{
Akron6aee7fe2022-12-20 16:00:54 +010089 "korapServer": "https://korap.ids-mannheim.de/instance/test",
Akron5e1252e2022-12-19 17:57:56 +010090 })
91 })
92
Akrone2161562022-12-19 17:05:39 +010093 r.GET("/:corpus_id/:doc_id/:text_id", CheckSaleUrl)
Akron5e1252e2022-12-19 17:57:56 +010094 r.Static("/assets", "./assets")
Akrone2161562022-12-19 17:05:39 +010095 return r
96}
97
98func main() {
99 initDB("db")
Akron3890fcc2022-12-20 15:26:36 +0100100 defer closeDB()
101
102 // Index csv file
103 if len(os.Args) > 1 {
104
105 file, err := os.Open(os.Args[1])
106 if err != nil {
107 log.Fatal(err)
108 }
109 r := csv.NewReader(file)
110
111 txn := db.NewTransaction(true)
112
113 i := 0
114
115 for {
116 record, err := r.Read()
117 if err == io.EOF {
118 break
119 }
120 if err != nil {
121 log.Fatal(err)
122 }
123
124 if err := txn.Set([]byte(record[0]), []byte(record[1]+","+record[2])); err == badger.ErrTxnTooBig {
125 log.Println("Commit", record[0], "after", i, "inserts")
126 i = 0
127 err = txn.Commit()
128 if err != nil {
129 log.Fatal("Unable to commit")
130 }
131 txn = db.NewTransaction(true)
132 _ = txn.Set([]byte(record[0]), []byte(record[1]+","+record[2]))
133 }
134 i++
135 }
136 err = txn.Commit()
137
138 if err != nil {
139 log.Fatal("Unable to commit")
140 }
141
142 return
143 }
Akrone2161562022-12-19 17:05:39 +0100144 r := setupRouter()
Akron0adfd5c2022-12-20 16:02:25 +0100145 log.Fatal(http.ListenAndServe(":5722", r))
Akrone2161562022-12-19 17:05:39 +0100146}