From ff3ed3a57e9880ff195c314d7cebdd9e9105570c Mon Sep 17 00:00:00 2001 From: bel Date: Sun, 26 Jan 2020 18:04:42 +0000 Subject: [PATCH] gzip --- server/routes.go | 81 ++++++++++++------------------------------------ 1 file changed, 19 insertions(+), 62 deletions(-) diff --git a/server/routes.go b/server/routes.go index c758ab3..a61ab3b 100755 --- a/server/routes.go +++ b/server/routes.go @@ -3,6 +3,7 @@ package server import ( "fmt" "io" + "local/gziphttp" "local/router" "local/todo-server/config" "log" @@ -20,78 +21,20 @@ func (s *Server) Routes() error { }{ { path: "/", - handler: s.index, + handler: s.gzip(s.index), }, { path: "/mytinytodo_lang.php", - handler: s.lang, + handler: s.gzip(s.lang), }, { path: fmt.Sprintf("%s%s", router.Wildcard, router.Wildcard), - handler: s.phpProxy, + handler: s.gzip(s.phpProxy), }, { path: "/ajax.php", - handler: s.HandleAjax, + handler: s.gzip(s.HandleAjax), }, - /* - { - path: "db/index.html", - handler: s.static, - }, - { - path: "jquery/index.html", - handler: s.static, - }, - { - path: "lang/index.html", - handler: s.static, - }, - { - path: "themes/index.html", - handler: s.static, - }, - { - path: "tmp/index.html", - handler: s.static, - }, - { - path: "jquery/jquery-1.4.4.min.js", - handler: s.static, - }, - { - path: "jquery/jquery-ui-1.8.7.custom.min.js", - handler: s.static, - }, - { - path: "jquery/jquery.autocomplete-1.1.js", - handler: s.static, - }, - { - path: "mytinytodo.js", - handler: s.static, - }, - { - path: "mytinytodo_ajax_storage.js", - handler: s.static, - }, - { - path: "testdata/mytinytodo2/themes/default/pda.css", - handler: s.static, - }, - { - path: "testdata/mytinytodo2/themes/default/print.css", - handler: s.static, - }, - { - path: "testdata/mytinytodo2/themes/default/style.css", - handler: s.static, - }, - { - path: "testdata/mytinytodo2/themes/default/style_rtl.css", - handler: s.static, - }, - */ } for _, route := range routes { @@ -161,3 +104,17 @@ func (s *Server) phpProxy(w http.ResponseWriter, r *http.Request) { func (s *Server) static(w http.ResponseWriter, r *http.Request) { s.fileServer.ServeHTTP(w, r) } + +func (s *Server) gzip(h http.HandlerFunc) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + if gziphttp.Can(r) { + gz := gziphttp.New(w) + defer gz.Close() + w = gz + } + if filepath.Ext(r.URL.Path) == ".css" { + w.Header().Set("Content-Type", "text/css; charset=utf-8") + } + h(w, r) + } +}