Commit 9ce7eabe authored by Anthony Gargiulo's avatar Anthony Gargiulo

Reorganized the code a little, updated /volume

parent 96882f72
# alsaRemoteControl
Control your alsa mixer based audio system from a simple web(api?)
### Build/deploy
## Build/deploy
```sh
GOOS=linux GOARCH=arm go build .
scp alsaRemoteControl <rasp-pi_ip-or-domain>:
```
### Running
## Running
#### With TLS
```sh
export ALSA_REMOTE_SSL_CERT=cert.pem ALSA_REMOTE_SSL_KEY=key.pem
......@@ -22,7 +22,7 @@ export ALSA_REMOTE_SSL_CERT=cert.pem ALSA_REMOTE_SSL_KEY=key.pem
if no port is specified, it will run on port 12345.
#### API
## API
None of this is a real REST API, just a heads up
|Request|Description|
......
......@@ -9,20 +9,53 @@ import (
"strconv"
)
func volume(w http.ResponseWriter, req *http.Request) {
func volumeStatusResponse(w http.ResponseWriter) {
alsaVolume, err := GetVolume()
if err != nil {
panic(err)
}
isMuted, err := GetMuted()
if err != nil {
panic(err)
}
if _, err := fmt.Fprintf(w, "Volume is %d\nMuted: %t", alsaVolume, isMuted); err != nil {
panic(err)
}
}
func errBadRequestVolume(w http.ResponseWriter, err error) {
w.WriteHeader(http.StatusBadRequest)
message := []byte("400 - [valid] POST /volume/(0-100)\n")
if err != nil {
message = append(message, err.Error()+"\n"...)
}
if _, err := w.Write(message); err != nil {
panic(err)
}
}
func index(w http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodGet {
http.NotFound(w, req)
return
}
alsaVolume, err := GetVolume()
if err != nil {
if req.URL.Path != "/" {
http.NotFound(w, req)
return
}
if _, err := fmt.Fprint(w, "Welcome!\n"); err != nil {
panic(err)
}
}
if _, err := fmt.Fprintf(w, "Volume is %d\n", alsaVolume); err != nil {
panic(err)
func volume(w http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodGet {
http.NotFound(w, req)
return
}
volumeStatusResponse(w)
}
func setVolume(w http.ResponseWriter, req *http.Request) {
......@@ -42,24 +75,7 @@ func setVolume(w http.ResponseWriter, req *http.Request) {
errBadRequestVolume(w, err)
return
}
alsaVolume, err := GetVolume()
if err != nil {
panic(err)
}
if _, nil := fmt.Fprintf(w, "Current volume: %d\n", alsaVolume); err != nil {
panic(err)
}
}
func errBadRequestVolume(w http.ResponseWriter, err error) {
w.WriteHeader(http.StatusBadRequest)
message := []byte("400 - [valid] POST /volume/(0-100)\n")
if err != nil {
message = append(message, err.Error()+"\n"...)
}
if _, err := w.Write(message); err != nil {
panic(err)
}
volumeStatusResponse(w)
}
func toggle(w http.ResponseWriter, req *http.Request) {
......@@ -115,21 +131,6 @@ func volDown(w http.ResponseWriter, req *http.Request) {
}
}
func index(w http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodGet {
http.NotFound(w, req)
return
}
if req.URL.Path != "/" {
http.NotFound(w, req)
return
}
if _, err := fmt.Fprint(w, "Welcome!\n"); err != nil {
panic(err)
}
}
func mute(w http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodGet {
http.NotFound(w, req)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment