Add GET for a specific device

This commit is contained in:
Daniel Lundin
2019-05-11 20:38:39 +02:00
parent 679a101d4f
commit 45d0c46501
+24
View File
@@ -228,6 +228,7 @@ func (s *Server) Start() error {
router := httprouter.New()
router.GET("/", s.Index)
router.GET("/api/v1/users/:user/devices/:device", s.withAuth(s.GetDevice))
router.GET("/api/v1/users/:user/devices", s.withAuth(s.GetDevices))
router.POST("/api/v1/users/:user/devices", s.withAuth(s.CreateDevice))
@@ -306,6 +307,29 @@ func (s *Server) GetDevices(w http.ResponseWriter, r *http.Request, ps httproute
}
}
func (s *Server) GetDevice(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
user := r.Context().Value("user").(string)
usercfg := s.Config.Users[user]
if usercfg == nil {
w.WriteHeader(http.StatusNotFound)
return
}
device := ps.ByName("device")
deviceCfg := usercfg.Devices[device]
if deviceCfg == nil {
w.WriteHeader(http.StatusNotFound)
return
}
err := json.NewEncoder(w).Encode(deviceCfg)
if err != nil {
log.Error(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
}
func (s *Server) CreateDevice(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
s.mutex.Lock()
defer s.mutex.Unlock()