Stian Aune
5 years ago
13 changed files with 889 additions and 77 deletions
-
5cmd/lucifer-server/main.go
-
265controllers/bridge-controller.go
-
256controllers/group-controller.go
-
195controllers/light-controller.go
-
2controllers/user-controller.go
-
4database/sqlite/bridge-repository.go
-
35database/sqlite/group-repository.go
-
5database/sqlite/light-repository.go
-
3internal/httperr/error.go
-
14light/driver.go
-
24light/hue/driver.go
-
136light/service.go
-
22models/light.go
@ -0,0 +1,265 @@ |
|||
package controllers |
|||
|
|||
import ( |
|||
"context" |
|||
"database/sql" |
|||
"encoding/json" |
|||
"errors" |
|||
"net/http" |
|||
"strconv" |
|||
|
|||
"git.aiterp.net/lucifer/lucifer/internal/httperr" |
|||
"git.aiterp.net/lucifer/lucifer/internal/respond" |
|||
"git.aiterp.net/lucifer/lucifer/light" |
|||
"git.aiterp.net/lucifer/lucifer/models" |
|||
"github.com/gorilla/mux" |
|||
) |
|||
|
|||
// The BridgeController is a controller for all bridge things.
|
|||
type BridgeController struct { |
|||
service *light.Service |
|||
groups models.GroupRepository |
|||
} |
|||
|
|||
// getBridges (`GET /`): Get all bridges
|
|||
func (c *BridgeController) getBridges(w http.ResponseWriter, r *http.Request) { |
|||
bridges, err := c.service.Bridges(r.Context()) |
|||
if err != nil { |
|||
httperr.Respond(w, err) |
|||
return |
|||
} |
|||
|
|||
respond.Data(w, bridges) |
|||
} |
|||
|
|||
// getBridge (`GET /:bridge_id`): Get bridge by ID
|
|||
func (c *BridgeController) getBridge(w http.ResponseWriter, r *http.Request) { |
|||
idStr := mux.Vars(r)["bridge_id"] |
|||
id, err := strconv.ParseInt(idStr, 10, 32) |
|||
if err != nil { |
|||
respond.Error(w, http.StatusBadRequest, "invalid_id", "The id "+idStr+" is not valid.") |
|||
return |
|||
} |
|||
|
|||
bridge, err := c.service.Bridge(r.Context(), int(id)) |
|||
if err == sql.ErrNoRows { |
|||
respond.Error(w, 404, "bridge_not_found", "The bridge cannot be found.") |
|||
return |
|||
} else if err != nil { |
|||
httperr.Respond(w, err) |
|||
return |
|||
} |
|||
|
|||
respond.Data(w, bridge) |
|||
} |
|||
|
|||
// postBridge (`POST /`): Post bridge
|
|||
func (c *BridgeController) postBridge(w http.ResponseWriter, r *http.Request) { |
|||
postData := struct { |
|||
Name string `json:"name"` |
|||
Driver string `json:"driver"` |
|||
Addr string `json:"addr"` |
|||
}{} |
|||
if err := json.NewDecoder(r.Body).Decode(&postData); err != nil || postData.Name == "" || postData.Driver == "" || postData.Addr == "" { |
|||
respond.Error(w, http.StatusBadRequest, "invalid_json", "Input is not valid JSON.") |
|||
return |
|||
} |
|||
|
|||
bridge, err := c.service.DirectConnect(r.Context(), postData.Driver, postData.Addr, postData.Name) |
|||
if err != nil { |
|||
httperr.Respond(w, err) |
|||
return |
|||
} |
|||
|
|||
respond.Data(w, bridge) |
|||
} |
|||
|
|||
// updateBridge (`PUT/PATCH /:bridge_id`): Update bridge by ID
|
|||
func (c *BridgeController) updateBridge(w http.ResponseWriter, r *http.Request) { |
|||
idStr := mux.Vars(r)["bridge_id"] |
|||
id, err := strconv.ParseInt(idStr, 10, 32) |
|||
if err != nil { |
|||
respond.Error(w, http.StatusBadRequest, "invalid_id", "The id "+idStr+" is not valid.") |
|||
return |
|||
} |
|||
|
|||
putData := struct { |
|||
Name string |
|||
}{} |
|||
if err := json.NewDecoder(r.Body).Decode(&putData); err != nil { |
|||
respond.Error(w, http.StatusBadRequest, "invalid_json", "Input is not valid JSON.") |
|||
return |
|||
} |
|||
if putData.Name == "" { |
|||
respond.Error(w, http.StatusBadRequest, "invalid_name", "The name cannot be blank.") |
|||
return |
|||
} |
|||
|
|||
bridge, err := c.service.Bridge(r.Context(), int(id)) |
|||
if err == sql.ErrNoRows { |
|||
respond.Error(w, 404, "bridge_not_found", "The bridge cannot be found.") |
|||
return |
|||
} else if err != nil { |
|||
httperr.Respond(w, err) |
|||
return |
|||
} |
|||
|
|||
bridge.Name = putData.Name |
|||
err = c.service.UpdateBridge(r.Context(), bridge) |
|||
if err != nil { |
|||
httperr.Respond(w, err) |
|||
return |
|||
} |
|||
|
|||
respond.Data(w, bridge) |
|||
} |
|||
|
|||
// postBridgeDiscover (`POST /:bridge_id/discover`): Delete bridge by ID
|
|||
func (c *BridgeController) postBridgeDiscover(w http.ResponseWriter, r *http.Request) { |
|||
idStr := mux.Vars(r)["bridge_id"] |
|||
id, err := strconv.ParseInt(idStr, 10, 32) |
|||
if err != nil { |
|||
respond.Error(w, http.StatusBadRequest, "invalid_id", "The id "+idStr+" is not valid.") |
|||
return |
|||
} |
|||
|
|||
bridge, err := c.service.Bridge(r.Context(), int(id)) |
|||
if err == sql.ErrNoRows { |
|||
respond.Error(w, 404, "bridge_not_found", "The bridge cannot be found.") |
|||
return |
|||
} else if err != nil { |
|||
httperr.Respond(w, err) |
|||
return |
|||
} |
|||
|
|||
err = c.service.DiscoverLights(r.Context(), bridge) |
|||
if err != nil { |
|||
httperr.Respond(w, err) |
|||
return |
|||
} |
|||
|
|||
respond.Data(w, bridge) |
|||
} |
|||
|
|||
// deleteBridge (`DELETE /:bridge_id`): Delete bridge by ID
|
|||
func (c *BridgeController) deleteBridge(w http.ResponseWriter, r *http.Request) { |
|||
idStr := mux.Vars(r)["bridge_id"] |
|||
id, err := strconv.ParseInt(idStr, 10, 32) |
|||
if err != nil { |
|||
respond.Error(w, http.StatusBadRequest, "invalid_id", "The id "+idStr+" is not valid.") |
|||
return |
|||
} |
|||
|
|||
bridge, err := c.service.Bridge(r.Context(), int(id)) |
|||
if err == sql.ErrNoRows { |
|||
respond.Error(w, 404, "bridge_not_found", "The bridge cannot be found.") |
|||
return |
|||
} else if err != nil { |
|||
httperr.Respond(w, err) |
|||
return |
|||
} |
|||
|
|||
err = c.service.DeleteBridge(r.Context(), bridge) |
|||
if err != nil { |
|||
httperr.Respond(w, err) |
|||
return |
|||
} |
|||
|
|||
respond.Data(w, bridge) |
|||
} |
|||
|
|||
// deleteBridgeLight (`DELETE /:bridge_id/light/:light_id`): Delete bridge by ID
|
|||
func (c *BridgeController) deleteBridgeLight(w http.ResponseWriter, r *http.Request) { |
|||
idStr := mux.Vars(r)["bridge_id"] |
|||
id, err := strconv.ParseInt(idStr, 10, 32) |
|||
if err != nil { |
|||
respond.Error(w, http.StatusBadRequest, "invalid_id", "The id "+idStr+" is not valid.") |
|||
return |
|||
} |
|||
|
|||
lightIDStr := mux.Vars(r)["light_id"] |
|||
lightID, err := strconv.ParseInt(lightIDStr, 10, 32) |
|||
if err != nil { |
|||
respond.Error(w, http.StatusBadRequest, "invalid_id", "The id "+lightIDStr+" is not valid.") |
|||
return |
|||
} |
|||
|
|||
bridge, err := c.service.Bridge(r.Context(), int(id)) |
|||
if err == sql.ErrNoRows { |
|||
respond.Error(w, 404, "bridge_not_found", "The bridge cannot be found.") |
|||
return |
|||
} else if err != nil { |
|||
httperr.Respond(w, err) |
|||
return |
|||
} |
|||
|
|||
light, err := c.service.Light(r.Context(), int(lightID)) |
|||
if err != nil { |
|||
httperr.Respond(w, err) |
|||
return |
|||
} |
|||
if light.BridgeID != bridge.ID { |
|||
respond.Error(w, 404, "bridge_not_found", "The bridge cannot be found.") |
|||
return |
|||
} |
|||
|
|||
err = c.service.DeleteBridgeLight(r.Context(), bridge, light) |
|||
if err != nil { |
|||
httperr.Respond(w, err) |
|||
return |
|||
} |
|||
|
|||
respond.Data(w, bridge) |
|||
} |
|||
|
|||
// Mount mounts the controller
|
|||
func (c *BridgeController) Mount(router *mux.Router, prefix string) { |
|||
sub := router.PathPrefix(prefix).Subrouter() |
|||
|
|||
sub.Use(c.adminMiddleware()) |
|||
|
|||
sub.HandleFunc("/", c.getBridges).Methods("GET") |
|||
sub.HandleFunc("/", c.postBridge).Methods("POST") |
|||
sub.HandleFunc("/{bridge_id}", c.getBridge).Methods("GET") |
|||
sub.HandleFunc("/{bridge_id}", c.updateBridge).Methods("PUT", "PATCH") |
|||
sub.HandleFunc("/{bridge_id}", c.deleteBridge).Methods("DELETE") |
|||
sub.HandleFunc("/{bridge_id}/light/{light_id}", c.deleteBridgeLight).Methods("DELETE") |
|||
sub.HandleFunc("/{bridge_id}/discover", c.postBridgeDiscover).Methods("POST") |
|||
} |
|||
|
|||
func (c *BridgeController) adminMiddleware() mux.MiddlewareFunc { |
|||
return func(next http.Handler) http.Handler { |
|||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
|||
err := c.isAdmin(r.Context()) |
|||
if err != nil { |
|||
httperr.Respond(w, err) |
|||
return |
|||
} |
|||
|
|||
next.ServeHTTP(w, r) |
|||
}) |
|||
} |
|||
} |
|||
|
|||
func (c *BridgeController) isAdmin(ctx context.Context) error { |
|||
user := models.UserFromContext(ctx) |
|||
|
|||
lonelyLights, err := c.groups.FindByID(ctx, 0) |
|||
if err != nil { |
|||
return errors.New("Lonely Lights group could not be found") |
|||
} |
|||
|
|||
if !lonelyLights.Permission(user.ID).Write { |
|||
return httperr.ErrAccessDenied |
|||
} |
|||
|
|||
return nil |
|||
} |
|||
|
|||
// NewBridgeController makes a new bridge controller
|
|||
func NewBridgeController(service *light.Service, groups models.GroupRepository) *BridgeController { |
|||
return &BridgeController{ |
|||
service: service, |
|||
groups: groups, |
|||
} |
|||
} |
@ -0,0 +1,195 @@ |
|||
package controllers |
|||
|
|||
import ( |
|||
"database/sql" |
|||
"encoding/json" |
|||
"net/http" |
|||
"strconv" |
|||
|
|||
"git.aiterp.net/lucifer/lucifer/light" |
|||
|
|||
"git.aiterp.net/lucifer/lucifer/internal/httperr" |
|||
|
|||
"git.aiterp.net/lucifer/lucifer/internal/respond" |
|||
"git.aiterp.net/lucifer/lucifer/models" |
|||
"github.com/gorilla/mux" |
|||
"golang.org/x/sync/errgroup" |
|||
) |
|||
|
|||
// The LightController is a controller for light matters.
|
|||
type LightController struct { |
|||
service *light.Service |
|||
groups models.GroupRepository |
|||
users models.UserRepository |
|||
lights models.LightRepository |
|||
} |
|||
|
|||
// getLights (`GET /:id`): Get user by id
|
|||
func (c *LightController) getLights(w http.ResponseWriter, r *http.Request) { |
|||
user := models.UserFromContext(r.Context()) |
|||
|
|||
groups, err := c.groups.ListByUser(r.Context(), *user) |
|||
if err != nil { |
|||
httperr.Respond(w, err) |
|||
return |
|||
} |
|||
|
|||
allLights := make([]models.Light, 0, len(groups)*8) |
|||
|
|||
eg, egCtx := errgroup.WithContext(r.Context()) |
|||
for i := range groups { |
|||
group := groups[i] |
|||
|
|||
eg.Go(func() error { |
|||
lights, err := c.lights.ListByGroup(egCtx, group) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
|
|||
allLights = append(allLights, lights...) |
|||
|
|||
return nil |
|||
}) |
|||
} |
|||
|
|||
if err := eg.Wait(); err != nil { |
|||
httperr.Respond(w, err) |
|||
return |
|||
} |
|||
|
|||
respond.Data(w, allLights) |
|||
} |
|||
|
|||
func (c *LightController) getLight(w http.ResponseWriter, r *http.Request) { |
|||
_, light, err := c.findLight(r) |
|||
if err != nil { |
|||
httperr.Respond(w, err) |
|||
return |
|||
} |
|||
|
|||
respond.Data(w, light) |
|||
} |
|||
|
|||
func (c *LightController) updateLight(w http.ResponseWriter, r *http.Request) { |
|||
patch := struct { |
|||
Color *string `json:"color,omitempty"` |
|||
Brightness *int `json:"brightness,omitempty"` |
|||
On *bool `json:"on,omitempty"` |
|||
Name *string `json:"name,omitempty"` |
|||
GroupID *int `json:"groupId,omitempty"` |
|||
}{} |
|||
|
|||
if err := json.NewDecoder(r.Body).Decode(&patch); err != nil { |
|||
respond.Error(w, http.StatusBadRequest, "invalid_json", "Input is not valid JSON.") |
|||
return |
|||
} |
|||
|
|||
group, light, err := c.findLight(r) |
|||
if err != nil { |
|||
httperr.Respond(w, err) |
|||
return |
|||
} |
|||
|
|||
if patch.Color != nil { |
|||
err := light.SetColor(*patch.Color) |
|||
if err != nil { |
|||
httperr.Respond(w, err) |
|||
return |
|||
} |
|||
} |
|||
if patch.Name != nil { |
|||
if len(*patch.Name) == 0 { |
|||
respond.Error(w, 400, "invalid_name", "The name is empty.") |
|||
return |
|||
} |
|||
|
|||
light.Name = *patch.Name |
|||
} |
|||
if patch.Brightness != nil { |
|||
if *patch.Brightness < 0 || *patch.Brightness > 255 { |
|||
respond.Error(w, 400, "invalid_brightness", "The brightness must be a value between 0-255 inclusive.") |
|||
return |
|||
} |
|||
|
|||
light.Brightness = uint8(*patch.Brightness) |
|||
} |
|||
if patch.On != nil { |
|||
light.On = *patch.On |
|||
} |
|||
if patch.GroupID != nil && *patch.GroupID != light.GroupID { |
|||
user := models.UserFromContext(r.Context()) |
|||
|
|||
if !group.Permission(user.ID).Delete { |
|||
respond.Error(w, 403, "cannot_move_out", "You are not permitted to delete lights from group.") |
|||
return |
|||
} |
|||
|
|||
// Anyone is allowed to move lights TO group 0 (Lonely Lights) as it's the closest thing there is
|
|||
// to deleting lights.
|
|||
if *patch.GroupID != 0 { |
|||
targetGroup, err := c.groups.FindByID(r.Context(), *patch.GroupID) |
|||
if err != nil { |
|||
respond.Error(w, 404, "group_not_found", "The group could not be found.") |
|||
return |
|||
} |
|||
if !targetGroup.Permission(user.ID).Create { |
|||
respond.Error(w, 403, "cannot_move_in", "You are not permitted to create lights in target group.") |
|||
return |
|||
} |
|||
} |
|||
|
|||
light.GroupID = *patch.GroupID |
|||
} |
|||
|
|||
err = c.service.UpdateLight(r.Context(), light) |
|||
if err != nil { |
|||
httperr.Respond(w, err) |
|||
return |
|||
} |
|||
|
|||
respond.Data(w, light) |
|||
} |
|||
|
|||
// Mount mounts the controller
|
|||
func (c *LightController) Mount(router *mux.Router, prefix string) { |
|||
sub := router.PathPrefix(prefix).Subrouter() |
|||
|
|||
sub.HandleFunc("/", c.getLights).Methods("GET") |
|||
sub.HandleFunc("/{id}", c.getLight).Methods("GET") |
|||
sub.HandleFunc("/{id}", c.updateLight).Methods("PATCH", "PUT") |
|||
} |
|||
|
|||
func (c *LightController) findLight(r *http.Request) (models.Group, models.Light, error) { |
|||
user := models.UserFromContext(r.Context()) |
|||
|
|||
idStr := mux.Vars(r)["id"] |
|||
id, err := strconv.ParseInt(idStr, 10, 32) |
|||
if err != nil { |
|||
return models.Group{}, models.Light{}, &httperr.Error{Status: http.StatusForbidden, Kind: "invalid_id", Message: "The light id " + idStr + " is not valid."} |
|||
} |
|||
|
|||
light, err := c.lights.FindByID(r.Context(), int(id)) |
|||
if err == sql.ErrNoRows { |
|||
return models.Group{}, models.Light{}, httperr.NotFound("Light") |
|||
} else if err != nil { |
|||
return models.Group{}, models.Light{}, err |
|||
} |
|||
|
|||
group, err := c.groups.FindByID(r.Context(), light.GroupID) |
|||
if err == sql.ErrNoRows { |
|||
return models.Group{}, models.Light{}, httperr.NotFound("Group") |
|||
} else if err != nil { |
|||
return models.Group{}, models.Light{}, err |
|||
} |
|||
|
|||
if !group.Permission(user.ID).Read { |
|||
return models.Group{}, models.Light{}, &httperr.Error{Status: http.StatusForbidden, Kind: "permission_denied", Message: "Thou canst not see the light."} |
|||
} |
|||
|
|||
return group, light, nil |
|||
} |
|||
|
|||
// NewLightController creates a new LightController.
|
|||
func NewLightController(service *light.Service, groups models.GroupRepository, users models.UserRepository, lights models.LightRepository) *LightController { |
|||
return &LightController{service: service, groups: groups, users: users, lights: lights} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue