Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

name: PR Check

on:
pull_request:
push:
branches: [main]

jobs:
vet:
defaults:
run:
working-directory: mpd-web-proxy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.22'

- run: go mod download

- name: Go Vet
run: go vet ./...

unit-test:
defaults:
run:
working-directory: mpd-web-proxy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
lfs: true

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.22'

- run: go mod download

- run: go install github.com/jstemmer/go-junit-report/v2@latest

- name: Unit Test
run: |
set +e
go test -v ./... | tee test.out
exitcode=$?
cat test.out | ~/go/bin/go-junit-report -set-exit-code > junit.xml
exit $exitcode

- if: always()
uses: EnricoMi/publish-unit-test-result-action@v2
with:
files: mpd-web-proxy/junit.xml

7 changes: 3 additions & 4 deletions mpd-web-proxy/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ ARG VERSION='development'
COPY go.mod go.sum ./
RUN go mod download
COPY *.go ./
COPY gpio/ ./gpio/
COPY art/ ./art/
COPY version/ ./version/
COPY cmd/ ./cmd/
COPY internal/ ./internal/

RUN go build -ldflags "-X 'github.com/aspage2/remote/mpd-web-proxy/version.Version=${VERSION}'" -o /app/main
RUN go build -ldflags "-X 'github.com/aspage2/remote/mpd-web-proxy/internal/version.Version=${VERSION}'" -o /app/main /app/cmd

ENTRYPOINT ["/app/main"]
2 changes: 1 addition & 1 deletion mpd-web-proxy/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
VERSION = $(shell git describe --exact-match --match='v[0-9].*' HEAD 2>/dev/null || git rev-parse --short HEAD)

