-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverExample.go
48 lines (43 loc) · 926 Bytes
/
serverExample.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
package main
import (
"fmt"
"html"
"net/http"
"strconv"
"strings"
"time"
)
var Numerals = map[int]string{
10: "X",
9: "IX",
8: "VIII",
7: "VII",
6: "VI",
5: "V",
4: "IV",
3: "III",
2: "II",
1: "I",
}
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
urlPathElements := strings.Split(r.URL.Path, "/")
if urlPathElements[1] == "roman_number" {
number, _ := strconv.Atoi(strings.TrimSpace(urlPathElements[2]))
if number == 0 || number > 10 {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("404 - Not Found"))
} else {
//fmt.Fprintf(w, "%q", html.EscapeString(romanNumerals.Numerals[number]))
fmt.Fprintf(w, "%q", html.EscapeString(Numerals[number]))
}
}
})
s := &http.Server{
Addr: ":8000",
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
s.ListenAndServe()
}