Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
Akron | 3890fcc | 2022-12-20 15:26:36 +0100 | [diff] [blame] | 4 | "encoding/csv" |
Akron | 65503a3 | 2023-01-02 12:38:32 +0100 | [diff] [blame] | 5 | "encoding/json" |
Akron | 3890fcc | 2022-12-20 15:26:36 +0100 | [diff] [blame] | 6 | "io" |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 7 | "log" |
| 8 | "net/http" |
Akron | 3890fcc | 2022-12-20 15:26:36 +0100 | [diff] [blame] | 9 | "os" |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 10 | |
| 11 | badger "github.com/dgraph-io/badger/v3" |
| 12 | "github.com/gin-gonic/gin" |
Akron | ce286af | 2022-12-28 13:58:26 +0100 | [diff] [blame] | 13 | "github.com/joho/godotenv" |
Akron | 65503a3 | 2023-01-02 12:38:32 +0100 | [diff] [blame] | 14 | "github.com/mattn/go-jsonpointer" |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 15 | ) |
| 16 | |
| 17 | var db *badger.DB |
| 18 | |
| 19 | func CheckSaleUrl(c *gin.Context) { |
| 20 | |
| 21 | corpusID := c.Param("corpus_id") |
| 22 | docID := c.Param("doc_id") |
| 23 | textID := c.Param("text_id") |
| 24 | |
| 25 | err := db.View(func(txn *badger.Txn) error { |
| 26 | |
| 27 | item, err := txn.Get([]byte(corpusID + "/" + docID + "/" + textID)) |
| 28 | |
| 29 | if err != nil { |
| 30 | c.String(http.StatusNotFound, "No entry found") |
Akron | 906a2c5 | 2022-12-21 11:35:28 +0100 | [diff] [blame] | 31 | return nil |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 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 { |
Akron | ce5186d | 2022-12-21 11:15:50 +0100 | [diff] [blame] | 40 | return err |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | return nil |
| 44 | }) |
| 45 | |
| 46 | if err != nil { |
| 47 | c.String(http.StatusNotFound, err.Error()) |
| 48 | } |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 49 | } |
| 50 | |
Akron | 6aee7fe | 2022-12-20 16:00:54 +0100 | [diff] [blame] | 51 | func add(corpusID, docID, textID string, provider string, url string) error { |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 52 | err := db.Update(func(txn *badger.Txn) error { |
Akron | 6aee7fe | 2022-12-20 16:00:54 +0100 | [diff] [blame] | 53 | err := txn.Set([]byte(corpusID+"/"+docID+"/"+textID), []byte(provider+","+url)) |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 54 | return err |
| 55 | }) |
| 56 | return err |
| 57 | } |
| 58 | |
| 59 | func 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 | |
| 70 | func closeDB() { |
| 71 | db.Close() |
| 72 | } |
| 73 | |
| 74 | func setupRouter() *gin.Engine { |
| 75 | r := gin.Default() |
Akron | 5e1252e | 2022-12-19 17:57:56 +0100 | [diff] [blame] | 76 | r.LoadHTMLGlob("templates/*") |
| 77 | |
Akron | ce286af | 2022-12-28 13:58:26 +0100 | [diff] [blame] | 78 | korapServer := os.Getenv("KORAP_SERVER") |
| 79 | if korapServer == "" { |
| 80 | korapServer = "https://korap.ids-mannheim.de" |
| 81 | } |
| 82 | |
Akron | 65503a3 | 2023-01-02 12:38:32 +0100 | [diff] [blame] | 83 | var pluginManifest map[string]any |
| 84 | json.Unmarshal([]byte(`{ |
| 85 | "name" : "External Provider", |
| 86 | "desc" : "Buy content from an external provider", |
| 87 | "embed" : [{ |
| 88 | "panel" : "match", |
| 89 | "title" : "Full Text", |
| 90 | "classes" : ["plugin", "cart"], |
| 91 | "icon" : "\f07a", |
| 92 | "onClick" : { |
| 93 | "action" : "addWidget", |
| 94 | "template":"", |
| 95 | "permissions": [ |
| 96 | "scripts", |
| 97 | "popups" |
| 98 | ] |
| 99 | } |
| 100 | }] |
| 101 | }`), &pluginManifest) |
| 102 | |
| 103 | externalProvider := os.Getenv("KORAP_EXTERNAL_PROVIDER") |
| 104 | if externalProvider == "" { |
| 105 | externalProvider = "https://korap.ids-mannheim.de/plugin/external/" |
| 106 | } |
| 107 | jsonpointer.Set(pluginManifest, "/embed/0/onClick/template", externalProvider) |
| 108 | |
Akron | 79d8348 | 2023-01-02 10:53:24 +0100 | [diff] [blame] | 109 | r.Use(func() gin.HandlerFunc { |
| 110 | return func(c *gin.Context) { |
| 111 | h := c.Writer.Header() |
| 112 | h.Set("Access-Control-Allow-Origin", "null") |
| 113 | h.Set("Access-Control-Allow-Credentials", "null") |
| 114 | h.Set("Vary", "Origin") |
| 115 | } |
| 116 | }(), |
| 117 | ) |
| 118 | |
Akron | 60e1160 | 2023-01-02 11:16:43 +0100 | [diff] [blame] | 119 | // Return widget page |
Akron | 6aee7fe | 2022-12-20 16:00:54 +0100 | [diff] [blame] | 120 | r.GET("/", func(c *gin.Context) { |
Akron | 5e1252e | 2022-12-19 17:57:56 +0100 | [diff] [blame] | 121 | c.HTML(http.StatusOK, "main.html", gin.H{ |
Akron | ce286af | 2022-12-28 13:58:26 +0100 | [diff] [blame] | 122 | "korapServer": korapServer, |
Akron | 5e1252e | 2022-12-19 17:57:56 +0100 | [diff] [blame] | 123 | }) |
| 124 | }) |
| 125 | |
Akron | 60e1160 | 2023-01-02 11:16:43 +0100 | [diff] [blame] | 126 | // Return provider information |
Akron | ce5186d | 2022-12-21 11:15:50 +0100 | [diff] [blame] | 127 | r.HEAD("/:corpus_id/:doc_id/:text_id", CheckSaleUrl) |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 128 | r.GET("/:corpus_id/:doc_id/:text_id", CheckSaleUrl) |
Akron | 60e1160 | 2023-01-02 11:16:43 +0100 | [diff] [blame] | 129 | |
| 130 | // Return plugin manifest |
Akron | 65503a3 | 2023-01-02 12:38:32 +0100 | [diff] [blame] | 131 | r.GET("/plugin.json", func(c *gin.Context) { |
| 132 | c.JSON(200, pluginManifest) |
| 133 | }) |
| 134 | |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 135 | return r |
| 136 | } |
| 137 | |
| 138 | func main() { |
Akron | 3c58d0d | 2023-01-02 13:35:41 +0100 | [diff] [blame] | 139 | if godotenv.Load() != nil { |
| 140 | log.Println(".env file not loaded.") |
| 141 | } |
Akron | ce286af | 2022-12-28 13:58:26 +0100 | [diff] [blame] | 142 | |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 143 | initDB("db") |
Akron | 3890fcc | 2022-12-20 15:26:36 +0100 | [diff] [blame] | 144 | defer closeDB() |
| 145 | |
| 146 | // Index csv file |
| 147 | if len(os.Args) > 1 { |
| 148 | |
| 149 | file, err := os.Open(os.Args[1]) |
| 150 | if err != nil { |
| 151 | log.Fatal(err) |
| 152 | } |
| 153 | r := csv.NewReader(file) |
| 154 | |
| 155 | txn := db.NewTransaction(true) |
| 156 | |
| 157 | i := 0 |
| 158 | |
| 159 | for { |
| 160 | record, err := r.Read() |
| 161 | if err == io.EOF { |
| 162 | break |
| 163 | } |
| 164 | if err != nil { |
| 165 | log.Fatal(err) |
| 166 | } |
| 167 | |
| 168 | if err := txn.Set([]byte(record[0]), []byte(record[1]+","+record[2])); err == badger.ErrTxnTooBig { |
| 169 | log.Println("Commit", record[0], "after", i, "inserts") |
| 170 | i = 0 |
| 171 | err = txn.Commit() |
| 172 | if err != nil { |
| 173 | log.Fatal("Unable to commit") |
| 174 | } |
| 175 | txn = db.NewTransaction(true) |
| 176 | _ = txn.Set([]byte(record[0]), []byte(record[1]+","+record[2])) |
| 177 | } |
| 178 | i++ |
| 179 | } |
| 180 | err = txn.Commit() |
| 181 | |
| 182 | if err != nil { |
| 183 | log.Fatal("Unable to commit") |
| 184 | } |
| 185 | |
| 186 | return |
| 187 | } |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 188 | r := setupRouter() |
Akron | ce286af | 2022-12-28 13:58:26 +0100 | [diff] [blame] | 189 | |
Akron | 3c58d0d | 2023-01-02 13:35:41 +0100 | [diff] [blame] | 190 | port := os.Getenv("KORAP_EXTERNAL_PROVIDER_PORT") |
Akron | ce286af | 2022-12-28 13:58:26 +0100 | [diff] [blame] | 191 | if port == "" { |
| 192 | port = "5722" |
| 193 | } |
| 194 | |
| 195 | log.Fatal(http.ListenAndServe(":"+port, r)) |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 196 | } |