Implemented deleting a device

This commit is contained in:
Daniel Lundin
2019-05-12 18:38:53 +02:00
parent 80ac5bdd10
commit 8cf2b7f69c
+15 -2
View File
@@ -256,6 +256,7 @@ func (s *Server) Start() error {
router := httprouter.New() router := httprouter.New()
router.GET("/", s.Index) router.GET("/", s.Index)
router.GET("/api/v1/users/:user/devices/:device", s.withAuth(s.GetDevice)) router.GET("/api/v1/users/:user/devices/:device", s.withAuth(s.GetDevice))
router.DELETE("/api/v1/users/:user/devices/:device", s.withAuth(s.DeleteDevice))
router.GET("/api/v1/users/:user/devices", s.withAuth(s.GetDevices)) router.GET("/api/v1/users/:user/devices", s.withAuth(s.GetDevices))
router.POST("/api/v1/users/:user/devices", s.withAuth(s.CreateDevice)) router.POST("/api/v1/users/:user/devices", s.withAuth(s.CreateDevice))
@@ -366,15 +367,28 @@ func (s *Server) DeleteDevice(w http.ResponseWriter, r *http.Request, ps httprou
} }
device := ps.ByName("device") device := ps.ByName("device")
if usercfg.Devices[device] == nil {
w.WriteHeader(http.StatusNotFound)
return
}
delete(usercfg.Devices, device) delete(usercfg.Devices, device)
err := s.configureWireguard() err := s.Config.Write()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Fatal(err)
}
err = s.configureWireguard()
if err != nil { if err != nil {
log.Error(err) log.Error(err)
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return return
} }
log.WithField("user", user).Debug("Deleted device: ", device)
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }
@@ -423,5 +437,4 @@ func (s *Server) CreateDevice(w http.ResponseWriter, r *http.Request, ps httprou
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return return
} }
} }