-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_tool.go
111 lines (89 loc) · 2.83 KB
/
http_tool.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package th_tools
import (
"fmt"
"io"
"log"
"net/http"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
//"github.com/NYTimes/gziphandler"
"net"
)
type HttpTool struct {
Methods map[string]map[string]http.Handler
Router *mux.Router
}
func (ht *HttpTool) Init() {
ht.Router = mux.NewRouter()
ht.Methods = make(map[string]map[string]http.Handler)
}
func (ht *HttpTool) Get(route string, handler http.Handler) {
if ht.Methods["GET"] == nil {
ht.Methods["GET"] = make(map[string]http.Handler)
}
ht.Methods["GET"][route] = handler
}
func (ht *HttpTool) Post(route string, handler http.Handler) {
if ht.Methods["POST"] == nil {
ht.Methods["POST"] = make(map[string]http.Handler)
}
ht.Methods["POST"][route] = handler
}
func (ht *HttpTool) Put(route string, handler http.Handler) {
if ht.Methods["PUT"] == nil {
ht.Methods["PUT"] = make(map[string]http.Handler)
}
ht.Methods["PUT"][route] = handler
}
func (ht *HttpTool) Delete(route string, handler http.Handler) {
if ht.Methods["DELETE"] == nil {
ht.Methods["DELETE"] = make(map[string]http.Handler)
}
ht.Methods["DELETE"][route] = handler
}
func (ht *HttpTool) Handle(route string, handler http.Handler) {
ht.Router.Handle(route, handler)
}
func (ht HttpTool) Route(listener net.Listener, logFile io.Writer) {
for method := range ht.Methods {
for route := range ht.Methods[method] {
fmt.Println(method, route)
if logFile != nil {
ht.Router.Handle(route, handlers.LoggingHandler(logFile, ht.Methods[method][route])).Methods(method)
} else {
ht.Router.Handle(route, ht.Methods[method][route]).Methods(method)
}
}
}
/*
cache := func(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
// 1000 * 60 = 60000 = 60 seconds / 1 minute
// 1000 * 60 * 60 = 3600000 = 1 hour
// 1000 * 60 * 60 * 24 = 86400000 = 24 hours / 1 day
// 1000 * 60 * 60 * 24 * 30 = 2592000000 = 30 days
w.Header().Set("Cache-control", "public, max-age=86400000")
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
*/
// nocache := func(next http.Handler) http.Handler {
// fn := func(w http.ResponseWriter, r *http.Request) {
// // 1000 * 60 = 60000 = 60 seconds / 1 minute
// // 1000 * 60 * 60 = 3600000 = 1 hour
// // 1000 * 60 * 60 * 24 = 86400000 = 24 hours / 1 day
// // 1000 * 60 * 60 * 24 * 30 = 2592000000 = 30 days
// w.Header().Set("Cache-control", "no-cache, no-store, must-revalidate")
// w.Header().Set("Pragma", "no-cache")
// w.Header().Set("Expires ", "0")
// next.ServeHTTP(w, r)
// }
// return http.HandlerFunc(fn)
// }
//
// ht.Router.PathPrefix("/").Handler(nocache(gziphandler.GzipHandler(http.FileServer(http.Dir("./public/")))))
// http.Handle("/", ht.Router)
log.Println(fmt.Sprintf("Listening"))
http.Serve(listener, nil)
}