Added ql database

This commit is contained in:
Senad Uka
2022-01-31 05:31:44 +01:00
parent 231cef3b33
commit 39a942bc78
5 changed files with 104 additions and 21 deletions

25
main.go
View File

@@ -14,35 +14,18 @@ func homePage(w http.ResponseWriter, r *http.Request) {
}
func handleRequests() {
http.HandleFunc("/", randomQuestion)
http.HandleFunc("/api/answer", answerHandler)
http.HandleFunc("/", randomQuestionHandler)
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ć"},
}
initializeQuestions()
handleRequests()
}
func randomQuestion(w http.ResponseWriter, r *http.Request) {
func answerHandler(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