38 lines
744 B
Go
38 lines
744 B
Go
// package containing the static server
|
|
package static
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
func ServeFiles(w http.ResponseWriter, r *http.Request) {
|
|
path := r.URL.Path
|
|
pl := strings.Split(path, ".")
|
|
l := len(pl)
|
|
reg := regexp.MustCompile(`.*\.(png|jpg|jpeg|css|js|html|gz)$`)
|
|
switch {
|
|
case l <= 1:
|
|
fmt.Println("path not found " + r.URL.Path)
|
|
http.NotFound(w, r)
|
|
return
|
|
case l >= 2:
|
|
if ok := reg.Match([]byte(r.URL.Path)); ok {
|
|
if pl[len(pl)-1] == "gz" {
|
|
w.Header().Set("Content-Encoding", "gzip")
|
|
}
|
|
hs := http.FileServer(http.Dir("./assets"))
|
|
http.StripPrefix("/static/", hs).ServeHTTP(w, r)
|
|
return
|
|
} else {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
default:
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
}
|