From 231cef3b337e969c6150ec2d9ffe1c823d93f6ca Mon Sep 17 00:00:00 2001 From: Senad Uka Date: Wed, 26 Jan 2022 19:49:54 +0100 Subject: [PATCH] Initial commit --- main.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 main.go diff --git a/main.go b/main.go new file mode 100644 index 0000000..2c036eb --- /dev/null +++ b/main.go @@ -0,0 +1,48 @@ +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