apiV0MediaHandler, apiV0TreeHandler
parent
9622c48395
commit
06deb5d0c9
|
|
@ -7,7 +7,10 @@ require (
|
||||||
local/router v0.0.0-00010101000000-000000000000
|
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
|
replace local/args => ../../../../../../../../args
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,16 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"io/ioutil"
|
||||||
"local/router"
|
"local/router"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Server struct {
|
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 {
|
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 {
|
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 {
|
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 {
|
func (server *Server) rootHandler(w http.ResponseWriter, r *http.Request) error {
|
||||||
return errors.New("not impl" + r.URL.Path)
|
return errors.New("not impl" + r.URL.Path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (server *Server) tree() *Tree {
|
||||||
|
return NewTree(path.Join(server.root, "tree.yaml"))
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
@ -55,6 +56,12 @@ func TestServerRoutes(t *testing.T) {
|
||||||
if w.Code == http.StatusNotFound {
|
if w.Code == http.StatusNotFound {
|
||||||
t.Fatal(w)
|
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())
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue