-
Notifications
You must be signed in to change notification settings - Fork 0
/
static_web_page.go
71 lines (60 loc) · 1.39 KB
/
static_web_page.go
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"strings"
"encoding/base64"
"io/ioutil"
"mime"
"net/http"
)
type staticWebPage struct {
Content []byte
ContentType string
}
var webPages map[string]*staticWebPage = make(map[string]*staticWebPage)
func newStaticWebPage(name, p string) *staticWebPage {
reader := strings.NewReader(p)
b64 := base64.NewDecoder(base64.StdEncoding, reader)
data, err := ioutil.ReadAll(b64)
if err != nil {
panic(err.Error())
}
mtype := "application/octet-string"
for i := len(name) - 1; i >= 0; i-- {
if name[i] == '.' {
m := mime.TypeByExtension(name[i:])
if m != "" {
mtype = m
}
break
}
}
res := &staticWebPage{data, mtype}
webPages[name] = res
return res
}
func (p *staticWebPage) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
resp.Header().Add("Content-Type", p.ContentType)
resp.WriteHeader(http.StatusOK)
resp.Write(p.Content)
}
type safeHandle struct {
Url string
Handle http.Handler
}
func (h safeHandle) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
if req.URL.Path != h.Url {
resp.Header().Add("Content-Type", "text/plain")
resp.WriteHeader(http.StatusNotFound)
resp.Write([]byte("File not found"))
return
}
h.Handle.ServeHTTP(resp, req)
}
func handleStaticPages() {
for k, p := range(webPages) {
http.Handle("/" + k, safeHandle{"/" + k, p})
if k == "index.html" {
http.Handle("/", safeHandle{"/", p})
}
}
}