apiV0MediaHandler, apiV0TreeHandler

master
Bel LaPointe 2022-02-08 10:54:19 -07:00
parent 9622c48395
commit 06deb5d0c9
4 changed files with 47 additions and 3 deletions

View File

@ -7,7 +7,10 @@ require (
local/router v0.0.0-00010101000000-000000000000
)
require gopkg.in/yaml.v2 v2.4.0 // indirect
require (
github.com/google/uuid v1.3.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
replace local/args => ../../../../../../../../args

View File

@ -1,3 +1,5 @@
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=

View File

@ -1,10 +1,16 @@
package main
import (
"encoding/json"
"errors"
"io/ioutil"
"local/router"
"net/http"
"os"
"path"
"strings"
"github.com/google/uuid"
)
type Server struct {
@ -56,11 +62,33 @@ func (server *Server) tryCatchHttpHandler(handler func(http.ResponseWriter, *htt
}
func (server *Server) apiV0TreeHandler(w http.ResponseWriter, r *http.Request) error {
return errors.New("not impl" + r.URL.Path)
tree := server.tree()
branches, err := tree.Get()
if err != nil {
return err
}
return json.NewEncoder(w).Encode(branches)
}
func (server *Server) apiV0MediaHandler(w http.ResponseWriter, r *http.Request) error {
return errors.New("not impl" + r.URL.Path)
if r.Method != http.MethodPost {
http.NotFound(w, r)
return nil
}
b, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
id := uuid.New().String()
os.MkdirAll(path.Join(server.root, "media"), os.ModePerm)
if err := ioutil.WriteFile(path.Join(server.root, "media", id), b, os.ModePerm); err != nil {
return err
}
return json.NewEncoder(w).Encode(map[string]map[string]string{
"data": map[string]string{
"filePath": path.Join("/api/v0/media", id),
},
})
}
func (server *Server) apiV0MediaIDHandler(w http.ResponseWriter, r *http.Request) error {
@ -82,3 +110,7 @@ func (server *Server) apiV0SearchHandler(w http.ResponseWriter, r *http.Request)
func (server *Server) rootHandler(w http.ResponseWriter, r *http.Request) error {
return errors.New("not impl" + r.URL.Path)
}
func (server *Server) tree() *Tree {
return NewTree(path.Join(server.root, "tree.yaml"))
}

View File

@ -1,6 +1,7 @@
package main
import (
"bytes"
"net/http"
"net/http/httptest"
"testing"
@ -55,6 +56,12 @@ func TestServerRoutes(t *testing.T) {
if w.Code == http.StatusNotFound {
t.Fatal(w)
}
if !bytes.HasPrefix(w.Body.Bytes(), []byte("not impl")) {
if w.Code != http.StatusOK {
t.Fatal(w)
}
t.Logf("%s %s => %s", c.method, c.path, w.Body.Bytes())
}
})
}
}