Skip to content

Commit 11a5f0e

Browse files
committed
Add blockchain audit endpoints
1 parent 3791ca9 commit 11a5f0e

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

https/rest/audit.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package rest
2+
3+
import (
4+
"dyn-https/blockchain/dynamic"
5+
"dyn-https/models"
6+
"encoding/json"
7+
"fmt"
8+
"io/ioutil"
9+
"net/http"
10+
11+
"github.com/gin-gonic/gin"
12+
)
13+
14+
func (w *WebProxy) addaudit(c *gin.Context) {
15+
body, err := ioutil.ReadAll(c.Request.Body)
16+
if err != nil {
17+
strErrMsg := fmt.Sprintf("Request body read all error %v", err)
18+
c.JSON(http.StatusBadRequest, gin.H{"error": strErrMsg})
19+
return
20+
}
21+
if len(body) == 0 {
22+
c.JSON(http.StatusBadRequest, gin.H{"error": "Request body is empty"})
23+
return
24+
}
25+
req := models.AuditAddRequest{}
26+
err = json.Unmarshal(body, &req)
27+
if err != nil {
28+
strErrMsg := fmt.Sprintf("Request body JSON unmarshal error %v", err)
29+
c.JSON(http.StatusBadRequest, gin.H{"error": strErrMsg})
30+
return
31+
}
32+
//audit add "hash_array" ( "account" ) ( "description" ) ( "algorithm" )
33+
cmd, _ := dynamic.NewRequest(`dynamic-cli audit add "` + req.HashArray + `" "` + req.Account +
34+
`"` + `" "` + req.Description + `"` + `" "` + req.Algorithm + `"`)
35+
response, _ := <-w.dynamicd.ExecCmdRequest(cmd)
36+
var result interface{}
37+
err = json.Unmarshal([]byte(response), &result)
38+
if err != nil {
39+
strErrMsg := fmt.Sprintf("Results JSON unmarshal error %v", err)
40+
c.JSON(http.StatusBadRequest, gin.H{"error": strErrMsg})
41+
return
42+
}
43+
c.JSON(http.StatusOK, result)
44+
}
45+
46+
func (w *WebProxy) getaudit(c *gin.Context) {
47+
Id := c.Param("UserID")
48+
cmd, _ := dynamic.NewRequest(`dynamic-cli audit get "` + Id + `"`)
49+
response, _ := <-w.dynamicd.ExecCmdRequest(cmd)
50+
var result interface{}
51+
err := json.Unmarshal([]byte(response), &result)
52+
if err != nil {
53+
strErrMsg := fmt.Sprintf("Results JSON unmarshal error %v", err)
54+
c.JSON(http.StatusBadRequest, gin.H{"error": strErrMsg})
55+
return
56+
}
57+
c.JSON(http.StatusOK, result)
58+
}
59+
60+
func (w *WebProxy) verifyaudit(c *gin.Context) {
61+
hash := c.Param("Hash")
62+
cmd, _ := dynamic.NewRequest(`dynamic-cli audit verify "` + hash + `"`)
63+
response, _ := <-w.dynamicd.ExecCmdRequest(cmd)
64+
var result interface{}
65+
err := json.Unmarshal([]byte(response), &result)
66+
if err != nil {
67+
strErrMsg := fmt.Sprintf("Results JSON unmarshal error %v", err)
68+
c.JSON(http.StatusBadRequest, gin.H{"error": strErrMsg})
69+
return
70+
}
71+
c.JSON(http.StatusOK, result)
72+
}

https/rest/router.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ func setupBlockchainRoutes(currentVersion *gin.RouterGroup) {
191191
blockchain.GET("/users/:UserID", runner.user)
192192
blockchain.GET("/groups", runner.groups)
193193
blockchain.GET("/groups/:GroupID", runner.group)
194+
blockchain.POST("/audit", runner.addaudit)
195+
blockchain.GET("/audit/:Id", runner.getaudit)
196+
blockchain.GET("/audit/verify/:Hash", runner.verifyaudit)
194197
}
195198

196199
// TODO: follow https://rest.bitcoin.com for rest endpoints

models/audit.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package models
2+
3+
type AuditAddRequest struct {
4+
HashArray string `json:"hash_array"`
5+
Account string `json:"account"`
6+
Description string `json:"description"`
7+
Algorithm string `json:"algorithm"`
8+
}
9+
10+
type AuditVerifyRequest struct {
11+
Hash string `json:"audit_hash"`
12+
}

0 commit comments

Comments
 (0)