Files
old-agresija-backend/main.go
2022-01-31 05:31:44 +01:00

32 lines
626 B
Go

package main
import (
"encoding/json"
"fmt"
"log"
"math/rand"
"net/http"
)
func homePage(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Welcome to the Agresija!")
fmt.Println("Endpoint Hit: homePage")
}
func handleRequests() {
http.HandleFunc("/api/answer", answerHandler)
http.HandleFunc("/", randomQuestionHandler)
log.Fatal(http.ListenAndServe(":10000", nil))
}
func main() {
initializeQuestions()
handleRequests()
}
func answerHandler(w http.ResponseWriter, r *http.Request) {
randomIndex := rand.Intn(len(Questions))
question := Questions[randomIndex]
json.NewEncoder(w).Encode(question)
}