Add go server
This commit is contained in:
parent
85c7c4fd9f
commit
6bddf284fe
4 changed files with 52 additions and 8 deletions
3
server/go.mod
Normal file
3
server/go.mod
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
module git.chandlerswift.com/chandlerswift/digital-turntable/server
|
||||
|
||||
go 1.25.5
|
||||
37
server/main.go
Normal file
37
server/main.go
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
//go:embed dist
|
||||
var webUI embed.FS
|
||||
|
||||
func setReader(state string) error {
|
||||
// echo 'on' > '/sys/bus/usb/devices/1-1/power/control' # or 'auto'
|
||||
switch state {
|
||||
case "auto", "on":
|
||||
return os.WriteFile("/sys/bus/usb/devices/1-1/power/control", []byte(state), 0)
|
||||
default:
|
||||
return fmt.Errorf("unknown state %q", state)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
setReader("on")
|
||||
http.Handle("/", http.FileServerFS(webUI))
|
||||
|
||||
http.HandleFunc("POST /api/reader/{state}", func(w http.ResponseWriter, r *http.Request) {
|
||||
if err := setReader(r.PathValue("state")); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Write([]byte("ok"))
|
||||
})
|
||||
|
||||
log.Fatal(http.ListenAndServe("127.0.0.1:8000", nil))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue