Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "log" |
| 5 | "net/http" |
| 6 | |
| 7 | badger "github.com/dgraph-io/badger/v3" |
| 8 | "github.com/gin-gonic/gin" |
| 9 | ) |
| 10 | |
| 11 | var db *badger.DB |
| 12 | |
| 13 | func CheckSaleUrl(c *gin.Context) { |
| 14 | |
| 15 | corpusID := c.Param("corpus_id") |
| 16 | docID := c.Param("doc_id") |
| 17 | textID := c.Param("text_id") |
| 18 | |
| 19 | err := db.View(func(txn *badger.Txn) error { |
| 20 | |
| 21 | item, err := txn.Get([]byte(corpusID + "/" + docID + "/" + textID)) |
| 22 | |
| 23 | if err != nil { |
| 24 | c.String(http.StatusNotFound, "No entry found") |
| 25 | } |
| 26 | |
| 27 | err = item.Value(func(v []byte) error { |
| 28 | c.String(http.StatusOK, string(v)) |
| 29 | return nil |
| 30 | }) |
| 31 | |
| 32 | if err != nil { |
| 33 | c.String(http.StatusNotFound, err.Error()) |
| 34 | } |
| 35 | |
| 36 | return nil |
| 37 | }) |
| 38 | |
| 39 | if err != nil { |
| 40 | c.String(http.StatusNotFound, err.Error()) |
| 41 | } |
| 42 | |
| 43 | c.String(http.StatusExpectationFailed, err.Error()) |
| 44 | } |
| 45 | |
| 46 | func add(corpusID, docID, textID string, url string) error { |
| 47 | err := db.Update(func(txn *badger.Txn) error { |
| 48 | err := txn.Set([]byte(corpusID+"/"+docID+"/"+textID), []byte(url)) |
| 49 | return err |
| 50 | }) |
| 51 | return err |
| 52 | } |
| 53 | |
| 54 | func initDB(dir string) { |
| 55 | if db != nil { |
| 56 | return |
| 57 | } |
| 58 | var err error |
| 59 | db, err = badger.Open(badger.DefaultOptions(dir)) |
| 60 | if err != nil { |
| 61 | log.Fatal(err) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | func closeDB() { |
| 66 | db.Close() |
| 67 | } |
| 68 | |
| 69 | func setupRouter() *gin.Engine { |
| 70 | r := gin.Default() |
Akron | 5e1252e | 2022-12-19 17:57:56 +0100 | [diff] [blame] | 71 | r.LoadHTMLGlob("templates/*") |
| 72 | |
| 73 | r.GET("/index", func(c *gin.Context) { |
| 74 | c.HTML(http.StatusOK, "main.html", gin.H{ |
Akron | 5e1252e | 2022-12-19 17:57:56 +0100 | [diff] [blame] | 75 | "korapServer": "https://korap.ids-mannheim.de/", |
| 76 | }) |
| 77 | }) |
| 78 | |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 79 | r.GET("/:corpus_id/:doc_id/:text_id", CheckSaleUrl) |
Akron | 5e1252e | 2022-12-19 17:57:56 +0100 | [diff] [blame] | 80 | r.Static("/assets", "./assets") |
Akron | e216156 | 2022-12-19 17:05:39 +0100 | [diff] [blame] | 81 | return r |
| 82 | } |
| 83 | |
| 84 | func main() { |
| 85 | initDB("db") |
| 86 | r := setupRouter() |
| 87 | r.Run(":8080") |
| 88 | defer closeDB() |
| 89 | } |