//Create devices func (this *DeviceHandller) CreateDevice(w http.ResponseWriter, r *http.Request) { log.Info("Creating Device") d, e := parseGetReq(r) if e != nil { ProcessError(w, r, e) return } if er := validateCreateDevice(d); er != nil { ProcessError(w, r, er) return } if d, err := d.CreateDevice(); err != nil { if d != nil { output := map[string]interface{}{"Error": err.Error(), "Status": d} ProcessResponse(w, r, output) } else { ProcessError(w, r, err) } } else { ProcessResponse(w, r, "Device Created Successfully..") } return }
func main() { config.LoadConfig() mux := http.NewServeMux() //Create device Handler dh := handler.NewDeviceHandler() mux.HandleFunc(consts.CREATE_DEVICE, dh.CreateDevice) mux.HandleFunc(consts.DEVICE_LIST, dh.GetDevices) log.Info("Listening at port:", consts.PORT) if err := http.ListenAndServe(fmt.Sprintf(":%s", consts.PORT), mux); err != nil { syslog.Printf("Listen and serve %s exits with error: %s", consts.PORT, err.Error()) } }
//Get Devices func (this *DeviceHandller) GetDevices(w http.ResponseWriter, r *http.Request) { log.Info("Getting Devices") d := models.NewDevice() d, e := parseGetReq(r) if e != nil { ProcessError(w, r, e) return } if len(d.IpAddress) > 0 { if device, err := d.GetDevicesByIp(d.IpAddress); err != nil { ProcessError(w, r, err) } else { ProcessResponse(w, r, device) } } else { if devices, err := d.GetDevices(); err != nil { ProcessError(w, r, err) } else { ProcessResponse(w, r, devices) } } }