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 | |
| 22 | err := db.View(func(txn *badger.Txn) error { |
| 23 | |
| 24 | item, err := txn.Get([]byte(corpusID + "/" + docID + "/" + textID)) |
| 25 | |
| 26 | if err != nil { |
| 27 | c.String(http.StatusNotFound, "No entry found") |
| 28 | } |
| 29 | |
| 30 | err = item.Value(func(v []byte) error { |
| 31 | c.String(http.StatusOK, string(v)) |
| 32 | return nil |
| 33 | }) |
| 34 | |
| 35 | if err != nil { |
| 36 | c.String(http.StatusNotFound, err.Error()) |
| 37 | } |
| 38 | |
| 39 | return nil |
| 40 | }) |
| 41 | |
| 42 | if err != nil { |
| 43 | c.String(http.StatusNotFound, err.Error()) |
| 44 | } |
| 45 | |
| 46 | c.String(http.StatusExpectationFailed, err.Error()) |
| 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 | 3890fcc | 2022-12-20 15:26:36 +0100 | [diff] [blame] | 76 | // |
Akron | 6aee7fe | 2022-12-20 16:00:54 +0100 | [diff] [blame^] | 77 | r.GET("/", func(c *gin.Context) { |
Akron | 5e1252e | 2022-12-19 17:57:56 +0100 | [diff] [blame] | 78 | c.HTML(http.StatusOK, "main.html", gin.H{ |
Akron | 6aee7fe | 2022-12-20 16:00:54 +0100 | [diff] [blame^] | 79 | "korapServer": "https://korap.ids-mannheim.de/instance/test", |
Akron | 5e1252e | 2022-12-19 17:57:56 +0100 | [diff] [blame] | 80 | }) |
| 81 | }) |
| 82 | |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 83 | r.GET("/:corpus_id/:doc_id/:text_id", CheckSaleUrl) |
Akron | 5e1252e | 2022-12-19 17:57:56 +0100 | [diff] [blame] | 84 | r.Static("/assets", "./assets") |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 85 | return r |
| 86 | } |
| 87 | |
| 88 | func main() { |
| 89 | initDB("db") |
Akron | 3890fcc | 2022-12-20 15:26:36 +0100 | [diff] [blame] | 90 | defer closeDB() |
| 91 | |
| 92 | // Index csv file |
| 93 | if len(os.Args) > 1 { |
| 94 | |
| 95 | file, err := os.Open(os.Args[1]) |
| 96 | if err != nil { |
| 97 | log.Fatal(err) |
| 98 | } |
| 99 | r := csv.NewReader(file) |
| 100 | |
| 101 | txn := db.NewTransaction(true) |
| 102 | |
| 103 | i := 0 |
| 104 | |
| 105 | for { |
| 106 | record, err := r.Read() |
| 107 | if err == io.EOF { |
| 108 | break |
| 109 | } |
| 110 | if err != nil { |
| 111 | log.Fatal(err) |
| 112 | } |
| 113 | |
| 114 | if err := txn.Set([]byte(record[0]), []byte(record[1]+","+record[2])); err == badger.ErrTxnTooBig { |
| 115 | log.Println("Commit", record[0], "after", i, "inserts") |
| 116 | i = 0 |
| 117 | err = txn.Commit() |
| 118 | if err != nil { |
| 119 | log.Fatal("Unable to commit") |
| 120 | } |
| 121 | txn = db.NewTransaction(true) |
| 122 | _ = txn.Set([]byte(record[0]), []byte(record[1]+","+record[2])) |
| 123 | } |
| 124 | i++ |
| 125 | } |
| 126 | err = txn.Commit() |
| 127 | |
| 128 | if err != nil { |
| 129 | log.Fatal("Unable to commit") |
| 130 | } |
| 131 | |
| 132 | return |
| 133 | } |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 134 | r := setupRouter() |
| 135 | r.Run(":8080") |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 136 | } |