blob: 05607866eada8e87d9eb1bc820a4169b313b8189 [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"
Akronce286af2022-12-28 13:58:26 +010012 "github.com/joho/godotenv"
Akrone2161562022-12-19 17:05:39 +010013)
14
15var db *badger.DB
16
17func CheckSaleUrl(c *gin.Context) {
18
19 corpusID := c.Param("corpus_id")
20 docID := c.Param("doc_id")
21 textID := c.Param("text_id")
22
23 err := db.View(func(txn *badger.Txn) error {
24
25 item, err := txn.Get([]byte(corpusID + "/" + docID + "/" + textID))
26
27 if err != nil {
28 c.String(http.StatusNotFound, "No entry found")
Akron906a2c52022-12-21 11:35:28 +010029 return nil
Akrone2161562022-12-19 17:05:39 +010030 }
31
32 err = item.Value(func(v []byte) error {
33 c.String(http.StatusOK, string(v))
34 return nil
35 })
36
37 if err != nil {
Akronce5186d2022-12-21 11:15:50 +010038 return err
Akrone2161562022-12-19 17:05:39 +010039 }
40
41 return nil
42 })
43
44 if err != nil {
45 c.String(http.StatusNotFound, err.Error())
46 }
Akrone2161562022-12-19 17:05:39 +010047}
48
Akron6aee7fe2022-12-20 16:00:54 +010049func add(corpusID, docID, textID string, provider string, url string) error {
Akrone2161562022-12-19 17:05:39 +010050 err := db.Update(func(txn *badger.Txn) error {
Akron6aee7fe2022-12-20 16:00:54 +010051 err := txn.Set([]byte(corpusID+"/"+docID+"/"+textID), []byte(provider+","+url))
Akrone2161562022-12-19 17:05:39 +010052 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
Akronce286af2022-12-28 13:58:26 +010076 korapServer := os.Getenv("KORAP_SERVER")
77 if korapServer == "" {
78 korapServer = "https://korap.ids-mannheim.de"
79 }
80
Akron79d83482023-01-02 10:53:24 +010081 r.Use(func() gin.HandlerFunc {
82 return func(c *gin.Context) {
83 h := c.Writer.Header()
84 h.Set("Access-Control-Allow-Origin", "null")
85 h.Set("Access-Control-Allow-Credentials", "null")
86 h.Set("Vary", "Origin")
87 }
88 }(),
89 )
90
Akron60e11602023-01-02 11:16:43 +010091 // Return widget page
Akron6aee7fe2022-12-20 16:00:54 +010092 r.GET("/", func(c *gin.Context) {
Akron5e1252e2022-12-19 17:57:56 +010093 c.HTML(http.StatusOK, "main.html", gin.H{
Akronce286af2022-12-28 13:58:26 +010094 "korapServer": korapServer,
Akron5e1252e2022-12-19 17:57:56 +010095 })
96 })
97
Akron60e11602023-01-02 11:16:43 +010098 // Return provider information
Akronce5186d2022-12-21 11:15:50 +010099 r.HEAD("/:corpus_id/:doc_id/:text_id", CheckSaleUrl)
Akrone2161562022-12-19 17:05:39 +0100100 r.GET("/:corpus_id/:doc_id/:text_id", CheckSaleUrl)
Akron60e11602023-01-02 11:16:43 +0100101
102 // Return plugin manifest
103 r.StaticFile("/plugin.json", "./assets/plugin.json")
Akrone2161562022-12-19 17:05:39 +0100104 return r
105}
106
107func main() {
Akronce286af2022-12-28 13:58:26 +0100108 godotenv.Load()
109
Akrone2161562022-12-19 17:05:39 +0100110 initDB("db")
Akron3890fcc2022-12-20 15:26:36 +0100111 defer closeDB()
112
113 // Index csv file
114 if len(os.Args) > 1 {
115
116 file, err := os.Open(os.Args[1])
117 if err != nil {
118 log.Fatal(err)
119 }
120 r := csv.NewReader(file)
121
122 txn := db.NewTransaction(true)
123
124 i := 0
125
126 for {
127 record, err := r.Read()
128 if err == io.EOF {
129 break
130 }
131 if err != nil {
132 log.Fatal(err)
133 }
134
135 if err := txn.Set([]byte(record[0]), []byte(record[1]+","+record[2])); err == badger.ErrTxnTooBig {
136 log.Println("Commit", record[0], "after", i, "inserts")
137 i = 0
138 err = txn.Commit()
139 if err != nil {
140 log.Fatal("Unable to commit")
141 }
142 txn = db.NewTransaction(true)
143 _ = txn.Set([]byte(record[0]), []byte(record[1]+","+record[2]))
144 }
145 i++
146 }
147 err = txn.Commit()
148
149 if err != nil {
150 log.Fatal("Unable to commit")
151 }
152
153 return
154 }
Akrone2161562022-12-19 17:05:39 +0100155 r := setupRouter()
Akronce286af2022-12-28 13:58:26 +0100156
157 port := os.Getenv("PORT")
158 if port == "" {
159 port = "5722"
160 }
161
162 log.Fatal(http.ListenAndServe(":"+port, r))
Akrone2161562022-12-19 17:05:39 +0100163}