-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserve.go
58 lines (50 loc) · 1.73 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"github.com/NYTimes/gziphandler"
)
func serve(directoryToServe, basepath string, port int) error {
// Example in my case.
// go run . --input="../blog-test-source" --output="../blog-test" & go run demo/server.go --dir="../blog-test" --basepath="/blog-test/"
go func() {
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM)
<-sc
os.Exit(0)
}()
log.Printf("Serving %s at http://localhost:%d%s", directoryToServe, port, basepath)
log.Println("Please remember to only use this command to serve your website in a development scenario.")
portString := fmt.Sprintf("localhost:%d", port)
dir := dirWith404Handler{http.Dir(directoryToServe)}
handler := gziphandler.GzipHandler(http.FileServer(dir))
if basepath == "" {
return http.ListenAndServe(portString, handler)
}
// Making sure there's not too many or too little slashes ;)
basepath = "/" + strings.Trim(basepath, "/\\") + "/"
http.Handle(basepath, http.StripPrefix(basepath, handler))
return http.ListenAndServe(portString, nil)
}
type dirWith404Handler struct {
dir http.Dir
}
// Open implements [fs.FileSystem] by using os.Open. Additionally to the
// default implementation of [http.Dir], this implementation returns a 404
// page located at the root of the specified filesystem directory if the
// requested path doesn't correspond to a path.
func (d dirWith404Handler) Open(name string) (http.File, error) {
file, err := d.dir.Open(name)
if os.IsNotExist(err) {
file404, err := d.dir.Open("404.html")
// Technically we'd need the old error to indicate 404 to the
// browser, but for demo/test purposes, this'll do.
return file404, err
}
return file, err
}