...
1
2
3
4
5
6
7
8
9
10
11
12
13
14 package server
15
16 import (
17 "net/http"
18 "path/filepath"
19 )
20
21 var mimeTypes = map[string]string{
22 ".cjs": "application/javascript",
23 ".css": "text/css",
24 ".eot": "font/eot",
25 ".gif": "image/gif",
26 ".ico": "image/x-icon",
27 ".jpg": "image/jpeg",
28 ".js": "application/javascript",
29 ".json": "application/json",
30 ".less": "text/plain",
31 ".map": "application/json",
32 ".otf": "font/otf",
33 ".png": "image/png",
34 ".svg": "image/svg+xml",
35 ".ttf": "font/ttf",
36 ".txt": "text/plain",
37 ".woff": "font/woff",
38 ".woff2": "font/woff2",
39 }
40
41 func StaticFileServer(root http.FileSystem) http.Handler {
42 return http.HandlerFunc(
43 func(w http.ResponseWriter, r *http.Request) {
44 fileExt := filepath.Ext(r.URL.Path)
45
46 if t, ok := mimeTypes[fileExt]; ok {
47 w.Header().Set("Content-Type", t)
48 }
49
50 http.FileServer(root).ServeHTTP(w, r)
51 },
52 )
53 }
54
View as plain text