diff --git a/agresija-backend.exe b/agresija-backend.exe new file mode 100644 index 0000000..d3bf723 Binary files /dev/null and b/agresija-backend.exe differ diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e17a644 --- /dev/null +++ b/go.mod @@ -0,0 +1,21 @@ +module gitlab.com/kbr4/agresija-backend + +go 1.17 + +require ( + github.com/edsrzf/mmap-go v1.1.0 // indirect + github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect + golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect + modernc.org/b v1.0.2 // indirect + modernc.org/db v1.0.3 // indirect + modernc.org/file v1.0.3 // indirect + modernc.org/fileutil v1.0.0 // indirect + modernc.org/golex v1.0.1 // indirect + modernc.org/internal v1.0.3 // indirect + modernc.org/lldb v1.0.2 // indirect + modernc.org/mathutil v1.4.1 // indirect + modernc.org/ql v1.4.1 // indirect + modernc.org/sortutil v1.1.0 // indirect + modernc.org/strutil v1.1.1 // indirect + modernc.org/zappy v1.0.3 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..2806f4c --- /dev/null +++ b/go.sum @@ -0,0 +1,42 @@ +github.com/edsrzf/mmap-go v1.0.0/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M= +github.com/edsrzf/mmap-go v1.1.0 h1:6EUwBLQ/Mcr1EYLE4Tn1VdW1A4ckqCQWZBw8Hr0kjpQ= +github.com/edsrzf/mmap-go v1.1.0/go.mod h1:19H/e8pUPLicwkyNgOykDXkJ9F0MHE+Z52B8EIth78Q= +github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +github.com/remyoudompheng/bigfft v0.0.0-20190728182440-6a916e37a237/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= +github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 h1:OdAsTTz6OkFY5QxjkYwrChwuRruF69c169dPK26NUlk= +github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= +golang.org/x/exp v0.0.0-20181106170214-d68db9428509/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM= +golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +modernc.org/b v1.0.2 h1:iPC2u39ebzq12GOC2yXT4mve0HrWcH85cz+midWjzeo= +modernc.org/b v1.0.2/go.mod h1:fVGfCIzkZw5RsuF2A2WHbJmY7FiMIq30nP4s52uWsoY= +modernc.org/db v1.0.3 h1:apxOlWU69je04bY22OT6J0RL23mzvUy22EgTAVyw+Yg= +modernc.org/db v1.0.3/go.mod h1:L4ltUg8tu2pkSJk+fKaRrXs/3EdW79ZKYQ5PfVDT53U= +modernc.org/file v1.0.3 h1:McYGAMMuqjRp6ptmpcLr3r5yw3gNPsonFCAJ0tNK74U= +modernc.org/file v1.0.3/go.mod h1:CNj/pwOfCtCbqiHcXDUlHBB2vWrzdaDCWdcnjtS1+XY= +modernc.org/fileutil v1.0.0 h1:Z1AFLZwl6BO8A5NldQg/xTSjGLetp+1Ubvl4alfGx8w= +modernc.org/fileutil v1.0.0/go.mod h1:JHsWpkrk/CnVV1H/eGlFf85BEpfkrp56ro8nojIq9Q8= +modernc.org/golex v1.0.1 h1:EYKY1a3wStt0RzHaH8mdSRNg78Ub0OHxYfCRWw35YtM= +modernc.org/golex v1.0.1/go.mod h1:QCA53QtsT1NdGkaZZkF5ezFwk4IXh4BGNafAARTC254= +modernc.org/internal v1.0.0/go.mod h1:VUD/+JAkhCpvkUitlEOnhpVxCgsBI90oTzSCRcqQVSM= +modernc.org/internal v1.0.2/go.mod h1:bycJAcev709ZU/47nil584PeBD+kbu8nv61ozeMso9E= +modernc.org/internal v1.0.3 h1:ivuy70etcIw7JY5y5eIjEcElOBpwWtAVs9baukLyKxg= +modernc.org/internal v1.0.3/go.mod h1:dvHFQEGEd33HZar0OdSYIm6yen/77eukCqffWSAwQUc= +modernc.org/lex v1.0.0/go.mod h1:G6rxMTy3cH2iA0iXL/HRRv4Znu8MK4higxph/lE7ypk= +modernc.org/lexer v1.0.0/go.mod h1:F/Dld0YKYdZCLQ7bD0USbWL4YKCyTDRDHiDTOs0q0vk= +modernc.org/lldb v1.0.2 h1:LBw58xVFl01OuM5U9++tLy3wmu+PoWok6T3dHuNjcZk= +modernc.org/lldb v1.0.2/go.mod h1:ovbKqyzA9H/iPwHkAOH0qJbIQVT9rlijecenxDwVUi0= +modernc.org/mathutil v1.0.0/go.mod h1:wU0vUrJsVWBZ4P6e7xtFJEhFSNsfRLJ8H458uRjg03k= +modernc.org/mathutil v1.1.1/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E= +modernc.org/mathutil v1.2.2/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E= +modernc.org/mathutil v1.4.1 h1:ij3fYGe8zBF4Vu+g0oT7mB06r8sqGWKuJu1yXeR4by8= +modernc.org/mathutil v1.4.1/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E= +modernc.org/ql v1.4.1 h1:p0Bx3+AZ29YvJVNWkpKWa0dYaV+x8a2XABZDkw8GfqE= +modernc.org/ql v1.4.1/go.mod h1:c3RNgNK67+UYCyZXWf0tEYmBtWtGrg9M0IbAB5MAWS4= +modernc.org/sortutil v1.1.0 h1:oP3U4uM+NT/qBQcbg/K2iqAX0Nx7B1b6YZtq3Gk/PjM= +modernc.org/sortutil v1.1.0/go.mod h1:ZyL98OQHJgH9IEfN71VsamvJgrtRX9Dj2gX+vH86L1k= +modernc.org/strutil v1.1.1 h1:xv+J1BXY3Opl2ALrBwyfEikFAj8pmqcpnfmuwUwcozs= +modernc.org/strutil v1.1.1/go.mod h1:DE+MQQ/hjKBZS2zNInV5hhcipt5rLPWkmpbGeW5mmdw= +modernc.org/zappy v1.0.3 h1:Tr+P3kclDSrvC6zYBW2hWmOmu5SjG6PtvCt3RCjRmss= +modernc.org/zappy v1.0.3/go.mod h1:w/Akq8ipfols/xZJdR5IYiQNOqC80qz2mVvsEwEbkiI= diff --git a/main.go b/main.go index 2c036eb..2664c7a 100644 --- a/main.go +++ b/main.go @@ -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 diff --git a/question.go b/question.go new file mode 100644 index 0000000..79f22dc --- /dev/null +++ b/question.go @@ -0,0 +1,37 @@ +package main + +import ( + "encoding/json" + "math/rand" + "net/http" +) + +type Question struct { + ID int `json:"id"` + Category string `json:"category"` + Question string `json:"question"` + Answer1 string `json:"answer1"` + Answer2 string `json:"answer2"` + Answer3 string `json:"answer3"` + Answer4 string `json:"answer4"` + CorrectAnswer int `json:"-"` +} + +// let's declare a global Questions array +// that we can then populate in our main function +// to simulate a database +var Questions []Question + +func initializeQuestions() { + Questions = []Question{ + Question{ID: 0, Category: "Muzika", Question: "Koji kompozitor je stariji od Mozarta?", Answer1: "Haydn", Answer2: "Rizo Ruža", Answer3: "Wagner", Answer4: "Vatroslav Lisinski", CorrectAnswer: 1}, + Question{ID: 1, Category: "Matematika", Question: "Beskonačno minus beskonačno - koliki je rezultat ?", Answer1: "0", Answer2: "Zavisi", Answer3: "2 beskonačna", Answer4: "Nedefinisano u matematici"}, + Question{ID: 2, Category: "Književnost", Question: "Ko je napisao djelo 'Idiot'?", Answer1: "Dostojevski", Answer2: "Decembarski", Answer3: "Lenjin", Answer4: "Branko Čopić"}, + } +} + +func randomQuestionHandler(w http.ResponseWriter, r *http.Request) { + randomIndex := rand.Intn(len(Questions)) + question := Questions[randomIndex] + json.NewEncoder(w).Encode(question) +}