Support gzip
This commit is contained in:
parent
0369976212
commit
0c4498ccee
12
main.go
12
main.go
|
@ -4,8 +4,10 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"mime"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
func handler(w http.ResponseWriter, r *http.Request) {
|
func handler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
@ -14,8 +16,18 @@ func handler(w http.ResponseWriter, r *http.Request) {
|
||||||
file, err := os.Stat(path)
|
file, err := os.Stat(path)
|
||||||
if err == nil && !file.IsDir() {
|
if err == nil && !file.IsDir() {
|
||||||
// file exists
|
// file exists
|
||||||
|
gz := path + ".gz"
|
||||||
|
file, err := os.Stat(gz)
|
||||||
|
t := mime.TypeByExtension(filepath.Ext(path))
|
||||||
|
if err == nil && !file.IsDir() && t != "" {
|
||||||
|
fmt.Println(" => " + gz)
|
||||||
|
w.Header().Add("Content-Encoding", "gzip")
|
||||||
|
w.Header().Add("Content-Type", t)
|
||||||
|
http.ServeFile(w, r, gz)
|
||||||
|
} else {
|
||||||
fmt.Println(" => " + path)
|
fmt.Println(" => " + path)
|
||||||
http.ServeFile(w, r, path)
|
http.ServeFile(w, r, path)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// file does not exist
|
// file does not exist
|
||||||
index := "index.html"
|
index := "index.html"
|
||||||
|
|
Loading…
Reference in New Issue