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" |
| 12 | ) |
| 13 | |
| 14 | var db *badger.DB |
| 15 | |
| 16 | func CheckSaleUrl(c *gin.Context) { |
| 17 | |
| 18 | corpusID := c.Param("corpus_id") |
| 19 | docID := c.Param("doc_id") |
| 20 | textID := c.Param("text_id") |
| 21 | |
Akron | ce5186d | 2022-12-21 11:15:50 +0100 | [diff] [blame] | 22 | c.Header("Access-Control-Allow-Origin", "null") |
| 23 | c.Header("Access-Control-Allow-Credentials", "null") |
| 24 | c.Header("Vary", "Origin") |
Akron | f1adbe0 | 2022-12-21 10:32:21 +0100 | [diff] [blame] | 25 | |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 26 | err := db.View(func(txn *badger.Txn) error { |
| 27 | |
| 28 | item, err := txn.Get([]byte(corpusID + "/" + docID + "/" + textID)) |
| 29 | |
| 30 | if err != nil { |
| 31 | c.String(http.StatusNotFound, "No entry found") |
Akron | 906a2c5 | 2022-12-21 11:35:28 +0100 | [diff] [blame^] | 32 | return nil |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | err = item.Value(func(v []byte) error { |
| 36 | c.String(http.StatusOK, string(v)) |
| 37 | return nil |
| 38 | }) |
| 39 | |
| 40 | if err != nil { |
Akron | ce5186d | 2022-12-21 11:15:50 +0100 | [diff] [blame] | 41 | return err |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | return nil |
| 45 | }) |
| 46 | |
| 47 | if err != nil { |
| 48 | c.String(http.StatusNotFound, err.Error()) |
| 49 | } |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 50 | } |
| 51 | |
Akron | 6aee7fe | 2022-12-20 16:00:54 +0100 | [diff] [blame] | 52 | func add(corpusID, docID, textID string, provider string, url string) error { |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 53 | err := db.Update(func(txn *badger.Txn) error { |
Akron | 6aee7fe | 2022-12-20 16:00:54 +0100 | [diff] [blame] | 54 | err := txn.Set([]byte(corpusID+"/"+docID+"/"+textID), []byte(provider+","+url)) |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 55 | return err |
| 56 | }) |
| 57 | return err |
| 58 | } |
| 59 | |
| 60 | func initDB(dir string) { |
| 61 | if db != nil { |
| 62 | return |
| 63 | } |
| 64 | var err error |
| 65 | db, err = badger.Open(badger.DefaultOptions(dir)) |
| 66 | if err != nil { |
| 67 | log.Fatal(err) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | func closeDB() { |
| 72 | db.Close() |
| 73 | } |
| 74 | |
| 75 | func setupRouter() *gin.Engine { |
| 76 | r := gin.Default() |
Akron | 5e1252e | 2022-12-19 17:57:56 +0100 | [diff] [blame] | 77 | r.LoadHTMLGlob("templates/*") |
| 78 | |
Akron | 3890fcc | 2022-12-20 15:26:36 +0100 | [diff] [blame] | 79 | // |
Akron | 6aee7fe | 2022-12-20 16:00:54 +0100 | [diff] [blame] | 80 | r.GET("/", func(c *gin.Context) { |
Akron | ce5186d | 2022-12-21 11:15:50 +0100 | [diff] [blame] | 81 | c.Header("Access-Control-Allow-Origin", "null") |
| 82 | c.Header("Access-Control-Allow-Credentials", "null") |
| 83 | c.Header("Vary", "Origin") |
Akron | 5e1252e | 2022-12-19 17:57:56 +0100 | [diff] [blame] | 84 | c.HTML(http.StatusOK, "main.html", gin.H{ |
Akron | 6aee7fe | 2022-12-20 16:00:54 +0100 | [diff] [blame] | 85 | "korapServer": "https://korap.ids-mannheim.de/instance/test", |
Akron | 5e1252e | 2022-12-19 17:57:56 +0100 | [diff] [blame] | 86 | }) |
| 87 | }) |
| 88 | |
Akron | ce5186d | 2022-12-21 11:15:50 +0100 | [diff] [blame] | 89 | r.HEAD("/:corpus_id/:doc_id/:text_id", CheckSaleUrl) |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 90 | r.GET("/:corpus_id/:doc_id/:text_id", CheckSaleUrl) |
Akron | 5e1252e | 2022-12-19 17:57:56 +0100 | [diff] [blame] | 91 | r.Static("/assets", "./assets") |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 92 | return r |
| 93 | } |
| 94 | |
| 95 | func main() { |
| 96 | initDB("db") |
Akron | 3890fcc | 2022-12-20 15:26:36 +0100 | [diff] [blame] | 97 | defer closeDB() |
| 98 | |
| 99 | // Index csv file |
| 100 | if len(os.Args) > 1 { |
| 101 | |
| 102 | file, err := os.Open(os.Args[1]) |
| 103 | if err != nil { |
| 104 | log.Fatal(err) |
| 105 | } |
| 106 | r := csv.NewReader(file) |
| 107 | |
| 108 | txn := db.NewTransaction(true) |
| 109 | |
| 110 | i := 0 |
| 111 | |
| 112 | for { |
| 113 | record, err := r.Read() |
| 114 | if err == io.EOF { |
| 115 | break |
| 116 | } |
| 117 | if err != nil { |
| 118 | log.Fatal(err) |
| 119 | } |
| 120 | |
| 121 | if err := txn.Set([]byte(record[0]), []byte(record[1]+","+record[2])); err == badger.ErrTxnTooBig { |
| 122 | log.Println("Commit", record[0], "after", i, "inserts") |
| 123 | i = 0 |
| 124 | err = txn.Commit() |
| 125 | if err != nil { |
| 126 | log.Fatal("Unable to commit") |
| 127 | } |
| 128 | txn = db.NewTransaction(true) |
| 129 | _ = txn.Set([]byte(record[0]), []byte(record[1]+","+record[2])) |
| 130 | } |
| 131 | i++ |
| 132 | } |
| 133 | err = txn.Commit() |
| 134 | |
| 135 | if err != nil { |
| 136 | log.Fatal("Unable to commit") |
| 137 | } |
| 138 | |
| 139 | return |
| 140 | } |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 141 | r := setupRouter() |
Akron | 0adfd5c | 2022-12-20 16:02:25 +0100 | [diff] [blame] | 142 | log.Fatal(http.ListenAndServe(":5722", r)) |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 143 | } |