@@ -68,27 +68,26 @@ func (m *messageResponse) Encode() ([]byte, error) {
6868
6969// StartAdminInterface - starts admin interface web server
7070func (d * DBClient ) StartAdminInterface () {
71- go func () {
72- // starting admin interface
73- mux := getBoneRouter (* d )
74- n := negroni .Classic ()
7571
76- logLevel := log .ErrorLevel
72+ // starting admin interface
73+ mux := getBoneRouter (* d )
74+ n := negroni .Classic ()
7775
78- if d .Cfg .Verbose {
79- logLevel = log .DebugLevel
80- }
76+ logLevel := log .ErrorLevel
8177
82- n .Use (negronilogrus .NewCustomMiddleware (logLevel , & log.JSONFormatter {}, "admin" ))
83- n .UseHandler (mux )
78+ if d .Cfg .Verbose {
79+ logLevel = log .DebugLevel
80+ }
8481
85- // admin interface starting message
86- log .WithFields (log.Fields {
87- "AdminPort" : d .Cfg .AdminPort ,
88- }).Info ("Admin interface is starting..." )
82+ n .Use (negronilogrus .NewCustomMiddleware (logLevel , & log.JSONFormatter {}, "admin" ))
83+ n .UseHandler (mux )
8984
90- n .Run (fmt .Sprintf (":%s" , d .Cfg .AdminPort ))
91- }()
85+ // admin interface starting message
86+ log .WithFields (log.Fields {
87+ "AdminPort" : d .Cfg .AdminPort ,
88+ }).Info ("Admin interface is starting..." )
89+
90+ n .Run (fmt .Sprintf (":%s" , d .Cfg .AdminPort ))
9291}
9392
9493// getBoneRouter returns mux for admin interface
@@ -193,14 +192,26 @@ func getBoneRouter(d DBClient) *bone.Mux {
193192
194193// AllRecordsHandler returns JSON content type http response
195194func (d * DBClient ) AllRecordsHandler (w http.ResponseWriter , req * http.Request , next http.HandlerFunc ) {
196- records , err := d .Cache .GetAllRequests ()
195+ records , err := d .Cache .GetAllValues ()
197196
198197 if err == nil {
199198
199+ var payloads []Payload
200+
201+ for _ , v := range records {
202+ if payload , err := decodePayload (v ); err == nil {
203+ payloads = append (payloads , * payload )
204+ } else {
205+ log .Error (err )
206+ http .Error (w , err .Error (), http .StatusInternalServerError )
207+ return
208+ }
209+ }
210+
200211 w .Header ().Set ("Content-Type" , "application/json" )
201212
202213 var response recordedRequests
203- response .Data = records
214+ response .Data = payloads
204215 b , err := json .Marshal (response )
205216
206217 if err != nil {
@@ -567,27 +578,39 @@ func (d *DBClient) StateHandler(w http.ResponseWriter, r *http.Request, next htt
567578 "synthesize" : true ,
568579 }
569580
570- if ! availableModes [sr .Mode ] {
581+ if sr .Mode != "" {
582+ if ! availableModes [sr .Mode ] {
583+ log .WithFields (log.Fields {
584+ "suppliedMode" : sr .Mode ,
585+ }).Error ("Wrong mode found, can't change state" )
586+ http .Error (w , "Bad mode supplied, available modes: virtualize, capture, modify, synthesize." , 400 )
587+ return
588+ }
571589 log .WithFields (log.Fields {
572- "suppliedMode" : sr .Mode ,
573- }).Error ("Wrong mode found, can't change state" )
574- http .Error (w , "Bad mode supplied, available modes: virtualize, capture, modify, synthesize." , 400 )
575- return
576- }
590+ "newState" : sr .Mode ,
591+ "body" : string (body ),
592+ "destination" : sr .Destination ,
593+ }).Info ("Handling state change request!" )
577594
578- log .WithFields (log.Fields {
579- "newState" : sr .Mode ,
580- "body" : string (body ),
581- }).Info ("Handling state change request!" )
595+ // setting new state
596+ d .Cfg .SetMode (sr .Mode )
582597
583- // setting new state
584- d .Cfg .SetMode (sr .Mode )
598+ }
599+
600+ // checking whether we should update destination
601+ if sr .Destination != "" {
602+ err := d .UpdateDestination (sr .Destination )
603+ if err != nil {
604+ http .Error (w , fmt .Sprintf ("Error while updating destination: %s" , err .Error ()), 500 )
605+ return
606+ }
607+ }
585608
586609 var en Entry
587610 en .ActionType = ActionTypeConfigurationChanged
588611 en .Message = "changed"
589612 en .Time = time .Now ()
590- en .Data = [] byte ( "sr.Mode" )
613+ en .Data = body
591614
592615 if err := d .Hooks .Fire (ActionTypeConfigurationChanged , & en ); err != nil {
593616 log .WithFields (log.Fields {
@@ -615,11 +638,7 @@ func (d *DBClient) AllMetadataHandler(w http.ResponseWriter, req *http.Request,
615638 w .Header ().Set ("Content-Type" , "application/json" )
616639
617640 var response storedMetadata
618- respMap := make (map [string ]string )
619- for _ , v := range metadata {
620- respMap [v .Key ] = v .Value
621- }
622- response .Data = respMap
641+ response .Data = metadata
623642 b , err := json .Marshal (response )
624643
625644 if err != nil {
@@ -689,7 +708,7 @@ func (d *DBClient) SetMetadataHandler(w http.ResponseWriter, req *http.Request,
689708 w .WriteHeader (400 )
690709
691710 } else {
692- err = d .MD .Set ([] byte ( sm .Key ), [] byte ( sm .Value ) )
711+ err = d .MD .Set (sm .Key , sm .Value )
693712 if err != nil {
694713 mr .Message = fmt .Sprintf ("Failed to set metadata. Error: %s" , err .Error ())
695714 w .WriteHeader (500 )
0 commit comments