Pulled routes out, added first api routes

This commit is contained in:
Sammy Shear 2024-07-21 18:28:14 -04:00
parent a6317ff77e
commit 5b6e22075d
Signed by: sammyshear
GPG Key ID: 69B1EDA35F4F4C09
4 changed files with 120 additions and 8 deletions

26
api/data.go Normal file
View File

@ -0,0 +1,26 @@
package api
type QuestionReq struct {
Token string `json:"token"`
Category uint `json:"category"`
}
type QuestionResp struct {
Results []TextQuestion `json:"results"`
ResponseCode uint `json:"response_code"`
}
type TextQuestion struct {
Difficulty string `json:"difficulty"`
Question string `json:"question"`
CorrectAnswer string `json:"correct_answer"`
Type string `json:"type"`
IncorrectAnswers []string `json:"incorrect_answers"`
Category uint `json:"category"`
}
type SessionResp struct {
ResponseMessage string `json:"response_message"`
Token string `json:"token"`
ResponseCode uint `json:"response_code"`
}

69
api/handlers.go Normal file
View File

@ -0,0 +1,69 @@
package api
import (
"encoding/json"
"fmt"
"io"
"net/http"
)
func OpenSession(w http.ResponseWriter, r *http.Request) {
res, err := http.Get("https://opentdb.com/api_token.php?command=request")
if err != nil {
http.Error(w, fmt.Sprintf("Error requesting session: %s", err), 500)
return
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
http.Error(w, fmt.Sprintf("Error reading session token: %s", err), 500)
return
}
var session SessionResp
json.Unmarshal(body, &session)
w.Header().Add("content-type", "text/plain")
w.Write([]byte(session.Token))
w.WriteHeader(200)
}
func GetQuestion(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
req, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, fmt.Sprintf("Error parsing request body: %s", err), 421)
return
}
var body QuestionReq
json.Unmarshal(req, &body)
res, err := http.Get(fmt.Sprintf("https://opentdb.com/api.php?amount=1&category=%d&type=multiple&token=%s", body.Category, body.Token))
if err != nil {
http.Error(w, fmt.Sprintf("Failed to request questions: %s", err), 500)
return
}
defer res.Body.Close()
b, err := io.ReadAll(res.Body)
if err != nil {
http.Error(w, fmt.Sprintf("Failed to parse question response: %s", err), 500)
return
}
var questionResp QuestionResp
json.Unmarshal(b, &questionResp)
if questionResp.ResponseCode > 0 {
http.Error(w, "Error with question response", 500)
return
}
result, err := json.Marshal(questionResp.Results[len(questionResp.Results)-1])
if err != nil {
http.Error(w, fmt.Sprintf("Error encoding question: %s", err), 500)
}
w.Header().Add("content-type", "application/json")
w.Write(result)
w.WriteHeader(200)
}

23
api/routes.go Normal file
View File

@ -0,0 +1,23 @@
package api
import (
"net/http"
"github.com/a-h/templ"
"github.com/sammyshear/trivia-chase/views/pages"
)
func NewRoutes() *http.ServeMux {
mux := http.NewServeMux()
// pages and static assets
indexPage := pages.Home()
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
mux.Handle("/", templ.Handler(indexPage))
// api routes
mux.HandleFunc("POST /api/question", GetQuestion)
mux.HandleFunc("GET /api/session", OpenSession)
return mux
}

View File

@ -3,16 +3,10 @@ package cmd
import (
"net/http"
"github.com/a-h/templ"
"github.com/sammyshear/trivia-chase/views/pages"
"github.com/sammyshear/trivia-chase/api"
)
func App() {
mux := http.NewServeMux()
indexPage := pages.Home()
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
mux.Handle("/", templ.Handler(indexPage))
mux := api.NewRoutes()
http.ListenAndServe(":3000", mux)
}