-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrest_server_debug_block.go
More file actions
65 lines (54 loc) · 1.25 KB
/
rest_server_debug_block.go
File metadata and controls
65 lines (54 loc) · 1.25 KB
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
package main
import (
"fmt"
"github.com/RobinUS2/golang-jresp"
"github.com/julienschmidt/httprouter"
"net/http"
"strings"
)
// Allocate new block
func PostDebugBlockAllocate(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Response object
jr := jresp.NewJsonResp()
// Auth
if !restServer.auth(r) {
restServer.notAuthorized(w)
return
}
// New block
block := datastore.NewBlock()
// Response
jr.Set("block_id", block.IdStr())
jr.OK()
fmt.Fprint(w, jr.ToString(restServer.PrettyPrint))
}
// Persist existing block
func PutDebugBlockPersist(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Response object
jr := jresp.NewJsonResp()
// Auth
if !restServer.auth(r) {
restServer.notAuthorized(w)
return
}
// Id
id := strings.TrimSpace(r.URL.Query().Get("id"))
if len(id) < 1 {
jr.Error("Please provide the block 'id' as query parameter")
fmt.Fprint(w, jr.ToString(restServer.PrettyPrint))
return
}
// Find block
block := datastore.BlockByIdStr(id)
if block == nil {
jr.Error("Block not found")
fmt.Fprint(w, jr.ToString(restServer.PrettyPrint))
return
}
// Persist
res := block.Persist()
// Response
jr.Set("persisted", res)
jr.OK()
fmt.Fprint(w, jr.ToString(restServer.PrettyPrint))
}