-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponse.go
More file actions
46 lines (36 loc) · 764 Bytes
/
response.go
File metadata and controls
46 lines (36 loc) · 764 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package mbedder
import (
"io/ioutil"
"mime"
"net/http"
"os"
"strings"
)
type response struct {
filename string
}
func newResponse(filename string) (*response, error) {
var resp response
_, err := os.Stat(filename)
if err != nil {
return nil, err
}
resp.filename = filename
return &resp, nil
}
func (resp *response) ServeHTTP(w http.ResponseWriter, r *http.Request) {
data, err := ioutil.ReadFile(resp.filename)
if err != nil {
http.Error(w, "could not read file", 500)
return
}
var mimeType string
extension := strings.Split(resp.filename, ".")
if len(extension) > 0 {
mimeType = mime.TypeByExtension(extension[len(extension)-1])
} else {
mimeType = "text/plain"
}
w.Header().Add("Content-Type", mimeType)
w.Write(data)
}