2022-01-26 19:49:54 +01:00
|
|
|
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() {
|
2022-01-31 05:31:44 +01:00
|
|
|
http.HandleFunc("/api/answer", answerHandler)
|
|
|
|
|
http.HandleFunc("/", randomQuestionHandler)
|
2022-01-26 19:49:54 +01:00
|
|
|
log.Fatal(http.ListenAndServe(":10000", nil))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func main() {
|
2022-01-31 05:31:44 +01:00
|
|
|
initializeQuestions()
|
2022-01-26 19:49:54 +01:00
|
|
|
handleRequests()
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-31 05:31:44 +01:00
|
|
|
func answerHandler(w http.ResponseWriter, r *http.Request) {
|
2022-01-26 19:49:54 +01:00
|
|
|
randomIndex := rand.Intn(len(Questions))
|
|
|
|
|
question := Questions[randomIndex]
|
|
|
|
|
json.NewEncoder(w).Encode(question)
|
|
|
|
|
}
|