Skip to content

Commit

Permalink
feat: preset routes
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsNotGoodName committed Apr 25, 2022
1 parent a38f291 commit ba9ebd2
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 7 deletions.
6 changes: 3 additions & 3 deletions core/preset/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ var (

type (
Preset struct {
TitleNew string
URL string
URLNew string
TitleNew string `json:"title_new"`
URL string `json:"url"`
URLNew string `json:"url_new"`
}

PresetStore interface {
Expand Down
98 changes: 98 additions & 0 deletions left/api/preset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package api

import (
"encoding/json"
"net/http"

"github.com/ItsNotGoodName/reciva-web-remote/core/preset"
"github.com/ItsNotGoodName/reciva-web-remote/left/presenter"
)

func handlePresetError(err error) presenter.Response {
code := http.StatusInternalServerError
if err == preset.ErrPresetNotFound {
code = http.StatusNotFound
}

return presenter.Response{
Code: code,
Error: err,
}
}

func GetPreset(presetStore preset.PresetStore) presenter.Requester {
return func(r *http.Request) presenter.Response {
url := r.URL.Query().Get("url")

p, err := presetStore.Get(r.Context(), url)
if err != nil {
return handlePresetError(err)
}

return presenter.Response{
Code: http.StatusOK,
Data: p,
}
}
}

func GetPresets(presetStore preset.PresetStore) presenter.Requester {
return func(r *http.Request) presenter.Response {
ps, err := presetStore.List(r.Context())
if err != nil {
handlePresetError(err)
}

return presenter.Response{
Code: http.StatusOK,
Data: ps,
}
}
}

func PostPreset(presetStore preset.PresetStore) presenter.Requester {
type request struct {
URL string `json:"url"`
TitleNew string `json:"title_new"`
URLNew string `json:"url_new"`
}

return func(r *http.Request) presenter.Response {
req := request{}
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
return presenter.Response{
Code: http.StatusBadRequest,
Error: err,
}
}

p, err := preset.ParsePreset(req.URL, req.TitleNew, req.URLNew)
if err != nil {
return presenter.Response{
Code: http.StatusBadRequest,
Error: err,
}
}

if err := presetStore.Update(r.Context(), p); err != nil {
return handlePresetError(err)
}

return presenter.Response{
Code: http.StatusOK,
}
}
}

func GetPresetURL(presetStore preset.PresetStore, url string) http.HandlerFunc {
return func(rw http.ResponseWriter, r *http.Request) {
preset, err := presetStore.Get(r.Context(), url)
if err != nil {
http.Error(rw, err.Error(), handlePresetError(err).Code)
return
}

rw.Write([]byte(preset.URLNew))
}
}
10 changes: 9 additions & 1 deletion left/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/ItsNotGoodName/reciva-web-remote/core/app"
"github.com/ItsNotGoodName/reciva-web-remote/core/preset"
"github.com/ItsNotGoodName/reciva-web-remote/core/radio"
"github.com/ItsNotGoodName/reciva-web-remote/left/api"
"github.com/ItsNotGoodName/reciva-web-remote/left/presenter"
Expand All @@ -25,7 +26,7 @@ type Router struct {
r chi.Router
}

func New(port string, p presenter.Presenter, fs fs.FS, hub radio.HubService, radioService radio.RadioService, application *app.App) *Router {
func New(port string, p presenter.Presenter, fs fs.FS, hub radio.HubService, radioService radio.RadioService, application *app.App, presetStore preset.PresetStore) *Router {
r := newMux()
upgrader := newUpgrader()

Expand All @@ -52,10 +53,17 @@ func New(port string, p presenter.Presenter, fs fs.FS, hub radio.HubService, rad
r.Post("/", p(api.RequireRadio(hub, api.PostRadio(radioService))))
r.Post("/volume", p(api.RequireRadio(hub, api.PostRadioVolume(radioService))))
})

r.Get("/presets", p(api.GetPresets(presetStore)))

r.Get("/preset", p(api.GetPreset(presetStore)))
r.Post("/preset", p(api.PostPreset(presetStore)))
})

mountFS(r, fs)

mountPresets(r, presetStore)

return &Router{
port: port,
r: r,
Expand Down
33 changes: 33 additions & 0 deletions left/router/util.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,49 @@
package router

import (
"context"
"fmt"
"io/fs"
"log"
"net"
"net/http"
"net/url"
"strings"

"github.com/ItsNotGoodName/reciva-web-remote/core/preset"
"github.com/ItsNotGoodName/reciva-web-remote/left/api"
"github.com/go-chi/chi/v5"
)

// mountPresets mounts all presets from the given preset store.
func mountPresets(r chi.Router, presetStore preset.PresetStore) {
presets, err := presetStore.List(context.Background())
if err != nil {
log.Fatalln("router.mountPresets:", err)
}

for _, p := range presets {
u, _ := url.Parse(p.URL)
route, url := u.Path, p.URL

if err := validRoute(u.Path); err != nil {
log.Fatalf("router.mountPresets: URL=%s route=%s: %s", url, route, err)
}

r.Get(route, api.GetPresetURL(presetStore, url))
log.Println("router.mountPresets: mounting url", url, "to", route)
}
}

// validRoute returns nil if the given chi route is valid.
func validRoute(route string) error {
if !strings.HasPrefix(route, "/") {
return fmt.Errorf("route must start with /")
}

return nil
}

// printAddresses prints all listening addresses.
func printAddresses(port string) {
addr, err := net.InterfaceAddrs()
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func main() {
application := app.New(hubService, radioService, statePub)

// Left
router := router.New(cfg.PortStr, presenter.New(json.Render), web.FS(), hubService, radioService, application)
router := router.New(cfg.PortStr, presenter.New(json.Render), web.FS(), hubService, radioService, application, presetStore)
backgrounds = append(backgrounds, router)

// Run backgrounds
Expand Down
8 changes: 6 additions & 2 deletions right/file/preset.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package file

import (
"context"
"log"
"errors"
"io/fs"
"sync"

"github.com/ItsNotGoodName/reciva-web-remote/core/preset"
Expand All @@ -18,7 +19,10 @@ type PresetStore struct {
func NewPresetStore(file string) (*PresetStore, error) {
presetsMap, err := readConfig(file)
if err != nil {
log.Println("file.NewPresetStore:", err)
if !errors.Is(err, fs.ErrNotExist) {
return nil, err
}

presetsMap = make(map[string]preset.Preset)
}

Expand Down

0 comments on commit ba9ebd2

Please sign in to comment.