-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
67 lines (61 loc) · 1.4 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"flag"
"github.com/boltdb/bolt"
"github.com/gin-gonic/gin"
"os"
)
func getGinRouter() *gin.Engine {
router := gin.Default()
return router
}
func createDatabase() {
db, err := bolt.Open("my.db", 0600, nil)
if err != nil {
log.Fatal(err)
}
db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucketIfNotExists([]byte("queue"))
if err != nil {
return fmt.Errorf("create bucket: %s", err)
}
q := b.Get([]byte("network_ids"))
if q == nil {
b.Put([]byte("network_ids"), []string)
}
return nil
})
return db
}
func setRoutes(r *gin.Engine, d *bolt.DB) {
r.POST("/network/:network_id", func(c *gin.Context) {
id := c.Param("network_id")
d.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("queue"))
q := b.Get([]byte("network_ids"))
append(*q, id)
err = b.Put([]byte("network_ids"), q)
return err
})
c.JSON(200, gin.H{"message": "Network submission successful."})
})
r.GET("/network", func(c *gin.Context) {
d.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("queue"))
q := b.Get([]byte("network_ids"))
len := q.Len() - 1
network_id = (*q)[len]
*q = (*q)[:x]
err = b.Put([]byte("network_ids"), q)
return err
})
c.JSON(200, gin.H{"network_id": network_id})
})
}
func main() {
database := getDatabase()
defer database.Close()
router := getGinRouter(database)
setRoutes(router, database)
router.Run(":8080")
}