-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyramide.go
158 lines (128 loc) · 3.23 KB
/
pyramide.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// To Do
// -------
// Make delete a little cleaner
// Add traversal
package main
import (
"github.com/gromnsk/tree"
"github.com/takama/router"
"strconv"
"encoding/json"
"io/ioutil"
// "net/http"
"fmt"
// "net/url"
// "encoding/json"
)
var path = "/var/lib/pyramide/nodes.json"
type Result struct {
Success bool
}
func main() {
root := new(tree.Tree)
data := tree.Data{Id: 1}
node := root.Insert(data)
// for index := 2; index < 50000; index++ {
// data := tree.Data{Id: index}
// node.Insert(data)
// }
fmt.Println("Started!")
r := router.New()
//put node somewhere from root
r.PUT("/node/:id", func(c *router.Control) {
c.UseTimer()
Id, err := strconv.Atoi(c.Get(":id"))
if err != nil {
// TODO catch error
}
data := tree.Data{Id: Id}
node.Insert(data)
c.Code(200).Body(data)
})
// PUT node to ReferrerId
r.PUT("/node/:referrerId/:id", func(c *router.Control) {
c.UseTimer()
Id, err := strconv.Atoi(c.Get(":id"))
referrerId, refErr := strconv.Atoi(c.Get(":referrerId"))
if err != nil || refErr != nil {
// TODO catch error
}
referrerNode := node.Search(referrerId)
data := tree.Data{Id: Id}
referrerNode.Insert(data)
c.Code(200).Body(data)
})
//Get node by ID
r.GET("/node/:id", func(c *router.Control) {
c.UseTimer()
Id, err := strconv.Atoi(c.Get(":id"))
if err != nil {
// TODO catch error
}
findedNode := node.Search(Id)
c.Code(200).Body(findedNode.GetData())
})
// GET all nodes from some node
r.GET("/nodes/:id", func(c *router.Control) {
c.UseTimer()
Id, err := strconv.Atoi(c.Get(":id"))
if err != nil {
// TODO catch error
}
referrerNode := node.Search(Id)
referrerNode.Print()
c.Code(200).Body(referrerNode.GetData())
})
// GET all nodes from some node with level limit
r.GET("/nodes/:id/:level", func(c *router.Control) {
c.UseTimer()
Id, err := strconv.Atoi(c.Get(":id"))
if err != nil {
// TODO catch error
}
Level, err := strconv.Atoi(c.Get(":level"))
if err != nil {
// TODO catch error
}
referrerNode := node.Search(Id)
c.Code(200).Body(referrerNode.GetNodes(Level))
})
// GET balance for current node
r.GET("/balance/:id", func(c *router.Control) {
c.UseTimer()
Id, err := strconv.Atoi(c.Get(":id"))
if err != nil {
// TODO catch error
}
referrerNode := node.Search(Id)
data := referrerNode.GetNodes(8)
c.Code(200).Body(len(data))
})
// GET balance for current node
r.GET("/dump", func(c *router.Control) {
c.UseTimer()
data := node.GetNodes(10000)
j, jerr := json.MarshalIndent(data, "", " ")
if jerr != nil {
fmt.Println("jerr:", jerr.Error())
}
ioutil.WriteFile(path, j, 0644)
c.Code(200).Body(len(data))
})
// GET balance for current node
r.GET("/restore", func(c *router.Control) {
c.UseTimer()
data, err := ioutil.ReadFile(path)
if err != nil {
fmt.Println("error reading json: ", err.Error())
}
var restored []*tree.Result
jerr := json.Unmarshal(data, &restored)
if jerr != nil {
fmt.Println("jerr:", jerr.Error())
}
node.SetAllNodes(restored)
c.Code(200).Body(node)
})
r.Listen(":3333")
}