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 | |
Akron | ce5186d | 2022-12-21 11:15:50 +0100 | [diff] [blame] | 23 | c.Header("Access-Control-Allow-Origin", "null") |
| 24 | c.Header("Access-Control-Allow-Credentials", "null") |
| 25 | c.Header("Vary", "Origin") |
Akron | f1adbe0 | 2022-12-21 10:32:21 +0100 | [diff] [blame] | 26 | |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 27 | 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") |
Akron | 906a2c5 | 2022-12-21 11:35:28 +0100 | [diff] [blame] | 33 | return nil |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 34 | } |
| 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 { |
Akron | ce5186d | 2022-12-21 11:15:50 +0100 | [diff] [blame] | 42 | return err |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | return nil |
| 46 | }) |
| 47 | |
| 48 | if err != nil { |
| 49 | c.String(http.StatusNotFound, err.Error()) |
| 50 | } |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 51 | } |
| 52 | |
Akron | 6aee7fe | 2022-12-20 16:00:54 +0100 | [diff] [blame] | 53 | func add(corpusID, docID, textID string, provider string, url string) error { |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 54 | err := db.Update(func(txn *badger.Txn) error { |
Akron | 6aee7fe | 2022-12-20 16:00:54 +0100 | [diff] [blame] | 55 | err := txn.Set([]byte(corpusID+"/"+docID+"/"+textID), []byte(provider+","+url)) |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 56 | return err |
| 57 | }) |
| 58 | return err |
| 59 | } |
| 60 | |
| 61 | func 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 | |
| 72 | func closeDB() { |
| 73 | db.Close() |
| 74 | } |
| 75 | |
| 76 | func setupRouter() *gin.Engine { |
| 77 | r := gin.Default() |
Akron | 5e1252e | 2022-12-19 17:57:56 +0100 | [diff] [blame] | 78 | r.LoadHTMLGlob("templates/*") |
| 79 | |
Akron | ce286af | 2022-12-28 13:58:26 +0100 | [diff] [blame] | 80 | korapServer := os.Getenv("KORAP_SERVER") |
| 81 | if korapServer == "" { |
| 82 | korapServer = "https://korap.ids-mannheim.de" |
| 83 | } |
| 84 | |
Akron | 3890fcc | 2022-12-20 15:26:36 +0100 | [diff] [blame] | 85 | // |
Akron | 6aee7fe | 2022-12-20 16:00:54 +0100 | [diff] [blame] | 86 | r.GET("/", func(c *gin.Context) { |
Akron | ce5186d | 2022-12-21 11:15:50 +0100 | [diff] [blame] | 87 | c.Header("Access-Control-Allow-Origin", "null") |
| 88 | c.Header("Access-Control-Allow-Credentials", "null") |
| 89 | c.Header("Vary", "Origin") |
Akron | 5e1252e | 2022-12-19 17:57:56 +0100 | [diff] [blame] | 90 | c.HTML(http.StatusOK, "main.html", gin.H{ |
Akron | ce286af | 2022-12-28 13:58:26 +0100 | [diff] [blame] | 91 | "korapServer": korapServer, |
Akron | 5e1252e | 2022-12-19 17:57:56 +0100 | [diff] [blame] | 92 | }) |
| 93 | }) |
| 94 | |
Akron | ce5186d | 2022-12-21 11:15:50 +0100 | [diff] [blame] | 95 | r.HEAD("/:corpus_id/:doc_id/:text_id", CheckSaleUrl) |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 96 | r.GET("/:corpus_id/:doc_id/:text_id", CheckSaleUrl) |
Akron | 5e1252e | 2022-12-19 17:57:56 +0100 | [diff] [blame] | 97 | r.Static("/assets", "./assets") |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 98 | return r |
| 99 | } |
| 100 | |
| 101 | func main() { |
Akron | ce286af | 2022-12-28 13:58:26 +0100 | [diff] [blame] | 102 | godotenv.Load() |
| 103 | |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 104 | initDB("db") |
Akron | 3890fcc | 2022-12-20 15:26:36 +0100 | [diff] [blame] | 105 | 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 | } |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 149 | r := setupRouter() |
Akron | ce286af | 2022-12-28 13:58:26 +0100 | [diff] [blame] | 150 | |
| 151 | port := os.Getenv("PORT") |
| 152 | if port == "" { |
| 153 | port = "5722" |
| 154 | } |
| 155 | |
| 156 | log.Fatal(http.ListenAndServe(":"+port, r)) |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 157 | } |