forked from eficode-academy/go-roman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
42 lines (33 loc) · 810 Bytes
/
http.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
package main
import (
"fmt"
"net/http"
"strconv"
)
func hello(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello world!")
}
func to_roman(n int) string {
if n == 2 {
return "III"
}
return "I" // oopsie
}
type romanGenerator int
func (n romanGenerator) ServeHTTP(w http.ResponseWriter, r *http.Request) {
number := r.URL.Query().Get("number")
if len(number) == 0 {
fmt.Fprintf(w, "Please pass the number as parameter in the URL")
}
i, err := strconv.Atoi(number)
if err == nil {
fmt.Fprintf(w, "Here's your number: %s\n", to_roman(i))
}
}
func main() {
h := http.NewServeMux()
h.Handle("/roman/", romanGenerator(1))
h.HandleFunc("/", hello)
err := http.ListenAndServe(":8000", h)
panic(err)
}