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" |
| 5 | "io" |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 6 | "log" |
| 7 | "net/http" |
Akron | 3890fcc | 2022-12-20 15:26:36 +0100 | [diff] [blame] | 8 | "os" |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 9 | |
| 10 | badger "github.com/dgraph-io/badger/v3" |
| 11 | "github.com/gin-gonic/gin" |
Akron | ce286af | 2022-12-28 13:58:26 +0100 | [diff] [blame] | 12 | "github.com/joho/godotenv" |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 13 | ) |
| 14 | |
| 15 | var db *badger.DB |
| 16 | |
| 17 | func 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") |
Akron | 906a2c5 | 2022-12-21 11:35:28 +0100 | [diff] [blame] | 29 | return nil |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 30 | } |
| 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 { |
Akron | ce5186d | 2022-12-21 11:15:50 +0100 | [diff] [blame] | 38 | return err |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | return nil |
| 42 | }) |
| 43 | |
| 44 | if err != nil { |
| 45 | c.String(http.StatusNotFound, err.Error()) |
| 46 | } |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 47 | } |
| 48 | |
Akron | 6aee7fe | 2022-12-20 16:00:54 +0100 | [diff] [blame] | 49 | func add(corpusID, docID, textID string, provider string, url string) error { |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 50 | err := db.Update(func(txn *badger.Txn) error { |
Akron | 6aee7fe | 2022-12-20 16:00:54 +0100 | [diff] [blame] | 51 | err := txn.Set([]byte(corpusID+"/"+docID+"/"+textID), []byte(provider+","+url)) |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 52 | return err |
| 53 | }) |
| 54 | return err |
| 55 | } |
| 56 | |
| 57 | func 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 | |
| 68 | func closeDB() { |
| 69 | db.Close() |
| 70 | } |
| 71 | |
| 72 | func setupRouter() *gin.Engine { |
| 73 | r := gin.Default() |
Akron | 5e1252e | 2022-12-19 17:57:56 +0100 | [diff] [blame] | 74 | r.LoadHTMLGlob("templates/*") |
| 75 | |
Akron | ce286af | 2022-12-28 13:58:26 +0100 | [diff] [blame] | 76 | korapServer := os.Getenv("KORAP_SERVER") |
| 77 | if korapServer == "" { |
| 78 | korapServer = "https://korap.ids-mannheim.de" |
| 79 | } |
| 80 | |
Akron | 79d8348 | 2023-01-02 10:53:24 +0100 | [diff] [blame] | 81 | 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 | |
Akron | 60e1160 | 2023-01-02 11:16:43 +0100 | [diff] [blame^] | 91 | // Return widget page |
Akron | 6aee7fe | 2022-12-20 16:00:54 +0100 | [diff] [blame] | 92 | r.GET("/", func(c *gin.Context) { |
Akron | 5e1252e | 2022-12-19 17:57:56 +0100 | [diff] [blame] | 93 | c.HTML(http.StatusOK, "main.html", gin.H{ |
Akron | ce286af | 2022-12-28 13:58:26 +0100 | [diff] [blame] | 94 | "korapServer": korapServer, |
Akron | 5e1252e | 2022-12-19 17:57:56 +0100 | [diff] [blame] | 95 | }) |
| 96 | }) |
| 97 | |
Akron | 60e1160 | 2023-01-02 11:16:43 +0100 | [diff] [blame^] | 98 | // Return provider information |
Akron | ce5186d | 2022-12-21 11:15:50 +0100 | [diff] [blame] | 99 | r.HEAD("/:corpus_id/:doc_id/:text_id", CheckSaleUrl) |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 100 | r.GET("/:corpus_id/:doc_id/:text_id", CheckSaleUrl) |
Akron | 60e1160 | 2023-01-02 11:16:43 +0100 | [diff] [blame^] | 101 | |
| 102 | // Return plugin manifest |
| 103 | r.StaticFile("/plugin.json", "./assets/plugin.json") |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 104 | return r |
| 105 | } |
| 106 | |
| 107 | func main() { |
Akron | ce286af | 2022-12-28 13:58:26 +0100 | [diff] [blame] | 108 | godotenv.Load() |
| 109 | |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 110 | initDB("db") |
Akron | 3890fcc | 2022-12-20 15:26:36 +0100 | [diff] [blame] | 111 | 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 | } |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 155 | r := setupRouter() |
Akron | ce286af | 2022-12-28 13:58:26 +0100 | [diff] [blame] | 156 | |
| 157 | port := os.Getenv("PORT") |
| 158 | if port == "" { |
| 159 | port = "5722" |
| 160 | } |
| 161 | |
| 162 | log.Fatal(http.ListenAndServe(":"+port, r)) |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 163 | } |