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") |
| 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 | 3890fcc | 2022-12-20 15:26:36 +0100 | [diff] [blame] | 78 | // |
Akron | 6aee7fe | 2022-12-20 16:00:54 +0100 | [diff] [blame] | 79 | r.GET("/", func(c *gin.Context) { |
Akron | ce5186d | 2022-12-21 11:15:50 +0100 | [diff] [blame^] | 80 | c.Header("Access-Control-Allow-Origin", "null") |
| 81 | c.Header("Access-Control-Allow-Credentials", "null") |
| 82 | c.Header("Vary", "Origin") |
Akron | 5e1252e | 2022-12-19 17:57:56 +0100 | [diff] [blame] | 83 | c.HTML(http.StatusOK, "main.html", gin.H{ |
Akron | 6aee7fe | 2022-12-20 16:00:54 +0100 | [diff] [blame] | 84 | "korapServer": "https://korap.ids-mannheim.de/instance/test", |
Akron | 5e1252e | 2022-12-19 17:57:56 +0100 | [diff] [blame] | 85 | }) |
| 86 | }) |
| 87 | |
Akron | ce5186d | 2022-12-21 11:15:50 +0100 | [diff] [blame^] | 88 | r.HEAD("/:corpus_id/:doc_id/:text_id", CheckSaleUrl) |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 89 | r.GET("/:corpus_id/:doc_id/:text_id", CheckSaleUrl) |
Akron | 5e1252e | 2022-12-19 17:57:56 +0100 | [diff] [blame] | 90 | r.Static("/assets", "./assets") |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 91 | return r |
| 92 | } |
| 93 | |
| 94 | func main() { |
| 95 | initDB("db") |
Akron | 3890fcc | 2022-12-20 15:26:36 +0100 | [diff] [blame] | 96 | defer closeDB() |
| 97 | |
| 98 | // Index csv file |
| 99 | if len(os.Args) > 1 { |
| 100 | |
| 101 | file, err := os.Open(os.Args[1]) |
| 102 | if err != nil { |
| 103 | log.Fatal(err) |
| 104 | } |
| 105 | r := csv.NewReader(file) |
| 106 | |
| 107 | txn := db.NewTransaction(true) |
| 108 | |
| 109 | i := 0 |
| 110 | |
| 111 | for { |
| 112 | record, err := r.Read() |
| 113 | if err == io.EOF { |
| 114 | break |
| 115 | } |
| 116 | if err != nil { |
| 117 | log.Fatal(err) |
| 118 | } |
| 119 | |
| 120 | if err := txn.Set([]byte(record[0]), []byte(record[1]+","+record[2])); err == badger.ErrTxnTooBig { |
| 121 | log.Println("Commit", record[0], "after", i, "inserts") |
| 122 | i = 0 |
| 123 | err = txn.Commit() |
| 124 | if err != nil { |
| 125 | log.Fatal("Unable to commit") |
| 126 | } |
| 127 | txn = db.NewTransaction(true) |
| 128 | _ = txn.Set([]byte(record[0]), []byte(record[1]+","+record[2])) |
| 129 | } |
| 130 | i++ |
| 131 | } |
| 132 | err = txn.Commit() |
| 133 | |
| 134 | if err != nil { |
| 135 | log.Fatal("Unable to commit") |
| 136 | } |
| 137 | |
| 138 | return |
| 139 | } |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 140 | r := setupRouter() |
Akron | 0adfd5c | 2022-12-20 16:02:25 +0100 | [diff] [blame] | 141 | log.Fatal(http.ListenAndServe(":5722", r)) |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 142 | } |