moar
parent
d01a933b73
commit
f09d8e6cc9
|
|
@ -0,0 +1,13 @@
|
|||
package main
|
||||
|
||||
import "net/http"
|
||||
|
||||
func main() {
|
||||
s := NewServer()
|
||||
if err := s.Routes(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := http.ListenAndServe(":3004", s); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,77 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"local/router"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
router *router.Router
|
||||
}
|
||||
|
||||
func NewServer() *Server {
|
||||
return &Server{
|
||||
router: router.New(),
|
||||
}
|
||||
}
|
||||
|
||||
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{
|
||||
"/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 {
|
||||
return errors.New("not impl" + r.URL.Path)
|
||||
}
|
||||
|
||||
func (server *Server) apiV0MediaHandler(w http.ResponseWriter, r *http.Request) error {
|
||||
return errors.New("not impl" + r.URL.Path)
|
||||
}
|
||||
|
||||
func (server *Server) apiV0MediaIDHandler(w http.ResponseWriter, r *http.Request) error {
|
||||
return errors.New("not impl" + r.URL.Path)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
Loading…
Reference in New Issue