-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfactorial_api.go
More file actions
56 lines (44 loc) · 1.24 KB
/
factorial_api.go
File metadata and controls
56 lines (44 loc) · 1.24 KB
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
package main
import (
"fmt"
"log"
"net/http"
"strconv"
"github.com/gorilla/mux"
)
func main() {
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/", Index)
router.HandleFunc("/todos", TodoIndex)
router.HandleFunc("/fact/{factId}", FactorialCall)
router.HandleFunc("/factorial/{factId}", FactorialCall)
router.HandleFunc("/todos/{todoId}", TodoShow)
log.Fatal(http.ListenAndServe(":8080", router))
}
func Index(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Welcome to the Factorial Portal!")
}
func TodoIndex(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Todo Index!")
}
func TodoShow(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
todoId := vars["todoId"]
fmt.Fprintln(w, "Todo show:", todoId)
}
func FactorialCall(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
num := vars["factId"]
i, err := strconv.Atoi(num)
if err != nil {
log.Fatal(err)
}
fmt.Fprintf(w, "Factorial for number %d is: %d \nBye, see you in next run..\n", i, Factorial(uint64(i)))
}
func Factorial(n uint64)(result uint64) {
if (n > 0) {
result = n * Factorial(n-1)
return result
}
return 1
}