diff options
-rw-r--r-- | TODO | 1 | ||||
-rw-r--r-- | db.go | 18 | ||||
-rw-r--r-- | files.go | 4 | ||||
-rw-r--r-- | handlers.go | 21 |
4 files changed, 32 insertions, 12 deletions
@@ -1,4 +1,3 @@ * Remove things that should be configy to a config file (FileRoot, db params, server port/addr, ...) * Remove mp3 assumption -* Replace the NamedExec in postCache with a Select, using INSERT ... RETURNING *, then return the APICache to the user * Betterness the Path <-> URI functions @@ -39,17 +39,25 @@ func getCache(id uint64) (DBCache, error) { return cache, err } -func postCache(dbcache DBCache) error { +func postCache(incache DBCache) (DBCache, error) { + var outcache DBCache + db, err := sqlx.Connect("postgres", "user=audiocache password=audiocache dbname=audiocache sslmode=disable") if err != nil { - return err + return outcache, err + } + + rows, err := db.NamedQuery("INSERT INTO caches (latitude, longitude, created, path) VALUES (:latitude, :longitude, :created, :path) RETURNING *", incache) + if err != nil { + return outcache, err } - _, err = db.NamedExec("INSERT INTO caches (latitude, longitude, created, path) VALUES (:latitude, :longitude, :created, :path)", dbcache) + rows.Next() + err = rows.StructScan(&outcache) if err != nil { - return err + return outcache, err } err = db.Close() - return err + return outcache, err } @@ -9,8 +9,8 @@ func writeFile(postcache PostCache, filename string) error { data := postcache.Data var binary []byte - n, err := base64.StdEncoding.Decode(binary, []byte(data)) - if n == 0 || err != nil { + binary, err := base64.StdEncoding.DecodeString(data) + if err != nil { return err } diff --git a/handlers.go b/handlers.go index c0ec657..77fdeed 100644 --- a/handlers.go +++ b/handlers.go @@ -34,6 +34,11 @@ func CacheIndex(w http.ResponseWriter, r *http.Request) { } } +func MarshalCache(apicache APICache) (string, error) { + str, err := json.Marshal(apicache) + return string(str), err +} + func CacheShow(w http.ResponseWriter, r *http.Request) { cacheId, err := strconv.ParseUint(mux.Vars(r)["cacheId"], 10, 64) if err != nil { @@ -48,9 +53,12 @@ func CacheShow(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.WriteHeader(http.StatusOK) - if err := json.NewEncoder(w).Encode(DBToAPI(cache)); err != nil { + str, err := MarshalCache(DBToAPI(cache)) + if err != nil { panic(err) } + + fmt.Fprintln(w, str) } func CacheCreate(w http.ResponseWriter, r *http.Request) { @@ -72,9 +80,9 @@ func CacheCreate(w http.ResponseWriter, r *http.Request) { } filename := uuid.New() + ".mp3" - dbcache := PostToDB(postcache, filename) + incache := PostToDB(postcache, filename) - err = postCache(dbcache) + outcache, err := postCache(incache) if err != nil { panic(err) } @@ -87,5 +95,10 @@ func CacheCreate(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.WriteHeader(http.StatusCreated) - fmt.Fprintln(w, "OK") + str, err := MarshalCache(DBToAPI(outcache)) + if err != nil { + panic(err) + } + + fmt.Fprintln(w, str) } |