blob: 6436ddd72c122ad7c75b7a98d3a74e7cc2fe3b51 [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
Akronce5186d2022-12-21 11:15:50 +010023 c.Header("Access-Control-Allow-Origin", "null")
24 c.Header("Access-Control-Allow-Credentials", "null")
25 c.Header("Vary", "Origin")
Akronf1adbe02022-12-21 10:32:21 +010026
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")
Akron906a2c52022-12-21 11:35:28 +010033 return nil
Akrone2161562022-12-19 17:05:39 +010034 }
35
36 err = item.Value(func(v []byte) error {
37 c.String(http.StatusOK, string(v))
38 return nil
39 })
40
41 if err != nil {
Akronce5186d2022-12-21 11:15:50 +010042 return err
Akrone2161562022-12-19 17:05:39 +010043 }
44
45 return nil
46 })
47
48 if err != nil {
49 c.String(http.StatusNotFound, err.Error())
50 }
Akrone2161562022-12-19 17:05:39 +010051}
52
Akron6aee7fe2022-12-20 16:00:54 +010053func add(corpusID, docID, textID string, provider string, url string) error {
Akrone2161562022-12-19 17:05:39 +010054 err := db.Update(func(txn *badger.Txn) error {
Akron6aee7fe2022-12-20 16:00:54 +010055 err := txn.Set([]byte(corpusID+"/"+docID+"/"+textID), []byte(provider+","+url))
Akrone2161562022-12-19 17:05:39 +010056 return err
57 })
58 return err
59}
60
61func initDB(dir string) {
62 if db != nil {
63 return
64 }
65 var err error
66 db, err = badger.Open(badger.DefaultOptions(dir))
67 if err != nil {
68 log.Fatal(err)
69 }
70}
71
72func closeDB() {
73 db.Close()
74}
75
76func setupRouter() *gin.Engine {
77 r := gin.Default()
Akron5e1252e2022-12-19 17:57:56 +010078 r.LoadHTMLGlob("templates/*")
79
Akronce286af2022-12-28 13:58:26 +010080 korapServer := os.Getenv("KORAP_SERVER")
81 if korapServer == "" {
82 korapServer = "https://korap.ids-mannheim.de"
83 }
84
Akron3890fcc2022-12-20 15:26:36 +010085 //
Akron6aee7fe2022-12-20 16:00:54 +010086 r.GET("/", func(c *gin.Context) {
Akronce5186d2022-12-21 11:15:50 +010087 c.Header("Access-Control-Allow-Origin", "null")
88 c.Header("Access-Control-Allow-Credentials", "null")
89 c.Header("Vary", "Origin")
Akron5e1252e2022-12-19 17:57:56 +010090 c.HTML(http.StatusOK, "main.html", gin.H{
Akronce286af2022-12-28 13:58:26 +010091 "korapServer": korapServer,
Akron5e1252e2022-12-19 17:57:56 +010092 })
93 })
94
Akronce5186d2022-12-21 11:15:50 +010095 r.HEAD("/:corpus_id/:doc_id/:text_id", CheckSaleUrl)
Akrone2161562022-12-19 17:05:39 +010096 r.GET("/:corpus_id/:doc_id/:text_id", CheckSaleUrl)
Akron5e1252e2022-12-19 17:57:56 +010097 r.Static("/assets", "./assets")
Akrone2161562022-12-19 17:05:39 +010098 return r
99}
100
101func main() {
Akronce286af2022-12-28 13:58:26 +0100102 godotenv.Load()
103
Akrone2161562022-12-19 17:05:39 +0100104 initDB("db")
Akron3890fcc2022-12-20 15:26:36 +0100105 defer closeDB()
106
107 // Index csv file
108 if len(os.Args) > 1 {
109
110 file, err := os.Open(os.Args[1])
111 if err != nil {
112 log.Fatal(err)
113 }
114 r := csv.NewReader(file)
115
116 txn := db.NewTransaction(true)
117
118 i := 0
119
120 for {
121 record, err := r.Read()
122 if err == io.EOF {
123 break
124 }
125 if err != nil {
126 log.Fatal(err)
127 }
128
129 if err := txn.Set([]byte(record[0]), []byte(record[1]+","+record[2])); err == badger.ErrTxnTooBig {
130 log.Println("Commit", record[0], "after", i, "inserts")
131 i = 0
132 err = txn.Commit()
133 if err != nil {
134 log.Fatal("Unable to commit")
135 }
136 txn = db.NewTransaction(true)
137 _ = txn.Set([]byte(record[0]), []byte(record[1]+","+record[2]))
138 }
139 i++
140 }
141 err = txn.Commit()
142
143 if err != nil {
144 log.Fatal("Unable to commit")
145 }
146
147 return
148 }
Akrone2161562022-12-19 17:05:39 +0100149 r := setupRouter()
Akronce286af2022-12-28 13:58:26 +0100150
151 port := os.Getenv("PORT")
152 if port == "" {
153 port = "5722"
154 }
155
156 log.Fatal(http.ListenAndServe(":"+port, r))
Akrone2161562022-12-19 17:05:39 +0100157}