-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go.bkp
76 lines (61 loc) · 1.58 KB
/
main.go.bkp
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"fmt"
"log"
"net/http"
"github.com/GuilhermeCaruso/bellt"
"os/exec"
)
//main
func main() {
router := bellt.NewRouter()
router.HandleFunc("/contact/{id}/{user}", bellt.Use(
exampleHandler,
middlewareOne,
middlewareTwo,
), "GET")
router.HandleFunc("/tdc", bellt.Use(
exampleNewHandler,
middlewareOne,
middlewareTwo,
), "GET")
router.HandleGroup("/api",
router.SubHandleFunc("/check", bellt.Use(
exampleNewHandler,
middlewareOne,
middlewareTwo,
), "GET"),
router.SubHandleFunc("/check/{id}/{user}", bellt.Use(
exampleHandler,
middlewareOne,
middlewareTwo,
), "GET"),
)
log.Fatal(http.ListenAndServe(":8080", nil))
}
func exampleHandler(w http.ResponseWriter, r *http.Request) {
rv := bellt.RouteVariables(r)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(fmt.Sprintf(`{"id": %v, "user": %v}`, rv.GetVar("user"), rv.GetVar("id"))))
}
func exampleNewHandler(w http.ResponseWriter, r *http.Request) {
//rv := bellt.RouteVariables(r)
output, _ := exec.Command("/home/user/Golang/bin/hello").Output()
fmt.Println(output)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"msg": "Works"}`))
}
func middlewareOne(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
fmt.Println("Step One")
next.ServeHTTP(w, r)
}
}
func middlewareTwo(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
fmt.Println("Step Two")
next.ServeHTTP(w, r)
}
}