ok resty enough
parent
b737b440b1
commit
706522eeef
|
|
@ -13,6 +13,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"path"
|
"path"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
|
|
@ -160,14 +161,16 @@ func (h Handler) handle(session Session, w http.ResponseWriter, r *http.Request)
|
||||||
http.FileServer(public).ServeHTTP(w, r)
|
http.FileServer(public).ServeHTTP(w, r)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if strings.HasPrefix(r.URL.Path, "/api/v1/questions/") {
|
|
||||||
return h.handleAPIV1Question(session, w, r)
|
handlers := map[string]func(Session, http.ResponseWriter, *http.Request) error{
|
||||||
|
`^/api/v1/questions$`: h.handleAPIV1Questions,
|
||||||
|
`^/api/v1/questions/[^/]*$`: h.handleAPIV1Question,
|
||||||
|
`^/api/v1/questions/[^/]*/answers$`: h.handleAPIV1QuestionsAnswers,
|
||||||
}
|
}
|
||||||
if strings.HasPrefix(r.URL.Path, "/api/v1/questions") {
|
for k, v := range handlers {
|
||||||
return h.handleAPIV1Questions(session, w, r)
|
if regexp.MustCompile(k).MatchString(r.URL.Path) {
|
||||||
|
return v(session, w, r)
|
||||||
}
|
}
|
||||||
if strings.HasPrefix(r.URL.Path, "/api/v1/answers") {
|
|
||||||
return h.handleAPIV1Answers(session, w, r)
|
|
||||||
}
|
}
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
return nil
|
return nil
|
||||||
|
|
@ -190,19 +193,19 @@ func (h Handler) handleAPIV1Questions(session Session, w http.ResponseWriter, r
|
||||||
return json.NewEncoder(w).Encode(qs)
|
return json.NewEncoder(w).Encode(qs)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h Handler) handleAPIV1Answers(session Session, w http.ResponseWriter, r *http.Request) error {
|
func (h Handler) handleAPIV1QuestionsAnswers(session Session, w http.ResponseWriter, r *http.Request) error {
|
||||||
switch r.Method {
|
switch r.Method {
|
||||||
case http.MethodGet:
|
case http.MethodGet:
|
||||||
return h.handleAPIV1AnswersGet(session, w, r)
|
return h.handleAPIV1QuestionsAnswersGet(session, w, r)
|
||||||
case http.MethodPost:
|
case http.MethodPost:
|
||||||
return h.handleAPIV1AnswersPost(session, w, r)
|
return h.handleAPIV1QuestionsAnswersPost(session, w, r)
|
||||||
}
|
}
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h Handler) handleAPIV1AnswersGet(session Session, w http.ResponseWriter, r *http.Request) error {
|
func (h Handler) handleAPIV1QuestionsAnswersGet(session Session, w http.ResponseWriter, r *http.Request) error {
|
||||||
qid := path.Base(r.URL.Path)
|
qid := path.Base(path.Dir(r.URL.Path))
|
||||||
as, err := h.db.GetAnswers(qid)
|
as, err := h.db.GetAnswers(qid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
@ -210,8 +213,8 @@ func (h Handler) handleAPIV1AnswersGet(session Session, w http.ResponseWriter, r
|
||||||
return json.NewEncoder(w).Encode(as)
|
return json.NewEncoder(w).Encode(as)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h Handler) handleAPIV1AnswersPost(session Session, w http.ResponseWriter, r *http.Request) error {
|
func (h Handler) handleAPIV1QuestionsAnswersPost(session Session, w http.ResponseWriter, r *http.Request) error {
|
||||||
qid := path.Base(r.URL.Path)
|
qid := path.Base(path.Dir(r.URL.Path))
|
||||||
uid := session.User.ID
|
uid := session.User.ID
|
||||||
var a Answer
|
var a Answer
|
||||||
if err := json.NewDecoder(r.Body).Decode(&a); err != nil {
|
if err := json.NewDecoder(r.Body).Decode(&a); err != nil {
|
||||||
|
|
|
||||||
5
go.mod
5
go.mod
|
|
@ -2,4 +2,7 @@ module live-studio-audience
|
||||||
|
|
||||||
go 1.21.4
|
go 1.21.4
|
||||||
|
|
||||||
require golang.org/x/time v0.5.0 // indirect
|
require (
|
||||||
|
github.com/julienschmidt/httprouter v1.3.0 // indirect
|
||||||
|
golang.org/x/time v0.5.0 // indirect
|
||||||
|
)
|
||||||
|
|
|
||||||
2
go.sum
2
go.sum
|
|
@ -1,2 +1,4 @@
|
||||||
|
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
|
||||||
|
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
|
||||||
golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk=
|
golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk=
|
||||||
golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
|
golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue