blob: 3ed40e799ae4a349255d04c58d7c9b512d4eced2 [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")
Akron906a2c52022-12-21 11:35:28 +010032 return nil
Akrone2161562022-12-19 17:05:39 +010033 }
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 {
Akronce5186d2022-12-21 11:15:50 +010041 return err
Akrone2161562022-12-19 17:05:39 +010042 }
43
44 return nil
45 })
46
47 if err != nil {
48 c.String(http.StatusNotFound, err.Error())
49 }
Akrone2161562022-12-19 17:05:39 +010050}
51
Akron6aee7fe2022-12-20 16:00:54 +010052func add(corpusID, docID, textID string, provider string, url string) error {
Akrone2161562022-12-19 17:05:39 +010053 err := db.Update(func(txn *badger.Txn) error {
Akron6aee7fe2022-12-20 16:00:54 +010054 err := txn.Set([]byte(corpusID+"/"+docID+"/"+textID), []byte(provider+","+url))
Akrone2161562022-12-19 17:05:39 +010055 return err
56 })
57 return err
58}
59
60func initDB(dir string) {
61 if db != nil {
62 return
63 }
64 var err error
65 db, err = badger.Open(badger.DefaultOptions(dir))
66 if err != nil {
67 log.Fatal(err)
68 }
69}
70
71func closeDB() {
72 db.Close()
73}
74
75func setupRouter() *gin.Engine {
76 r := gin.Default()
Akron5e1252e2022-12-19 17:57:56 +010077 r.LoadHTMLGlob("templates/*")
78
Akron3890fcc2022-12-20 15:26:36 +010079 //
Akron6aee7fe2022-12-20 16:00:54 +010080 r.GET("/", func(c *gin.Context) {
Akronce5186d2022-12-21 11:15:50 +010081 c.Header("Access-Control-Allow-Origin", "null")
82 c.Header("Access-Control-Allow-Credentials", "null")
83 c.Header("Vary", "Origin")
Akron5e1252e2022-12-19 17:57:56 +010084 c.HTML(http.StatusOK, "main.html", gin.H{
Akron6aee7fe2022-12-20 16:00:54 +010085 "korapServer": "https://korap.ids-mannheim.de/instance/test",
Akron5e1252e2022-12-19 17:57:56 +010086 })
87 })
88
Akronce5186d2022-12-21 11:15:50 +010089 r.HEAD("/:corpus_id/:doc_id/:text_id", CheckSaleUrl)
Akrone2161562022-12-19 17:05:39 +010090 r.GET("/:corpus_id/:doc_id/:text_id", CheckSaleUrl)
Akron5e1252e2022-12-19 17:57:56 +010091 r.Static("/assets", "./assets")
Akrone2161562022-12-19 17:05:39 +010092 return r
93}
94
95func main() {
96 initDB("db")
Akron3890fcc2022-12-20 15:26:36 +010097 defer closeDB()
98
99 // Index csv file
100 if len(os.Args) > 1 {
101
102 file, err := os.Open(os.Args[1])
103 if err != nil {
104 log.Fatal(err)
105 }
106 r := csv.NewReader(file)
107
108 txn := db.NewTransaction(true)
109
110 i := 0
111
112 for {
113 record, err := r.Read()
114 if err == io.EOF {
115 break
116 }
117 if err != nil {
118 log.Fatal(err)
119 }
120
121 if err := txn.Set([]byte(record[0]), []byte(record[1]+","+record[2])); err == badger.ErrTxnTooBig {
122 log.Println("Commit", record[0], "after", i, "inserts")
123 i = 0
124 err = txn.Commit()
125 if err != nil {
126 log.Fatal("Unable to commit")
127 }
128 txn = db.NewTransaction(true)
129 _ = txn.Set([]byte(record[0]), []byte(record[1]+","+record[2]))
130 }
131 i++
132 }
133 err = txn.Commit()
134
135 if err != nil {
136 log.Fatal("Unable to commit")
137 }
138
139 return
140 }
Akrone2161562022-12-19 17:05:39 +0100141 r := setupRouter()
Akron0adfd5c2022-12-20 16:02:25 +0100142 log.Fatal(http.ListenAndServe(":5722", r))
Akrone2161562022-12-19 17:05:39 +0100143}