/
apps.go
121 lines (99 loc) · 3.37 KB
/
apps.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
package api
import (
"net/http"
"github.com/airbloc/airbloc-go/shared/adapter"
"github.com/airbloc/airbloc-go/shared/service"
"github.com/airbloc/airbloc-go/shared/service/api"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/klaytn/klaytn/common"
)
// appRegistryAPI is api wrapper of contract AppRegistry.sol
type appRegistryAPI struct {
apps adapter.IAppRegistryManager
}
// NewAppRegistryAPI makes new *appRegistryAPI struct
func NewAppRegistryAPI(backend service.Backend) (api.API, error) {
ar := adapter.NewAppRegistryManager(backend.Client())
return &appRegistryAPI{ar}, nil
}
func (api *appRegistryAPI) register(c *gin.Context) {
var req struct {
AppName string `json:"app_name" binding:"required"`
}
if err := c.ShouldBindWith(&req, binding.JSON); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if exists, err := api.apps.Exists(req.AppName); err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
} else if exists {
c.AbortWithStatusJSON(http.StatusConflict, gin.H{})
return
}
if err := api.apps.Register(c, nil, req.AppName); err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusCreated, gin.H{})
}
func (api *appRegistryAPI) unregister(c *gin.Context) {
var req struct {
AppName string `json:"app_name" binding:"required"`
}
if err := c.ShouldBindWith(&req, binding.JSON); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if err := api.apps.Unregister(c, nil, req.AppName); err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{})
}
func (api *appRegistryAPI) get(c *gin.Context) {
var req struct {
AppName string `form:"app_name" binding:"required"`
}
if err := c.ShouldBindWith(&req, binding.Query); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if exists, err := api.apps.Exists(req.AppName); err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
} else if !exists {
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"error": "cannot find app information"})
return
}
app, err := api.apps.Get(req.AppName)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, app)
}
func (api *appRegistryAPI) transferAppOwner(c *gin.Context) {
var req struct {
AppName string `json:"app_name" binding:"required"`
NewOwner string `json:"new_owner" binding:"required"`
}
if err := c.ShouldBindWith(&req, binding.JSON); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if err := api.apps.TransferAppOwner(c, nil, req.AppName, common.HexToAddress(req.NewOwner)); err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{})
}
// AttachToAPI is a registrant of an api.
func (api *appRegistryAPI) AttachToAPI(service *api.Service) {
apiMux := service.HttpServer.Group("/apps")
apiMux.GET("/", api.get)
apiMux.POST("/", api.register)
apiMux.PATCH("/", api.transferAppOwner)
apiMux.DELETE("/", api.unregister)
}