49 lines
1.5 KiB
Go
49 lines
1.5 KiB
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("/", randomQuestion)
|
|
log.Fatal(http.ListenAndServe(":10000", nil))
|
|
}
|
|
|
|
func main() {
|
|
Questions = []Question{
|
|
Question{Category: "Muzika", Question: "Koji kompozitor je stariji od Mozarta?", Answer1: "Haydn", Answer2: "Rizo Ruža", Answer3: "Wagner", Answer4: "Vatroslav Lisinski"},
|
|
Question{Category: "Matematika", Question: "Beskonačno minus beskonačno - koliki je rezultat ?", Answer1: "0", Answer2: "Zavisi", Answer3: "2 beskonačna", Answer4: "Nedefinisano u matematici"},
|
|
Question{Category: "Književnost", Question: "Ko je napisao djelo 'Idiot'?", Answer1: "Dostojevski", Answer2: "Decembarski", Answer3: "Lenjin", Answer4: "Branko Čopić"},
|
|
}
|
|
handleRequests()
|
|
}
|
|
|
|
func randomQuestion(w http.ResponseWriter, r *http.Request) {
|
|
randomIndex := rand.Intn(len(Questions))
|
|
question := Questions[randomIndex]
|
|
json.NewEncoder(w).Encode(question)
|
|
}
|
|
|
|
type Question struct {
|
|
Category string `json:"category"`
|
|
Question string `json:"question"`
|
|
Answer1 string `json:"answer1"`
|
|
Answer2 string `json:"answer2"`
|
|
Answer3 string `json:"answer3"`
|
|
Answer4 string `json:"answer4"`
|
|
}
|
|
|
|
// let's declare a global Questions array
|
|
// that we can then populate in our main function
|
|
// to simulate a database
|
|
var Questions []Question
|