Support gzip

This commit is contained in:
amutake 2015-01-03 15:01:03 +09:00
parent 0369976212
commit 0c4498ccee
1 changed files with 14 additions and 2 deletions

16
main.go
View File

@ -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
fmt.Println(" => " + path) gz := path + ".gz"
http.ServeFile(w, r, path) 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)
http.ServeFile(w, r, path)
}
} else { } else {
// file does not exist // file does not exist
index := "index.html" index := "index.html"