-
Notifications
You must be signed in to change notification settings - Fork 0
/
april.go
39 lines (31 loc) · 801 Bytes
/
april.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
package main
import (
"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
)
func main() {
handleRequests()
}
func handleRequests() {
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/", homePageHandler)
router.HandleFunc("/revert/{word}", wordRevertHandler).Methods("GET")
log.Fatal(http.ListenAndServe(":2999", router))
}
func homePageHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello world!")
}
func wordRevertHandler(w http.ResponseWriter, r *http.Request) {
word := mux.Vars(r)["word"]
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, "If you revert "+word+" you get "+reverse(word))
}
func reverse(word string) string {
r := []rune(word)
for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
r[i], r[j] = r[j], r[i]
}
return string(r)
}