-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.go
110 lines (88 loc) · 2.19 KB
/
serve.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"errors"
"net/http"
"os"
"path"
"strings"
"time"
)
const defaultAddress = "localhost:8080"
type resolution struct {
Resolved bool
Path string
ModTime time.Time
}
type handler struct {
Config *Config
}
func getServeAddress() string {
address := os.Getenv("SITE_ADDRESS")
if address == "" {
address = defaultAddress
}
return address
}
func resolve(config *Config, urlPath string) resolution {
base := path.Join(config.DstDir, urlPath)
if !strings.HasPrefix(base, config.DstDir) {
return resolution{Resolved: false}
}
entry, err := os.Stat(base)
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
check(err)
}
} else {
if !entry.IsDir() {
return resolution{
Resolved: true,
Path: base,
ModTime: entry.ModTime(),
}
}
}
if !strings.HasSuffix(urlPath, ".html") {
resolution := resolve(config, urlPath+".html")
if resolution.Resolved {
return resolution
}
}
if entry != nil && entry.IsDir() {
resolution := resolve(config, path.Join(urlPath, "index"))
if resolution.Resolved {
return resolution
}
}
return resolution{Resolved: false}
}
func (handler handler) ServeHTTP(res http.ResponseWriter, req *http.Request) {
urlPath := req.URL.Path
logger.Debugf("Incoming request (%v)", urlPath)
resolution := resolve(handler.Config, urlPath)
if !resolution.Resolved {
logger.Debugf("Request unresolved (%v -x 404)", urlPath)
res.WriteHeader(404)
resolution = resolve(handler.Config, handler.Config.NotFoundPath)
} else {
logger.Debugf("Request resolved (%v -> %v)", urlPath, resolution.Path)
}
if resolution.Resolved {
file, err := os.Open(resolution.Path)
check(err)
defer file.Close()
http.ServeContent(res, req, resolution.Path, resolution.ModTime, file)
} else {
res.Write([]byte("404 - Not Found"))
}
}
func Serve(config Config) {
logger.SetReportTimestamp(true)
logger.Info("Starting server...")
logger.SetReportTimestamp(false)
handler := handler{Config: &config}
address := getServeAddress()
logger.Infof("Hosting on 'http://%v/'\n (THIS SERVER IS FOR TESTING ONLY! DO NOT EXPOSE IT)", address)
logger.Print("Press '[Ctrl] + C' to exit")
check(http.ListenAndServe(address, handler))
}