-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
38 lines (32 loc) · 949 Bytes
/
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
package main
import (
"fmt"
"net/http"
"strconv"
"math/big"
"github.com/gorilla/mux"
)
func factorial(x *big.Int) *big.Int {
n := big.NewInt(1)
if x.Cmp(big.NewInt(0)) == 0 {
return n
}
return n.Mul(x, factorial(n.Sub(x, n)))
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/api/v1/{number}", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
number, err := strconv.Atoi(vars["number"])
if err != nil {
return
}
fmt.Fprintf(w, "%d! = %d\n", number, factorial(big.NewInt(int64(number))))
})
r.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Usage: http://factorial-app.okanozdemir.dev/api/v1/${NUMBER}\n" +
"Example request: curl http://factorial-app.okanozdemir.dev/api/v1/5\n" +
"Example response: 5! = 120\n")
})
http.ListenAndServe(":80", r)
}