blob: 0c88a692c422c04b7319c909df225efcb17ee1ad [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
Akron3890fcc2022-12-20 15:26:36 +010091 //
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
Akronce5186d2022-12-21 11:15:50 +010098 r.HEAD("/:corpus_id/:doc_id/:text_id", CheckSaleUrl)
Akrone2161562022-12-19 17:05:39 +010099 r.GET("/:corpus_id/:doc_id/:text_id", CheckSaleUrl)
Akron5e1252e2022-12-19 17:57:56 +0100100 r.Static("/assets", "./assets")
Akrone2161562022-12-19 17:05:39 +0100101 return r
102}
103
104func main() {
Akronce286af2022-12-28 13:58:26 +0100105 godotenv.Load()
106
Akrone2161562022-12-19 17:05:39 +0100107 initDB("db")
Akron3890fcc2022-12-20 15:26:36 +0100108 defer closeDB()
109
110 // Index csv file
111 if len(os.Args) > 1 {
112
113 file, err := os.Open(os.Args[1])
114 if err != nil {
115 log.Fatal(err)
116 }
117 r := csv.NewReader(file)
118
119 txn := db.NewTransaction(true)
120
121 i := 0
122
123 for {
124 record, err := r.Read()
125 if err == io.EOF {
126 break
127 }
128 if err != nil {
129 log.Fatal(err)
130 }
131
132 if err := txn.Set([]byte(record[0]), []byte(record[1]+","+record[2])); err == badger.ErrTxnTooBig {
133 log.Println("Commit", record[0], "after", i, "inserts")
134 i = 0
135 err = txn.Commit()
136 if err != nil {
137 log.Fatal("Unable to commit")
138 }
139 txn = db.NewTransaction(true)
140 _ = txn.Set([]byte(record[0]), []byte(record[1]+","+record[2]))
141 }
142 i++
143 }
144 err = txn.Commit()
145
146 if err != nil {
147 log.Fatal("Unable to commit")
148 }
149
150 return
151 }
Akrone2161562022-12-19 17:05:39 +0100152 r := setupRouter()
Akronce286af2022-12-28 13:58:26 +0100153
154 port := os.Getenv("PORT")
155 if port == "" {
156 port = "5722"
157 }
158
159 log.Fatal(http.ListenAndServe(":"+port, r))
Akrone2161562022-12-19 17:05:39 +0100160}