Skip to content
Permalink
Browse files

expand standard response and handle device delete

  • Loading branch information...
cbrake committed Sep 10, 2019
1 parent e555703 commit 5bcc13911bb0a60ae1e7c8daac53c5f16daf7bac
Showing with 39 additions and 15 deletions.
  1. +22 −8 api/devices.go
  2. +9 −5 api/server.go
  3. +3 −2 data/api.go
  4. +5 −0 db/db.go
@@ -28,7 +28,7 @@ func (h *Devices) processConfig(res http.ResponseWriter, req *http.Request, id s
}

en := json.NewEncoder(res)
en.Encode(data.Response{Success: true})
en.Encode(data.StandardResponse{Success: true, ID: id})
}

func (h *Devices) processSamples(res http.ResponseWriter, req *http.Request, id string) {
@@ -48,7 +48,7 @@ func (h *Devices) processSamples(res http.ResponseWriter, req *http.Request, id
}

en := json.NewEncoder(res)
en.Encode(data.Response{Success: true})
en.Encode(data.StandardResponse{Success: true, ID: id})
}

// Top level handler for http requests in the coap-server process
@@ -87,12 +87,26 @@ func (h *Devices) ServeHTTP(res http.ResponseWriter, req *http.Request) {
http.Error(res, "invalid method", http.StatusMethodNotAllowed)
}
} else {
device, err := h.db.Device(id)
if err != nil {
http.Error(res, err.Error(), http.StatusNotFound)
} else {
en := json.NewEncoder(res)
en.Encode(device)
switch req.Method {
case http.MethodGet:
device, err := h.db.Device(id)
if err != nil {
http.Error(res, err.Error(), http.StatusNotFound)
} else {
en := json.NewEncoder(res)
en.Encode(device)
}
case http.MethodDelete:
err := h.db.DeviceDelete(id)
if err != nil {
http.Error(res, err.Error(), http.StatusNotFound)
} else {
en := json.NewEncoder(res)
en.Encode(data.StandardResponse{Success: true, ID: id})
}

default:
http.Error(res, "invalid method", http.StatusMethodNotAllowed)
}
}
}
@@ -37,13 +37,16 @@ type App struct {
PublicHandler http.Handler
IndexHandler http.Handler
V1ApiHandler http.Handler
Debug bool
}

// Top level handler for http requests in the coap-server process
func (h *App) ServeHTTP(res http.ResponseWriter, req *http.Request) {
var head string

fmt.Println("Path: ", req.URL.Path)
if h.Debug {
fmt.Printf("HTTP %v: %v\n", req.Method, req.URL.Path)
}

if req.URL.Path == "/" {
h.IndexHandler.ServeHTTP(res, req)
@@ -62,17 +65,18 @@ func (h *App) ServeHTTP(res http.ResponseWriter, req *http.Request) {

// NewAppHandler returns a new application (root) http handler
func NewAppHandler(db *db.Db, getAsset func(string) []byte,
filesystem http.FileSystem) http.Handler {
filesystem http.FileSystem, debug bool) http.Handler {
return &App{
PublicHandler: http.FileServer(filesystem),
IndexHandler: NewIndexHandler(getAsset),
V1ApiHandler: NewV1Handler(db),
Debug: debug,
}
}

// Server starts a API server instance
func Server(getAsset func(string) []byte, filesystem http.FileSystem) error {
log.Println("Starting http server")
func Server(getAsset func(string) []byte, filesystem http.FileSystem, debug bool) error {
log.Println("Starting http server, debug: ", debug)

port := os.Getenv("SIOT_PORT")
if port == "" {
@@ -92,5 +96,5 @@ func Server(getAsset func(string) []byte, filesystem http.FileSystem) error {

log.Println("Starting portal on port: ", port)
address := fmt.Sprintf(":%s", port)
return http.ListenAndServe(address, NewAppHandler(db, getAsset, filesystem))
return http.ListenAndServe(address, NewAppHandler(db, getAsset, filesystem, debug))
}
@@ -1,7 +1,8 @@
package data

// Response is the standard response to any request
type Response struct {
// StandardResponse is the standard response to any request
type StandardResponse struct {
Success bool `json:"success"`
Error string `json:"error,omitempty"`
ID string `json:"id,omitempty"`
}
@@ -75,6 +75,11 @@ func (db *Db) Device(id string) (ret data.Device, err error) {
return
}

// DeviceDelete deletes a device from the database
func (db *Db) DeviceDelete(id string) error {
return db.store.Delete(id, data.Device{})
}

// Devices returns all devices
func (db *Db) Devices() (ret []data.Device, err error) {
err = db.store.Find(&ret, nil)

0 comments on commit 5bcc139

Please sign in to comment.
You can’t perform that action at this time.