142 lines
3.5 KiB
Go
142 lines
3.5 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"io/ioutil"
|
|
"local/router"
|
|
"net/http"
|
|
"os"
|
|
"path"
|
|
"strings"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type Server struct {
|
|
router *router.Router
|
|
root string
|
|
}
|
|
|
|
func NewServer(root string) *Server {
|
|
return &Server{
|
|
router: router.New(),
|
|
root: root,
|
|
}
|
|
}
|
|
|
|
func (server *Server) Routes() error {
|
|
wildcard := func(s string) string {
|
|
return strings.TrimSuffix(s, "/") + "/" + router.Wildcard
|
|
}
|
|
wildcards := func(s string) string {
|
|
return wildcard(wildcard(s))
|
|
}
|
|
_ = wildcards
|
|
for path, handler := range map[string]func(http.ResponseWriter, *http.Request) error{
|
|
"/": server.rootHandler,
|
|
"/api/v0/tree": server.apiV0TreeHandler,
|
|
"/api/v0/media": server.apiV0MediaHandler,
|
|
wildcard("/api/v0/media"): server.apiV0MediaIDHandler,
|
|
"/api/v0/files": server.apiV0FilesHandler,
|
|
wildcard("/api/v0/files/"): server.apiV0FilesIDHandler,
|
|
"/api/v0/search": server.apiV0SearchHandler,
|
|
} {
|
|
if err := server.router.Add(path, server.tryCatchHttpHandler(handler)); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (server *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
server.router.ServeHTTP(w, r)
|
|
}
|
|
|
|
func (server *Server) tryCatchHttpHandler(handler func(http.ResponseWriter, *http.Request) error) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
if err := handler(w, r); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (server *Server) apiV0TreeHandler(w http.ResponseWriter, r *http.Request) error {
|
|
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 {
|
|
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.Dir(server.diskMediaPath(id)), os.ModePerm)
|
|
if err := ioutil.WriteFile(server.diskMediaPath(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 {
|
|
switch r.Method {
|
|
case http.MethodGet:
|
|
return server.apiV0MediaIDGetHandler(w, r)
|
|
}
|
|
http.NotFound(w, r)
|
|
return nil
|
|
}
|
|
|
|
func (server *Server) apiV0MediaIDGetHandler(w http.ResponseWriter, r *http.Request) error {
|
|
id := path.Base(r.URL.Path)
|
|
f, err := os.Open(server.diskMediaPath(id))
|
|
if os.IsNotExist(err) {
|
|
http.NotFound(w, r)
|
|
return nil
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer f.Close()
|
|
io.Copy(w, f)
|
|
return nil
|
|
}
|
|
|
|
func (server *Server) apiV0FilesHandler(w http.ResponseWriter, r *http.Request) error {
|
|
return errors.New("not impl" + r.URL.Path)
|
|
}
|
|
|
|
func (server *Server) apiV0FilesIDHandler(w http.ResponseWriter, r *http.Request) error {
|
|
return errors.New("not impl" + r.URL.Path)
|
|
}
|
|
|
|
func (server *Server) apiV0SearchHandler(w http.ResponseWriter, r *http.Request) error {
|
|
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)
|
|
}
|
|
|
|
func (server *Server) tree() *Tree {
|
|
return NewTree(path.Join(server.root, "tree.yaml"))
|
|
}
|
|
|
|
func (server *Server) diskMediaPath(id string) string {
|
|
return path.Join(server.root, "media", id)
|
|
}
|