-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
82 lines (68 loc) · 2.11 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"bytes"
"fmt"
"log"
"math/rand"
"net/http"
"time"
)
// Initialize a global random generator
var r = rand.New(rand.NewSource(time.Now().UnixNano()))
// generateQuestion generates a random math question.
func generateQuestion(operation string) (string, int) {
num1 := r.Intn(50) + 1
num2 := r.Intn(50) + 1
var question string
var answer int
switch operation {
case "+":
question = fmt.Sprintf("%d + %d", num1, num2)
answer = num1 + num2
case "-":
question = fmt.Sprintf("%d - %d", num1, num2)
answer = num1 - num2
case "*":
num1 = r.Intn(10) + 1
num2 = r.Intn(10) + 1
question = fmt.Sprintf("%d * %d", num1, num2)
answer = num1 * num2
case "/":
num2 = r.Intn(10) + 1
answer = num1
num1 = answer * num2
question = fmt.Sprintf("%d / %d", num1, num2)
default:
panic("Invalid operation")
}
return question, answer
}
// handleMathQues handles the math ques HTTP request.
func handleMathQues(w http.ResponseWriter, r *http.Request) {
// Retrieve user name from query parameter "name"
name := r.URL.Query().Get("name")
if name == "" {
http.Error(w, "Missing name parameter", http.StatusBadRequest)
return
}
// Generate a random math question
operation := "+" // You can change the operation here if needed
question, _ := generateQuestion(operation) // Ignore the answer for now
// Prepare the response buffer
var responseBuffer bytes.Buffer
// Write the welcome message to the response buffer
responseBuffer.WriteString(fmt.Sprintf("Welcome, %s!\n", name))
// Print the question to the response buffer
responseBuffer.WriteString(fmt.Sprintf("Question: %s\n", question))
// Write the accumulated response from the buffer to the response writer
w.Write(responseBuffer.Bytes())
}
func main() {
// Serve static files (HTML, CSS, JavaScript) from the "public" directory
http.Handle("/", http.FileServer(http.Dir("public")))
// Define HTTP endpoint for math questions and its handler
http.HandleFunc("/mathques", handleMathQues)
// Start the HTTP server
fmt.Println("Server listening on port 8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}