Skip to content

Commit

Permalink
json marshalling handled on server side
Browse files Browse the repository at this point in the history
  • Loading branch information
indiependente committed Oct 28, 2020
1 parent f910371 commit 0ee2655
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
20 changes: 8 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"

"github.com/c-bata/go-prompt"
"github.com/indiependente/autoEqMac/autoeq"
"github.com/indiependente/autoEqMac/eqmac"
"github.com/indiependente/autoEqMac/eqmac/mapping"
"github.com/indiependente/autoEqMac/server"
"gopkg.in/alecthomas/kingpin.v2"
Expand Down Expand Up @@ -69,19 +66,18 @@ func run() error {
if err != nil {
return fmt.Errorf("could not find fixed band EQ preset: %w", err)
}

jsonPreset, err := json.Marshal([]eqmac.EQPreset{eqPreset})
if err != nil {
return fmt.Errorf("could not marshal preset to JSON: %w", err)
}
out := os.Stdout
if *file != "" {
err := ioutil.WriteFile(*file, jsonPreset, os.ModePerm)
f, err := os.Create(*file)
if err != nil {
return fmt.Errorf("could not write preset to file: %w", err)
return fmt.Errorf("could not create preset file: %w", err)
}
return nil
out = f
}
err = srv.WritePreset(out, eqPreset)
if err != nil {
return fmt.Errorf("could not write preset to file: %w", err)
}
fmt.Printf("%s\n", string(jsonPreset))
return nil
}

Expand Down
3 changes: 3 additions & 0 deletions server/interface.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package server

import (
"io"

"github.com/indiependente/autoEqMac/autoeq"
"github.com/indiependente/autoEqMac/eqmac"
)
Expand All @@ -9,4 +11,5 @@ type Server interface {
ListEQsMetadata() ([]autoeq.EQMetadata, error)
GetFixedBandEQPreset(id string) (eqmac.EQPreset, error)
GetEQMetadataByName(name string) (autoeq.EQMetadata, error)
WritePreset(w io.Writer, p eqmac.EQPreset) error
}
11 changes: 11 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package server

import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"

Expand Down Expand Up @@ -103,3 +105,12 @@ func (s HTTPServer) GetEQMetadataByName(name string) (autoeq.EQMetadata, error)
}
return eqMeta, nil
}

func (s HTTPServer) WritePreset(w io.Writer, p eqmac.EQPreset) error {
jsonPreset, err := json.Marshal([]eqmac.EQPreset{p})
if err != nil {
return fmt.Errorf("could not marshal preset to JSON: %w", err)
}
_, err = fmt.Fprintln(w, string(jsonPreset))
return err
}

0 comments on commit 0ee2655

Please sign in to comment.