Skip to content

Commit 38ceebf

Browse files
committed
Updating uninstrumented example
1 parent d34160a commit 38ceebf

File tree

2 files changed

+72
-19
lines changed

2 files changed

+72
-19
lines changed

examples/dice/uninstrumented/main.go

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ package main
77

88
import (
99
"context"
10+
"encoding/json"
1011
"log"
1112
"net"
1213
"net/http"
1314
"os"
1415
"os/signal"
16+
"strconv"
1517
"time"
1618
)
1719

@@ -27,8 +29,13 @@ func run() (err error) {
2729
defer stop()
2830

2931
// Start HTTP server.
32+
port := os.Getenv("APPLICATION_PORT")
33+
if port == "" {
34+
port = "8080"
35+
}
36+
3037
srv := &http.Server{
31-
Addr: ":8080",
38+
Addr: ":" + port,
3239
BaseContext: func(net.Listener) context.Context { return ctx },
3340
ReadTimeout: time.Second,
3441
WriteTimeout: 10 * time.Second,
@@ -60,8 +67,52 @@ func newHTTPHandler() http.Handler {
6067
mux := http.NewServeMux()
6168

6269
// Register handlers.
63-
mux.HandleFunc("/rolldice/", rolldice)
64-
mux.HandleFunc("/rolldice/{player}", rolldice)
65-
70+
mux.HandleFunc("/rolldice", handleRollDice)
6671
return mux
6772
}
73+
74+
func handleRollDice(w http.ResponseWriter, r *http.Request) {
75+
// Parse query parameters
76+
rollsParam := r.URL.Query().Get("rolls")
77+
player := r.URL.Query().Get("player")
78+
79+
// Default rolls = 1 if not defined
80+
if rollsParam == "" {
81+
rollsParam = "1"
82+
}
83+
84+
// Check if rolls is a number
85+
rolls, err := strconv.Atoi(rollsParam)
86+
if err != nil {
87+
w.WriteHeader(http.StatusBadRequest)
88+
msg := "Parameter rolls must be a positive integer"
89+
_ = json.NewEncoder(w).Encode(map[string]string{
90+
"status": "error",
91+
"message": msg,
92+
})
93+
log.Printf("WARN: %s", msg)
94+
return
95+
}
96+
97+
results, err := rolldice(rolls)
98+
if err != nil {
99+
// Library signals invalid input (<=0)
100+
w.WriteHeader(http.StatusInternalServerError)
101+
log.Printf("ERROR: %v", err)
102+
return
103+
}
104+
105+
if player == "" {
106+
log.Printf("DEBUG: anonymous player rolled %v", results)
107+
} else {
108+
log.Printf("DEBUG: player=%s rolled %v", player, results)
109+
}
110+
log.Printf("INFO: %s %s -> 200 OK", r.Method, r.URL.String())
111+
112+
w.Header().Set("Content-Type", "application/json")
113+
if len(results) == 1 {
114+
json.NewEncoder(w).Encode(results[0])
115+
} else {
116+
json.NewEncoder(w).Encode(results)
117+
}
118+
}

examples/dice/uninstrumented/rolldice.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,28 @@
44
package main
55

66
import (
7-
"io"
8-
"log"
7+
"errors"
98
"math/rand"
10-
"net/http"
11-
"strconv"
129
)
1310

14-
func rolldice(w http.ResponseWriter, r *http.Request) {
15-
roll := 1 + rand.Intn(6) //nolint:gosec // G404: Use of weak random number generator (math/rand instead of crypto/rand) is ignored as this is not security-sensitive.
11+
func rolldice(rolls int) ([]int, error) {
12+
if rolls <= 0 {
13+
return nil, errors.New("rolls must be positive")
14+
}
1615

17-
var msg string
18-
if player := r.PathValue("player"); player != "" {
19-
msg = player + " is rolling the dice"
20-
} else {
21-
msg = "Anonymous player is rolling the dice"
16+
if rolls == 1 {
17+
return []int{rollOnce()}, nil
2218
}
23-
log.Printf("%s, result: %d", msg, roll)
2419

25-
resp := strconv.Itoa(roll) + "\n"
26-
if _, err := io.WriteString(w, resp); err != nil {
27-
log.Printf("Write failed: %v", err)
20+
results := make([]int, rolls)
21+
for i := 0; i < rolls; i++ {
22+
results[i] = rollOnce()
2823
}
24+
return results, nil
25+
}
26+
27+
// rollOnce is the inner function — returns a random number 1–6.
28+
func rollOnce() int {
29+
roll := 1 + rand.Intn(6) //nolint:gosec // G404: Use of weak random number generator (math/rand instead of crypto/rand) is ignored as this is not security-sensitive.
30+
return roll
2931
}

0 commit comments

Comments
 (0)