From 706522eeef2ed90ba829d37af3c11c774f49b6af Mon Sep 17 00:00:00 2001 From: Bel LaPointe <153096461+breel-render@users.noreply.github.com> Date: Tue, 20 Feb 2024 08:45:29 -0700 Subject: [PATCH] ok resty enough --- cmd/server/main.go | 31 +++++++++++++++++-------------- go.mod | 5 ++++- go.sum | 2 ++ 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/cmd/server/main.go b/cmd/server/main.go index c09cac2..97aca75 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -13,6 +13,7 @@ import ( "os" "os/signal" "path" + "regexp" "strings" "syscall" @@ -160,14 +161,16 @@ func (h Handler) handle(session Session, w http.ResponseWriter, r *http.Request) http.FileServer(public).ServeHTTP(w, r) 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") { - return h.handleAPIV1Questions(session, w, r) - } - if strings.HasPrefix(r.URL.Path, "/api/v1/answers") { - return h.handleAPIV1Answers(session, w, r) + for k, v := range handlers { + if regexp.MustCompile(k).MatchString(r.URL.Path) { + return v(session, w, r) + } } http.NotFound(w, r) return nil @@ -190,19 +193,19 @@ func (h Handler) handleAPIV1Questions(session Session, w http.ResponseWriter, r 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 { case http.MethodGet: - return h.handleAPIV1AnswersGet(session, w, r) + return h.handleAPIV1QuestionsAnswersGet(session, w, r) case http.MethodPost: - return h.handleAPIV1AnswersPost(session, w, r) + return h.handleAPIV1QuestionsAnswersPost(session, w, r) } http.NotFound(w, r) return nil } -func (h Handler) handleAPIV1AnswersGet(session Session, w http.ResponseWriter, r *http.Request) error { - qid := path.Base(r.URL.Path) +func (h Handler) handleAPIV1QuestionsAnswersGet(session Session, w http.ResponseWriter, r *http.Request) error { + qid := path.Base(path.Dir(r.URL.Path)) as, err := h.db.GetAnswers(qid) if err != nil { return err @@ -210,8 +213,8 @@ func (h Handler) handleAPIV1AnswersGet(session Session, w http.ResponseWriter, r return json.NewEncoder(w).Encode(as) } -func (h Handler) handleAPIV1AnswersPost(session Session, w http.ResponseWriter, r *http.Request) error { - qid := path.Base(r.URL.Path) +func (h Handler) handleAPIV1QuestionsAnswersPost(session Session, w http.ResponseWriter, r *http.Request) error { + qid := path.Base(path.Dir(r.URL.Path)) uid := session.User.ID var a Answer if err := json.NewDecoder(r.Body).Decode(&a); err != nil { diff --git a/go.mod b/go.mod index 9221629..a55efc4 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module live-studio-audience 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 +) diff --git a/go.sum b/go.sum index a2652c5..5ea6ff3 100644 --- a/go.sum +++ b/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/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=