Exemplo n.º 1
0
func (self *CommandModule) LoadRoutes(router *httprouter.Router) error {
	router.GET(`/api/commands/list`, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
		util.Respond(w, http.StatusOK, self.Commands, nil)
	})

	router.PUT(`/api/commands/run/:name`, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
		name := params.ByName(`name`)

		if command, ok := self.Commands[name]; ok {
			if results, err := command.Execute(); err == nil {
				util.Respond(w, http.StatusOK, results, nil)
			} else {
				util.Respond(w, http.StatusOK, results, nil)
			}
		} else {
			util.Respond(w, http.StatusNotFound, nil, fmt.Errorf("Cannot find a command called '%s'", name))
		}
	})

	return nil
}
Exemplo n.º 2
0
func (self *API) loadRoutes() error {
	self.router.GET(`/api/status`, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
		util.Respond(w, http.StatusOK, map[string]interface{}{
			`started_at`: util.StartedAt,
		}, nil)
	})

	for _, module := range self.Modules {
		log.Debugf("Loading routes for %T", module)

		if err := module.LoadRoutes(self.router); err != nil {
			return err
		}
	}

	return nil
}
Exemplo n.º 3
0
func (self *SoundctlModule) LoadRoutes(router *httprouter.Router) error {
	router.GET(`/api/soundctl/backends`, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
		keys := make([]string, 0)

		for key, _ := range self.Backends {
			keys = append(keys, key)
		}

		util.Respond(w, http.StatusOK, keys, nil)
	})

	router.GET(`/api/soundctl/backends/:name`, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
		name := params.ByName(`name`)

		if backend, ok := self.Backends[name]; ok {
			util.Respond(w, http.StatusOK, backend, nil)
		} else {
			util.Respond(w, http.StatusNotFound, nil, fmt.Errorf("Unable to locate backend '%s'", name))
		}
	})

	router.GET(`/api/soundctl/backends/:name/outputs/:output`, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
		if output, err := self.getNamedOutput(params.ByName(`name`), params.ByName(`output`)); err == nil {
			util.Respond(w, http.StatusOK, output, nil)
		} else {
			util.Respond(w, http.StatusNotFound, nil, err)
		}
	})

	router.GET(`/api/soundctl/backends/:name/outputs-property/:key/:value`, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
		name := params.ByName(`name`)
		propName := params.ByName(`key`)
		propValue := params.ByName(`value`)

		if backend, ok := self.Backends[name]; ok {
			if outputs := backend.GetOutputsByProperty(propName, propValue); len(outputs) > 0 {
				util.Respond(w, http.StatusOK, outputs, nil)
			} else {
				util.Respond(w, http.StatusNotFound, nil, fmt.Errorf("Unable to locate any outputs with property %s=%s on backend '%s'", propName, propValue, name))
			}
		} else {
			util.Respond(w, http.StatusNotFound, nil, fmt.Errorf("Unable to locate backend '%s'", name))
		}
	})

	router.PUT(`/api/soundctl/backends/:name/outputs/:output/set-default`, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
	})

	router.PUT(`/api/soundctl/backends/:name/outputs/:output/mute`, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
		if output, err := self.getNamedOutput(params.ByName(`name`), params.ByName(`output`)); err == nil {
			if err := output.Mute(); err == nil {
				util.Respond(w, http.StatusNoContent, nil, nil)
			} else {
				util.Respond(w, http.StatusInternalServerError, nil, err)
			}
		} else {
			util.Respond(w, http.StatusNotFound, nil, err)
		}
	})

	router.PUT(`/api/soundctl/backends/:name/outputs/:output/unmute`, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
		if output, err := self.getNamedOutput(params.ByName(`name`), params.ByName(`output`)); err == nil {
			if err := output.Unmute(); err == nil {
				util.Respond(w, http.StatusNoContent, nil, nil)
			} else {
				util.Respond(w, http.StatusInternalServerError, nil, err)
			}
		} else {
			util.Respond(w, http.StatusNotFound, nil, err)
		}
	})

	router.PUT(`/api/soundctl/backends/:name/outputs/:output/toggle`, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
		if output, err := self.getNamedOutput(params.ByName(`name`), params.ByName(`output`)); err == nil {
			if err := output.ToggleMute(); err == nil {
				util.Respond(w, http.StatusNoContent, nil, nil)
			} else {
				util.Respond(w, http.StatusInternalServerError, nil, err)
			}
		} else {
			util.Respond(w, http.StatusNotFound, nil, err)
		}
	})

	router.PUT(`/api/soundctl/backends/:name/outputs/:output/volume/:factor`, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
		if output, err := self.getNamedOutput(params.ByName(`name`), params.ByName(`output`)); err == nil {
			if v, err := stringutil.ConvertToFloat(params.ByName(`factor`)); err == nil {
				if err := output.SetVolume(v); err == nil {
					util.Respond(w, http.StatusNoContent, nil, nil)
				} else {
					util.Respond(w, http.StatusInternalServerError, nil, err)
				}
			} else {
				util.Respond(w, http.StatusBadRequest, nil, err)
			}
		} else {
			util.Respond(w, http.StatusNotFound, nil, err)
		}
	})

	router.PUT(`/api/soundctl/backends/:name/outputs/:output/volume-up/:factor`, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
		if output, err := self.getNamedOutput(params.ByName(`name`), params.ByName(`output`)); err == nil {
			if v, err := stringutil.ConvertToFloat(params.ByName(`factor`)); err == nil {
				if err := output.IncreaseVolume(v); err == nil {
					util.Respond(w, http.StatusNoContent, nil, nil)
				} else {
					util.Respond(w, http.StatusInternalServerError, nil, err)
				}
			} else {
				util.Respond(w, http.StatusBadRequest, nil, err)
			}
		} else {
			util.Respond(w, http.StatusNotFound, nil, err)
		}
	})

	router.PUT(`/api/soundctl/backends/:name/outputs/:output/volume-down/:factor`, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
		if output, err := self.getNamedOutput(params.ByName(`name`), params.ByName(`output`)); err == nil {
			if v, err := stringutil.ConvertToFloat(params.ByName(`factor`)); err == nil {
				if err := output.DecreaseVolume(v); err == nil {
					util.Respond(w, http.StatusNoContent, nil, nil)
				} else {
					util.Respond(w, http.StatusInternalServerError, nil, err)
				}
			} else {
				util.Respond(w, http.StatusBadRequest, nil, err)
			}
		} else {
			util.Respond(w, http.StatusNotFound, nil, err)
		}
	})

	return nil
}
Exemplo n.º 4
0
func (self *SessionModule) LoadRoutes(router *httprouter.Router) error {
	router.GET(`/api/session/workspaces`, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
		if workspaces, err := self.GetAllWorkspaces(); err == nil {
			util.Respond(w, http.StatusOK, workspaces, nil)
		} else {
			util.Respond(w, http.StatusInternalServerError, nil, err)
		}
	})

	router.GET(`/api/session/workspaces/current`, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
		if workspaces, err := self.GetAllWorkspaces(); err == nil {
			for _, workspace := range workspaces {
				if workspace.IsCurrent {
					util.Respond(w, http.StatusOK, workspace, nil)
					return
				}
			}

			util.Respond(w, http.StatusNotFound, nil, fmt.Errorf("Current workspace not found"))
		} else {
			util.Respond(w, http.StatusInternalServerError, nil, err)
		}
	})

	router.GET(`/api/session/windows`, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
		if windows, err := self.GetAllWindows(); err == nil {
			for i, _ := range windows {
				windows[i].IconUri = fmt.Sprintf("/api/session/windows/%d/icon", windows[i].ID)
			}

			util.Respond(w, http.StatusOK, windows, nil)
		} else {
			util.Respond(w, http.StatusInternalServerError, nil, err)
		}
	})

	router.GET(`/api/session/windows/:id`, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
		if window, err := self.GetWindow(params.ByName(`id`)); err == nil {
			window.IconUri = fmt.Sprintf("/api/session/windows/%s/icon", params.ByName(`id`))

			util.Respond(w, http.StatusOK, window, nil)
		} else {
			util.Respond(w, http.StatusInternalServerError, nil, err)
		}
	})

	router.GET(`/api/session/windows/:id/icon`, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
		var buffer bytes.Buffer
		width := uint(16)
		height := uint(16)

		if w := req.URL.Query().Get(`w`); w != `` {
			if value, err := stringutil.ConvertToInteger(w); err == nil {
				width = uint(value)
			}
		}

		if h := req.URL.Query().Get(`h`); h != `` {
			if value, err := stringutil.ConvertToInteger(h); err == nil {
				height = uint(value)
			}
		}

		if height != width {
			height = width
		}

		if err := self.WriteWindowIcon(params.ByName(`id`), width, height, &buffer); err == nil {
			w.Header().Set(`Content-Type`, `image/png`)
			w.Write(buffer.Bytes())
			return
		} else {
			util.Respond(w, http.StatusInternalServerError, nil, err)
		}
	})

	router.GET(`/api/session/windows/:id/image`, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
		var buffer bytes.Buffer

		if err := self.WriteWindowImage(params.ByName(`id`), &buffer); err == nil {
			w.Header().Set(`Content-Type`, `image/png`)
			w.Write(buffer.Bytes())
			return
		} else {
			util.Respond(w, http.StatusInternalServerError, nil, err)
		}
	})

	router.PUT(`/api/session/windows/:id/do/:action`, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
		var err error
		id := params.ByName(`id`)

		switch params.ByName(`action`) {
		case `maximize`:
			err = self.MaximizeWindow(id)

		case `max-x`:
			err = self.MaximizeWindowHorizontal(id)

		case `max-y`:
			err = self.MaximizeWindowVertical(id)

		case `minimize`:
			err = self.MinimizeWindow(id)

		case `restore`:
			err = self.RestoreWindow(id)

		case `hide`:
			err = self.HideWindow(id)

		case `show`:
			err = self.ShowWindow(id)

		case `raise`:
			err = self.RaiseWindow(id)

		default:
			util.Respond(w, http.StatusBadRequest, nil, fmt.Errorf("Unknown action '%s'", params.ByName(`action`)))
			return
		}

		if err == nil {
			util.Respond(w, http.StatusAccepted, nil, nil)
		} else {
			util.Respond(w, http.StatusInternalServerError, nil, err)
		}
	})

	router.PUT(`/api/session/windows/:id/move/:x/:y`, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
		id := params.ByName(`id`)
		var x, y int

		if value, err := stringutil.ConvertToInteger(params.ByName(`x`)); err == nil {
			x = int(value)
		} else {
			util.Respond(w, http.StatusBadRequest, nil, err)
			return
		}

		if value, err := stringutil.ConvertToInteger(params.ByName(`y`)); err == nil {
			y = int(value)
		} else {
			util.Respond(w, http.StatusBadRequest, nil, err)
			return
		}

		if err := self.MoveWindow(id, x, y); err == nil {
			util.Respond(w, http.StatusAccepted, nil, nil)
		} else {
			util.Respond(w, http.StatusInternalServerError, nil, err)
		}
	})

	router.PUT(`/api/session/windows/:id/resize/:x/:y`, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
		id := params.ByName(`id`)
		var width, height uint

		if value, err := stringutil.ConvertToInteger(params.ByName(`width`)); err == nil {
			width = uint(value)
		} else {
			util.Respond(w, http.StatusBadRequest, nil, err)
			return
		}

		if value, err := stringutil.ConvertToInteger(params.ByName(`height`)); err == nil {
			height = uint(value)
		} else {
			util.Respond(w, http.StatusBadRequest, nil, err)
			return
		}

		if err := self.ResizeWindow(id, width, height); err == nil {
			util.Respond(w, http.StatusAccepted, nil, nil)
		} else {
			util.Respond(w, http.StatusInternalServerError, nil, err)
		}
	})

	router.GET(`/api/session/applications`, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
		keys := make([]string, 0)

		for key, _ := range self.Applications.Entries {
			keys = append(keys, key)
		}

		sort.Strings(keys)

		util.Respond(w, http.StatusOK, keys, nil)
	})

	router.GET(`/api/session/applications/:name`, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
		key := params.ByName(`name`)

		if app, ok := self.Applications.Entries[key]; ok {
			util.Respond(w, http.StatusOK, app, nil)
		} else {
			util.Respond(w, http.StatusNotFound, nil, fmt.Errorf("Could not locate application '%s'", key))
		}
	})

	router.GET(`/api/session/icons/list/:type`, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
		var filterMinSize, filterMaxSize int

		rv := make([]string, 0)
		listType := params.ByName(`type`)

		//  filters
		filterThemes := strings.Split(req.URL.Query().Get(`themes`), `,`)
		filterIconContextTypes := strings.Split(req.URL.Query().Get(`contexts`), `,`)
		filterIconFileTypes := strings.Split(req.URL.Query().Get(`filetypes`), `,`)
		filterMinSizeS := req.URL.Query().Get(`minsize`)
		filterMaxSizeS := req.URL.Query().Get(`maxsize`)
		filterIsScalable := req.URL.Query().Get(`scalable`)

		if filterMinSizeS != `` {
			if v, err := stringutil.ConvertToInteger(filterMinSizeS); err == nil {
				filterMinSize = int(v)
			} else {
				util.Respond(w, http.StatusBadRequest, nil, err)
				return
			}
		}

		if filterMaxSizeS != `` {
			if v, err := stringutil.ConvertToInteger(filterMaxSizeS); err == nil {
				filterMaxSize = int(v)
			} else {
				util.Respond(w, http.StatusBadRequest, nil, err)
				return
			}
		}

		for _, theme := range self.Themeset.Themes {
			if len(filterThemes) > 0 && filterThemes[0] != `` {
				if !sliceutil.ContainsString(filterThemes, strings.ToLower(theme.InternalName)) {
					inInherited := false

					for _, inheritedThemeName := range theme.Inherits {
						if sliceutil.ContainsString(filterThemes, strings.ToLower(inheritedThemeName)) {
							inInherited = true
							break
						}
					}

					if !inInherited {
						continue
					}
				}
			}

			switch listType {
			case `themes`:
				if !sliceutil.ContainsString(rv, theme.Name) {
					rv = append(rv, theme.InternalName)
				}
			default:
				for _, icon := range theme.Icons {
					//  filter context types
					if len(filterIconContextTypes) > 0 && filterIconContextTypes[0] != `` {
						if !sliceutil.ContainsString(filterIconContextTypes, strings.ToLower(icon.Context.Name)) {
							continue
						}
					}

					//  filter icon filetypes
					if len(filterIconFileTypes) > 0 && filterIconFileTypes[0] != `` {
						if !sliceutil.ContainsString(filterIconFileTypes, icon.Type) {
							continue
						}
					}

					//  filter icon size contraints
					if filterMinSize > 0 && icon.Context.MinSize < filterMinSize {
						continue
					}

					if filterMaxSize > 0 && icon.Context.MaxSize > filterMaxSize {
						continue
					}

					//  filter for scalable/non-scalable icons
					if filterIsScalable == `true` && icon.Context.Type != icons.IconContextScalable {
						continue
					} else if filterIsScalable == `false` && icon.Context.Type == icons.IconContextScalable {
						continue
					}

					var value string

					switch listType {
					case `names`:
						value = icon.Name
					case `contexts`:
						value = strings.ToLower(icon.Context.Name)
					case `display-names`:
						value = icon.DisplayName
					case `sizes`:
						if v, err := stringutil.ToString(icon.Context.Size); err == nil {
							value = v
						}
					default:
						util.Respond(w, http.StatusBadRequest, nil, fmt.Errorf("Unrecognized list type '%s'", listType))
						return
					}

					if value != `` {
						if !sliceutil.ContainsString(rv, value) {
							rv = append(rv, value)
						}
					}
				}
			}
		}

		sort.Strings(rv)

		util.Respond(w, http.StatusOK, rv, nil)
	})

	router.GET(`/api/session/icons/view/:name/size/:size`, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
		var iconSize int

		iconNames := strings.Split(params.ByName(`name`), `,`)
		iconSizeS := params.ByName(`size`)
		themeName := req.URL.Query().Get(`theme`)

		if themeName == `` {
			themeName = self.Themeset.DefaultTheme
		}

		if v, err := stringutil.ConvertToInteger(iconSizeS); err == nil {
			iconSize = int(v)

			var icon *icons.Icon

			switch req.URL.Query().Get(`mode`) {
			case `hicolor-first`:
				if hiColorIcon, ok := self.Themeset.FindIconViaTheme(`hicolor`, iconNames, iconSize); ok {
					icon = hiColorIcon
				} else if themeIcon, ok := self.Themeset.FindIconViaTheme(themeName, iconNames, iconSize); ok {
					icon = themeIcon
				}
			default:
				if themeIcon, ok := self.Themeset.FindIconViaTheme(themeName, iconNames, iconSize); ok {
					icon = themeIcon
				}
			}

			if icon != nil {
				var contentType string

				switch icon.Type {
				case `png`:
					contentType = `image/png`
				case `svg`:
					contentType = `image/svg+xml`
				default:
					util.Respond(w, http.StatusBadRequest, nil, fmt.Errorf("Unsupported icon type '%s'", icon.Type))
					return
				}

				defer icon.Close()

				if data, err := ioutil.ReadAll(icon); err == nil {
					w.Header().Set(`Content-Type`, contentType)
					w.Write(data)
				} else {
					util.Respond(w, http.StatusBadRequest, nil, err)
				}
			} else {
				util.Respond(w, http.StatusNotFound, nil, fmt.Errorf("Could not locate icon"))
			}
		} else {
			util.Respond(w, http.StatusBadRequest, nil, err)
		}
	})

	// router.GET(`/api/session/applications/find/:pattern`, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
	// })

	router.PUT(`/api/session/applications/:name/launch`, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
		if err := self.Applications.LaunchEntry(params.ByName(`name`)); err == nil {
			util.Respond(w, http.StatusAccepted, nil, nil)
		} else {
			util.Respond(w, http.StatusNotFound, nil, err)
		}

	})

	return nil
}