11package 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
512type 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+ }
0 commit comments