-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
42 lines (40 loc) · 1.09 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
//main.go
package main
import (
"fmt"
"html"
"net/http"
"strconv"
"strings"
"time"
"github.com/hftamayo/gonumconverter/data"
)
func main() {
//http package has methods for dealing with requests
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
urlPathElements := strings.Split(r.URL.Path, "/")
//if the request is GET with correct syntax
if urlPathElements[1] == "roman_number" {
number, _ := strconv.Atoi(strings.TrimSpace(urlPathElements[2]))
if number == 0 || number > 10 {
//if resource is not in the list, send NotFound status
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("404 - Not Found"))
} else {
fmt.Fprintf(w, "%q", html.EscapeString(romans.Numerals[number]))
}
} else {
// For all other requests, tell that client sent a bad request
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("400 - Bad Request"))
}
})
//create a server and run it on 8000 port
s := &http.Server{
Addr: ":8000",
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
s.ListenAndServe()
}