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