-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
61 lines (47 loc) · 917 Bytes
/
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
package main
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
toml "github.com/BurntSushi/toml"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
type Config struct {
Connection string
DB string
}
type Event struct {
DocID int
Total int
}
func main() {
var config Config
if _, err := toml.DecodeFile("config.toml", &config); err != nil {
fmt.Println(err)
return
}
router := gin.Default()
// increment counter
router.GET("/counter/:id", func(c *gin.Context) {
id := c.Param("id")
session, err := mgo.Dial(config.Connection)
if err != nil {
panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
col := session.DB(config.DB).C("Events")
col.Upsert(
bson.M{"docid": id},
bson.M{"$inc": bson.M{"total": 1}},
)
if err != nil {
panic(err)
}
c.JSON(http.StatusOK, gin.H{
"status": fmt.Sprintf("ok"),
})
})
router.Run(":8000")
}