build/mpd-web-proxy: $(wildcard **/*.go *.go)
go build -ldflags="-X 'github.com/aspage2/remote/mpd-web-proxy.Version=$(VERSION)'" -o build/mpd-web-proxy .
go build -ldflags="-X 'github.com/aspage2/remote/mpd-web-proxy/internal.Version=$(VERSION)'" -o build/mpd-web-proxy ./cmd
File renamed without changes.
19 changes: 10 additions & 9 deletions mpd-web-proxy/main.go → mpd-web-proxy/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import (
"runtime/debug"
"strings"

"github.com/aspage2/remote/mpd-web-proxy/art"
"github.com/aspage2/remote/mpd-web-proxy/gpio"
"github.com/aspage2/remote/mpd-web-proxy/version"
"github.com/aspage2/remote/mpd-web-proxy/internal"
"github.com/aspage2/remote/mpd-web-proxy/internal/art"
"github.com/aspage2/remote/mpd-web-proxy/internal/gpio"
"github.com/aspage2/remote/mpd-web-proxy/internal/version"
)

const (
Expand All @@ -28,7 +29,7 @@ const (
type Server struct {
Pins []gpio.Pin
PinState *gpio.PinState
MPDStateTopic *Topic[Event]
MPDStateTopic *internal.Topic[internal.Event]
}

func (s *Server) WriteResponse(wr io.Writer) error {
Expand Down Expand Up @@ -119,7 +120,7 @@ func (s *Server) MpdEvents(rw http.ResponseWriter, req *http.Request) {
io.Copy(io.Discard, req.Body)
}()

for ev := range getEvents(s.MPDStateTopic, req.Context()) {
for ev := range internal.GetEvents(s.MPDStateTopic, req.Context()) {
eventPayload := ev.SSEPayload()
_, err := io.WriteString(rw, eventPayload)
if err != nil {
Expand All @@ -141,7 +142,7 @@ func MpdCommand(rw http.ResponseWriter, req *http.Request) {
return
}
q := qs[0]
data := Must(MpdQuery(q))
data := Must(internal.MpdQuery(q, MpdAuthority))
rw.Write(data)
}

Expand Down Expand Up @@ -237,7 +238,7 @@ func httpServer(s *Server) {
nonEventMux.HandleFunc("/go/channels", s.Channels)
nonEventMux.HandleFunc("/go/art/{albumartist}/{album}", AlbumArt)

http.Handle("/go/", loggingMiddleware(&PanicCatchall{&nonEventMux}))
http.Handle("/go/", internal.LoggingMiddleware(&PanicCatchall{&nonEventMux}))
http.Handle("/go/events", &PanicCatchall{http.HandlerFunc(s.MpdEvents)})

http.ListenAndServe(BindAddr, nil)
Expand Down Expand Up @@ -290,7 +291,7 @@ func main() {
var s Server
s.PinState = ps
s.Pins = pins
s.MPDStateTopic = NewTopic[Event]()
go MPDIdler(s.MPDStateTopic)
s.MPDStateTopic = internal.NewTopic[internal.Event]()
go internal.MPDIdler(s.MPDStateTopic, MpdAuthority)
httpServer(&s)
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
"github.com/stretchr/testify/assert"
)

const DataPath = "../../testdata/file.mp3"

func TestFindAPIC(t *testing.T) {
f, err := os.Open("../testdata/file.mp3")
f, err := os.Open(DataPath)
assert.NoError(t, err)

mime, size, err := FindAPICInMP3(f)
Expand All @@ -22,7 +24,7 @@ func TestFindAPIC(t *testing.T) {
}

func BenchmarkFindAPICInMP3(b *testing.B) {
f, err := os.Open("../testdata/file.mp3")
f, err := os.Open(DataPath)
if err != nil {
panic(err)
}
Expand Down
File renamed without changes.
File renamed without changes.
10 changes: 5 additions & 5 deletions mpd-web-proxy/events.go → mpd-web-proxy/internal/events.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package internal

import (
"bufio"
Expand Down Expand Up @@ -62,11 +62,11 @@ func (ev Event) SSEPayload() string {
// event and tries to re-create the connection. After re-
// connecting, the idler sends a `server:mpd-connected`
// event and continues idling.
func MPDIdler(tpc *Topic[Event]) {
func MPDIdler(tpc *Topic[Event], authority string) {
oneRound := func() error {
slog.Info("start idler")
defer slog.Info("stop idler")
mpd, err := net.Dial("tcp", MpdAuthority)
mpd, err := net.Dial("tcp", authority)
if err != nil {
return err
}
Expand Down Expand Up @@ -124,9 +124,9 @@ func mpdIdle(mpd net.Conn, tpc *Topic[Event]) error {
}
}

// getEvents wraps the given Topic[Event] with a ticker
// GetEvents wraps the given Topic[Event] with a ticker
// that sends a `ping` event every 5 seconds.
func getEvents(tpc *Topic[Event], ctx context.Context) chan Event {
func GetEvents(tpc *Topic[Event], ctx context.Context) chan Event {
ret := make(chan Event)
go func() {
defer close(ret)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package internal

import (
"fmt"
Expand Down Expand Up @@ -26,7 +26,7 @@ func (lr *StatusCaptureRW) WriteHeader(code int) {
lr.ResponseWriter.WriteHeader(code)
}

func loggingMiddleware(next http.Handler) http.Handler {
func LoggingMiddleware(next http.Handler) http.Handler {
f := func(rw http.ResponseWriter, req *http.Request) {
sc := NewStatusCaptureRW(rw)
next.ServeHTTP(sc, req)
Expand Down
10 changes: 5 additions & 5 deletions mpd-web-proxy/mpd.go → mpd-web-proxy/internal/mpd.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package internal

import (
"bufio"
Expand All @@ -14,8 +14,8 @@ import (
// MpdQuery connects and queries the MPD server
// with the given command. Returns the response body
// or any connection error.
func MpdQuery(cmd string) ([]byte, error) {
mpd, err := net.Dial("tcp", MpdAuthority)
func MpdQuery(cmd string, authority string) ([]byte, error) {
mpd, err := net.Dial("tcp", authority)
if err != nil {
return nil, err
}
Expand All @@ -24,8 +24,8 @@ func MpdQuery(cmd string) ([]byte, error) {
}

// A cancellable MPD Query.
func MpdQueryContext(cmd string, ctx context.Context) ([]byte, error) {
mpd, err := net.Dial("tcp", MpdAuthority)
func MpdQueryContext(cmd string, authority string, ctx context.Context) ([]byte, error) {
mpd, err := net.Dial("tcp", authority)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion mpd-web-proxy/topic.go → mpd-web-proxy/internal/topic.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package internal

import (
"log/slog"
Expand Down
Loading