53 lines
1.1 KiB
Go
53 lines
1.1 KiB
Go
package server
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"local/router"
|
|
"log"
|
|
"net/http"
|
|
"regexp"
|
|
)
|
|
|
|
func (s *Server) Routes() error {
|
|
handles := []struct {
|
|
path string
|
|
handler http.HandlerFunc
|
|
}{
|
|
{path: fmt.Sprintf("/api/tag/%s", router.Wildcard), handler: s.tag},
|
|
}
|
|
for _, handle := range handles {
|
|
if err := s.router.Add(handle.path, handle.handler); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *Server) notFound(w http.ResponseWriter, r *http.Request) {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
|
|
func (s *Server) userError(w http.ResponseWriter, r *http.Request, err error) {
|
|
status := http.StatusBadRequest
|
|
log.Printf("%d: %v", status, err)
|
|
w.WriteHeader(status)
|
|
}
|
|
|
|
func (s *Server) error(w http.ResponseWriter, r *http.Request, err error) {
|
|
status := http.StatusInternalServerError
|
|
log.Printf("%d: %v", status, err)
|
|
w.WriteHeader(status)
|
|
}
|
|
|
|
func (s *Server) tag(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != "GET" {
|
|
s.notFound(w, r)
|
|
return
|
|
}
|
|
tag := regexp.MustCompile("^.*\\/").ReplaceAllString(r.URL.Path, "")
|
|
log.Println(tag)
|
|
s.error(w, r, errors.New("not impl"))
|
|
}
|