whee
parent
f09d8e6cc9
commit
8f8dd81404
|
|
@ -1 +1,2 @@
|
||||||
**/*.sw*
|
**/*.sw*
|
||||||
|
spike/review/reinvent/ezmded/server/ezmded
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,14 @@
|
||||||
|
module ezmded
|
||||||
|
|
||||||
|
go 1.17
|
||||||
|
|
||||||
|
require (
|
||||||
|
local/args v0.0.0-00010101000000-000000000000
|
||||||
|
local/router v0.0.0-00010101000000-000000000000
|
||||||
|
)
|
||||||
|
|
||||||
|
require gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||||
|
|
||||||
|
replace local/args => ../../../../../../../../args
|
||||||
|
|
||||||
|
replace local/router => ../../../../../../../../router
|
||||||
|
|
@ -0,0 +1,4 @@
|
||||||
|
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=
|
||||||
|
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||||
|
|
@ -1,13 +1,20 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "net/http"
|
import (
|
||||||
|
"local/args"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s := NewServer()
|
as := args.NewArgSet()
|
||||||
|
as.Append(args.INT, "p", "port to listen on", 3004)
|
||||||
|
as.Append(args.STRING, "d", "root dir with /index.html and /media and /files", "./public")
|
||||||
|
s := NewServer(as.GetString("d"))
|
||||||
if err := s.Routes(); err != nil {
|
if err := s.Routes(); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
if err := http.ListenAndServe(":3004", s); err != nil {
|
if err := http.ListenAndServe(":"+strconv.Itoa(as.GetInt("p")), s); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,11 +9,13 @@ import (
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
router *router.Router
|
router *router.Router
|
||||||
|
root string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServer() *Server {
|
func NewServer(root string) *Server {
|
||||||
return &Server{
|
return &Server{
|
||||||
router: router.New(),
|
router: router.New(),
|
||||||
|
root: root,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -26,6 +28,7 @@ func (server *Server) Routes() error {
|
||||||
}
|
}
|
||||||
_ = wildcards
|
_ = wildcards
|
||||||
for path, handler := range map[string]func(http.ResponseWriter, *http.Request) error{
|
for path, handler := range map[string]func(http.ResponseWriter, *http.Request) error{
|
||||||
|
"/": server.rootHandler,
|
||||||
"/api/v0/tree": server.apiV0TreeHandler,
|
"/api/v0/tree": server.apiV0TreeHandler,
|
||||||
"/api/v0/media": server.apiV0MediaHandler,
|
"/api/v0/media": server.apiV0MediaHandler,
|
||||||
wildcard("/api/v0/media"): server.apiV0MediaIDHandler,
|
wildcard("/api/v0/media"): server.apiV0MediaIDHandler,
|
||||||
|
|
@ -75,3 +78,7 @@ func (server *Server) apiV0FilesIDHandler(w http.ResponseWriter, r *http.Request
|
||||||
func (server *Server) apiV0SearchHandler(w http.ResponseWriter, r *http.Request) error {
|
func (server *Server) apiV0SearchHandler(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) rootHandler(w http.ResponseWriter, r *http.Request) error {
|
||||||
|
return errors.New("not impl" + r.URL.Path)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,60 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestServerRoutes(t *testing.T) {
|
||||||
|
server := NewServer(t.TempDir())
|
||||||
|
if err := server.Routes(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cases := map[string]struct {
|
||||||
|
path string
|
||||||
|
method string
|
||||||
|
}{
|
||||||
|
"v0: /: get": {
|
||||||
|
path: "/",
|
||||||
|
method: http.MethodGet,
|
||||||
|
},
|
||||||
|
"v0: search: get": {
|
||||||
|
path: "/api/v0/search",
|
||||||
|
method: http.MethodGet,
|
||||||
|
},
|
||||||
|
"v0: tree: get": {
|
||||||
|
path: "/api/v0/tree",
|
||||||
|
method: http.MethodGet,
|
||||||
|
},
|
||||||
|
"v0: media: post": {
|
||||||
|
path: "/api/v0/media",
|
||||||
|
method: http.MethodPost,
|
||||||
|
},
|
||||||
|
"v0: media id: get": {
|
||||||
|
path: "/api/v0/media/id",
|
||||||
|
method: http.MethodGet,
|
||||||
|
},
|
||||||
|
"v0: files: post": {
|
||||||
|
path: "/api/v0/files",
|
||||||
|
method: http.MethodPost,
|
||||||
|
},
|
||||||
|
"v0: files id: get": {
|
||||||
|
path: "/api/v0/files/id",
|
||||||
|
method: http.MethodGet,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for name, d := range cases {
|
||||||
|
c := d
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
r := httptest.NewRequest(c.method, c.path, nil)
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
server.ServeHTTP(w, r)
|
||||||
|
if w.Code == http.StatusNotFound {
|
||||||
|
t.Fatal(w)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue