blob: e1ac40dd9484cb4abaf948138d13e77dd58197da [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
Akronce5186d2022-12-21 11:15:50 +010022 c.Header("Access-Control-Allow-Origin", "null")
23 c.Header("Access-Control-Allow-Credentials", "null")
24 c.Header("Vary", "Origin")
Akronf1adbe02022-12-21 10:32:21 +010025
Akrone2161562022-12-19 17:05:39 +010026 err := db.View(func(txn *badger.Txn) error {
27
28 item, err := txn.Get([]byte(corpusID + "/" + docID + "/" + textID))
29
30 if err != nil {
31 c.String(http.StatusNotFound, "No entry found")
32 }
33
34 err = item.Value(func(v []byte) error {
35 c.String(http.StatusOK, string(v))
36 return nil
37 })
38
39 if err != nil {
Akronce5186d2022-12-21 11:15:50 +010040 return err
Akrone2161562022-12-19 17:05:39 +010041 }
42
43 return nil
44 })
45
46 if err != nil {
47 c.String(http.StatusNotFound, err.Error())
48 }
Akrone2161562022-12-19 17:05:39 +010049}
50
Akron6aee7fe2022-12-20 16:00:54 +010051func add(corpusID, docID, textID string, provider string, url string) error {
Akrone2161562022-12-19 17:05:39 +010052 err := db.Update(func(txn *badger.Txn) error {
Akron6aee7fe2022-12-20 16:00:54 +010053 err := txn.Set([]byte(corpusID+"/"+docID+"/"+textID), []byte(provider+","+url))
Akrone2161562022-12-19 17:05:39 +010054 return err
55 })
56 return err
57}
58
59func initDB(dir string) {
60 if db != nil {
61 return
62 }
63 var err error
64 db, err = badger.Open(badger.DefaultOptions(dir))
65 if err != nil {
66 log.Fatal(err)
67 }
68}
69
70func closeDB() {
71 db.Close()
72}
73
74func setupRouter() *gin.Engine {
75 r := gin.Default()
Akron5e1252e2022-12-19 17:57:56 +010076 r.LoadHTMLGlob("templates/*")
77
Akron3890fcc2022-12-20 15:26:36 +010078 //
Akron6aee7fe2022-12-20 16:00:54 +010079 r.GET("/", func(c *gin.Context) {
Akronce5186d2022-12-21 11:15:50 +010080 c.Header("Access-Control-Allow-Origin", "null")
81 c.Header("Access-Control-Allow-Credentials", "null")
82 c.Header("Vary", "Origin")
Akron5e1252e2022-12-19 17:57:56 +010083 c.HTML(http.StatusOK, "main.html", gin.H{
Akron6aee7fe2022-12-20 16:00:54 +010084 "korapServer": "https://korap.ids-mannheim.de/instance/test",
Akron5e1252e2022-12-19 17:57:56 +010085 })
86 })
87
Akronce5186d2022-12-21 11:15:50 +010088 r.HEAD("/:corpus_id/:doc_id/:text_id", CheckSaleUrl)
Akrone2161562022-12-19 17:05:39 +010089 r.GET("/:corpus_id/:doc_id/:text_id", CheckSaleUrl)
Akron5e1252e2022-12-19 17:57:56 +010090 r.Static("/assets", "./assets")
Akrone2161562022-12-19 17:05:39 +010091 return r
92}
93
94func main() {
95 initDB("db")
Akron3890fcc2022-12-20 15:26:36 +010096 defer closeDB()
97
98 // Index csv file
99 if len(os.Args) > 1 {
100
101 file, err := os.Open(os.Args[1])
102 if err != nil {
103 log.Fatal(err)
104 }
105 r := csv.NewReader(file)
106
107 txn := db.NewTransaction(true)
108
109 i := 0
110
111 for {
112 record, err := r.Read()
113 if err == io.EOF {
114 break
115 }
116 if err != nil {
117 log.Fatal(err)
118 }
119
120 if err := txn.Set([]byte(record[0]), []byte(record[1]+","+record[2])); err == badger.ErrTxnTooBig {
121 log.Println("Commit", record[0], "after", i, "inserts")
122 i = 0
123 err = txn.Commit()
124 if err != nil {
125 log.Fatal("Unable to commit")
126 }
127 txn = db.NewTransaction(true)
128 _ = txn.Set([]byte(record[0]), []byte(record[1]+","+record[2]))
129 }
130 i++
131 }
132 err = txn.Commit()
133
134 if err != nil {
135 log.Fatal("Unable to commit")
136 }
137
138 return
139 }
Akrone2161562022-12-19 17:05:39 +0100140 r := setupRouter()
Akron0adfd5c2022-12-20 16:02:25 +0100141 log.Fatal(http.ListenAndServe(":5722", r))
Akrone2161562022-12-19 17:05:39 +0100142}