Skip to content
This repository was archived by the owner on Sep 17, 2019. It is now read-only.

Commit 2f8a226

Browse files
committed
signal rest
1 parent 3922e44 commit 2f8a226

12 files changed

Lines changed: 320 additions & 83 deletions

File tree

‎common/oas.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ func InitOasClient(endpoint string) error {
1919
return fmt.Errorf("Bad config value type (expect bool): apiapi::oasport")
2020
}
2121

22-
DefaultClient = oas.NewOasClient(
23-
a.Endpoint,
22+
DefaultOasClient = oas.NewOasClient(
23+
endpoint,
2424
beego.AppConfig.String("aliapi::apikey"),
2525
beego.AppConfig.String("aliapi::secret"),
2626
oasPort,
@@ -32,7 +32,7 @@ func InitOasClient(endpoint string) error {
3232
func GetOasVaultId(name string) (string, error) {
3333
v := new(oas.VaultsList)
3434
for {
35-
id, v, err := DefaultClient.ListVaults(-1, v.Marker)
35+
id, v, err := DefaultOasClient.ListVaults(-1, v.Marker)
3636
beego.Debug("OAS request ID:", id)
3737
if err != nil {
3838

‎common/redis.go‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package common
2+
3+
import (
4+
"encoding/json"
5+
6+
"github.com/astaxie/beego"
7+
"github.com/astaxie/beego/cache"
8+
_ "github.com/astaxie/beego/cache/redis"
9+
)
10+
11+
const DefaultRedisKey = "ModuleAB"
12+
13+
var DefaultRedisClient cache.Cache
14+
15+
func init() {
16+
var err error
17+
redisConf := make(map[string]string)
18+
redisConf["conn"] = beego.AppConfig.String("redis::host")
19+
redisConf["password"] = beego.AppConfig.String("reids::password")
20+
redisConf["key"] = beego.AppConfig.String("redis::key")
21+
b, _ := json.Marshal(redisConf)
22+
DefaultRedisClient, err = cache.NewCache("redis", string(b))
23+
if err != nil {
24+
beego.Alert("Connect to redis failed:", err)
25+
}
26+
}

‎controllers/app_sets.go‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (a *AppSetsController) Get() {
5757
appSet := &models.AppSets{
5858
Name: name,
5959
}
60-
appSets, err := models.GetAppSets(appSet)
60+
appSets, err := models.GetAppSets(appSet, 0, 0)
6161
if err != nil {
6262
a.Data["json"] = map[string]string{
6363
"message": fmt.Sprint("Failed to get with name:", name),
@@ -84,8 +84,11 @@ func (a *AppSetsController) Get() {
8484

8585
// @router / [get]
8686
func (a *AppSetsController) GetAll() {
87+
limit, _ := a.GetInt("limit", 0)
88+
index, _ := a.GetInt("index", 0)
89+
8790
appSet := &models.AppSets{}
88-
appSets, err := models.GetAppSets(appSet)
91+
appSets, err := models.GetAppSets(appSet, limit, index)
8992
if err != nil {
9093
a.Data["json"] = map[string]string{
9194
"message": fmt.Sprint("Failed to get"),
@@ -117,7 +120,7 @@ func (a *AppSetsController) Delete() {
117120
appSet := &models.AppSets{
118121
Name: name,
119122
}
120-
appSets, err := models.GetAppSets(appSet)
123+
appSets, err := models.GetAppSets(appSet, 0, 0)
121124
if err != nil {
122125
a.Data["json"] = map[string]string{
123126
"message": fmt.Sprint("Failed to get with name:", name),
@@ -160,7 +163,7 @@ func (a *AppSetsController) Put() {
160163
appSet := &models.AppSets{
161164
Name: name,
162165
}
163-
appSets, err := models.GetAppSets(appSet)
166+
appSets, err := models.GetAppSets(appSet, 0, 0)
164167
if err != nil {
165168
a.Data["json"] = map[string]string{
166169
"message": fmt.Sprint("Failed to get with name:", name),

‎controllers/client.go‎

Lines changed: 199 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
package controllers
22

3-
import "github.com/astaxie/beego"
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"moduleab_server/models"
7+
"net/http"
8+
9+
"github.com/astaxie/beego"
10+
)
411

512
type ClientController struct {
613
beego.Controller
@@ -11,11 +18,200 @@ type ClientController struct {
1118
// @Param body body models.Hosts true "body for host content"
1219
// @Success 200
1320
// @Failure 403 body is empty
14-
// @router / [get]
15-
func (c *ClientController) Get() {
21+
// @router /config [get]
22+
func (c *ClientController) GetAliConfig() {
1623
c.Data["json"] = map[string]string{
1724
"ali_key": beego.AppConfig.String("aliapi::apikey"),
1825
"ali_secret": beego.AppConfig.String("aliapi::secret"),
1926
}
2027
c.ServeJSON()
2128
}
29+
30+
// @router /signal/:name [get]
31+
func (c *ClientController) GetSignals() {
32+
name := c.GetString(":name")
33+
beego.Debug("[C] Got name:", name)
34+
if name != "" {
35+
host := &models.Hosts{
36+
Name: name,
37+
}
38+
hosts, err := models.GetHosts(host, 0, 0)
39+
if err != nil {
40+
c.Data["json"] = map[string]string{
41+
"message": fmt.Sprint("Failed to get with name:", name),
42+
"error": err.Error(),
43+
}
44+
beego.Warn("[C] Got error:", err)
45+
c.Ctx.Output.SetStatus(http.StatusInternalServerError)
46+
c.ServeJSON()
47+
return
48+
}
49+
if len(hosts) == 0 {
50+
beego.Debug("[C] Got nothing with name:", name)
51+
c.Ctx.Output.SetStatus(http.StatusNotFound)
52+
c.ServeJSON()
53+
return
54+
}
55+
c.Data["json"] = models.GetSignals(hosts[0].Id)
56+
c.Ctx.Output.SetStatus(http.StatusOK)
57+
c.ServeJSON()
58+
return
59+
60+
}
61+
}
62+
63+
// @router /signal/:name/:id [get]
64+
func (c *ClientController) GetSignal() {
65+
name := c.GetString(":name")
66+
id := c.GetString(":id")
67+
beego.Debug("[C] Got name:", name)
68+
if name != "" {
69+
host := &models.Hosts{
70+
Name: name,
71+
}
72+
hosts, err := models.GetHosts(host, 0, 0)
73+
if err != nil {
74+
c.Data["json"] = map[string]string{
75+
"message": fmt.Sprint("Failed to get with name:", name),
76+
"error": err.Error(),
77+
}
78+
beego.Warn("[C] Got error:", err)
79+
c.Ctx.Output.SetStatus(http.StatusInternalServerError)
80+
c.ServeJSON()
81+
return
82+
}
83+
if len(hosts) == 0 {
84+
beego.Debug("[C] Got nothing with name:", name)
85+
c.Ctx.Output.SetStatus(http.StatusNotFound)
86+
c.ServeJSON()
87+
return
88+
}
89+
signals := models.GetSignals(hosts[0].Id)
90+
for _, v := range signals {
91+
if v["id"] == id {
92+
c.Data["json"] = v
93+
c.Ctx.Output.SetStatus(http.StatusOK)
94+
c.ServeJSON()
95+
return
96+
}
97+
}
98+
99+
c.Data["json"] = map[string]string{
100+
"message": fmt.Sprintf("Got nothing with id:", id),
101+
}
102+
c.Ctx.Output.SetStatus(http.StatusNotFound)
103+
c.ServeJSON()
104+
return
105+
106+
}
107+
}
108+
109+
// @router /signal/:name [post]
110+
func (c *ClientController) PostSignal() {
111+
name := c.GetString(":name")
112+
beego.Debug("[C] Got name:", name)
113+
if name != "" {
114+
host := &models.Hosts{
115+
Name: name,
116+
}
117+
hosts, err := models.GetHosts(host, 0, 0)
118+
if err != nil {
119+
c.Data["json"] = map[string]string{
120+
"message": fmt.Sprint("Failed to get with name:", name),
121+
"error": err.Error(),
122+
}
123+
beego.Warn("[C] Got error:", err)
124+
c.Ctx.Output.SetStatus(http.StatusInternalServerError)
125+
c.ServeJSON()
126+
return
127+
}
128+
if len(hosts) == 0 {
129+
beego.Debug("[C] Got nothing with name:", name)
130+
c.Ctx.Output.SetStatus(http.StatusNotFound)
131+
c.ServeJSON()
132+
return
133+
}
134+
var signal models.Signal
135+
err = json.Unmarshal(c.Ctx.Input.RequestBody, &signal)
136+
if err != nil {
137+
beego.Warn("[C] Got error:", err)
138+
c.Data["json"] = map[string]string{
139+
"message": "Bad request",
140+
"error": err.Error(),
141+
}
142+
c.Ctx.Output.SetStatus(http.StatusBadRequest)
143+
c.ServeJSON()
144+
return
145+
}
146+
beego.Debug("[C] Got data:", signal)
147+
id, err := models.AddSignal(hosts[0].Id, signal)
148+
if err != nil {
149+
beego.Warn("[C] Got error:", err)
150+
c.Data["json"] = map[string]string{
151+
"message": "Failed to add new signal",
152+
"error": err.Error(),
153+
}
154+
c.Ctx.Output.SetStatus(http.StatusBadRequest)
155+
c.ServeJSON()
156+
return
157+
}
158+
c.Data["json"] = map[string]string{
159+
"id": id,
160+
}
161+
c.Ctx.Output.SetStatus(http.StatusCreated)
162+
c.ServeJSON()
163+
return
164+
}
165+
}
166+
167+
// @router /signal/:name/:id [delete]
168+
func (c *ClientController) DeleteSignal() {
169+
name := c.GetString(":name")
170+
id := c.GetString(":id")
171+
beego.Debug("[C] Got name:", name)
172+
if name != "" {
173+
host := &models.Hosts{
174+
Name: name,
175+
}
176+
hosts, err := models.GetHosts(host, 0, 0)
177+
if err != nil {
178+
c.Data["json"] = map[string]string{
179+
"message": fmt.Sprint("Failed to get with name:", name),
180+
"error": err.Error(),
181+
}
182+
beego.Warn("[C] Got error:", err)
183+
c.Ctx.Output.SetStatus(http.StatusInternalServerError)
184+
c.ServeJSON()
185+
return
186+
}
187+
if len(hosts) == 0 {
188+
beego.Debug("[C] Got nothing with name:", name)
189+
c.Ctx.Output.SetStatus(http.StatusNotFound)
190+
c.ServeJSON()
191+
return
192+
}
193+
err = models.DeleteSignal(hosts[0].Id, id)
194+
if err != nil {
195+
beego.Warn("[C] Got error:", err)
196+
c.Data["json"] = map[string]string{
197+
"message": "Delete failed",
198+
"error": err.Error(),
199+
}
200+
if err == models.ErrorSignalNotFound {
201+
c.Ctx.Output.SetStatus(http.StatusNotFound)
202+
} else {
203+
c.Ctx.Output.SetStatus(http.StatusInternalServerError)
204+
}
205+
c.ServeJSON()
206+
return
207+
}
208+
209+
c.Data["json"] = map[string]string{
210+
"message": fmt.Sprintf("Got nothing with id:", id),
211+
}
212+
c.Ctx.Output.SetStatus(http.StatusNotFound)
213+
c.ServeJSON()
214+
return
215+
216+
}
217+
}

‎controllers/hosts.go‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (h *HostsController) Get() {
6767
host := &models.Hosts{
6868
Name: name,
6969
}
70-
hosts, err := models.GetHosts(host)
70+
hosts, err := models.GetHosts(host, 0, 0)
7171
if err != nil {
7272
h.Data["json"] = map[string]string{
7373
"message": fmt.Sprint("Failed to get with name:", name),
@@ -97,8 +97,11 @@ func (h *HostsController) Get() {
9797
// @Success 200
9898
// @router / [get]
9999
func (h *HostsController) GetAll() {
100+
limit, _ := h.GetInt("limit", 0)
101+
index, _ := h.GetInt("index", 0)
102+
100103
host := &models.Hosts{}
101-
hosts, err := models.GetHosts(host)
104+
hosts, err := models.GetHosts(host, limit, index)
102105
if err != nil {
103106
h.Data["json"] = map[string]string{
104107
"message": fmt.Sprint("Failed to get"),
@@ -134,7 +137,7 @@ func (h *HostsController) Delete() {
134137
host := &models.Hosts{
135138
Name: name,
136139
}
137-
hosts, err := models.GetHosts(host)
140+
hosts, err := models.GetHosts(host, 0, 0)
138141
if err != nil {
139142
h.Data["json"] = map[string]string{
140143
"message": fmt.Sprint("Failed to get with name:", name),
@@ -176,14 +179,14 @@ func (h *HostsController) Delete() {
176179
// @router /:name [put]
177180
func (h *HostsController) Put() {
178181
name := h.GetString(":name")
179-
beego.Debug("[C] Got host name:", name)
182+
beego.Debug("[C] Got name:", name)
180183
if name != "" {
181184
host := &models.Hosts{
182185
Name: name,
183186
// 外键关系也需要初始化,否则会出现问题,反向关系则不用
184187
AppSet: new(models.AppSets),
185188
}
186-
hosts, err := models.GetHosts(host)
189+
hosts, err := models.GetHosts(host, 0, 0)
187190
if err != nil {
188191
h.Data["json"] = map[string]string{
189192
"message": fmt.Sprint("Failed to get with name:", name),

0 commit comments

Comments
 (0)