-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
61 lines (54 loc) · 1.15 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
package main
import (
//standard
"fmt"
"net/http"
"strconv"
//github
"github.com/WestEast1st/FizzBuzzHTTPServer_GoLang/modules"
"github.com/gorilla/mux"
)
var route = mux.NewRouter()
/*
* controller
* helloController : top
* fizzBuzzController : fizzBuzz
*/
func helloController(writer http.ResponseWriter, request *http.Request) {
fmt.Fprint(writer, "<h1>Hello Fizz Buzz!</h1>")
}
func fizzBuzzController(writer http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request)
inputNum, err := strconv.Atoi(vars["num"])
//Processing other than int.
if err != nil {
StatusBadRequest(writer)
return
}
//success
fmt.Fprint(writer, "<h1>"+fizzbuzz.Handler(inputNum)+"</h1>")
}
/*
* error
* StatusBadRequest : 400 invalid number
*/
func StatusBadRequest(writer http.ResponseWriter) {
writer.WriteHeader(http.StatusBadRequest)
fmt.Fprint(writer, "<h1>invalid number</h1>")
}
/*
* HTTP Route
*/
func httpRoute() {
route.HandleFunc("/", helloController)
route.HandleFunc("/fizzbuzz/{num}", fizzBuzzController)
}
/*
* Main
* Main -> HTTP Route -> controller -> FizzBuzz
*
*/
func main() {
httpRoute()
http.ListenAndServe(":8080", route)
}