-
Notifications
You must be signed in to change notification settings - Fork 4
/
gm_serve.go
120 lines (113 loc) · 3.82 KB
/
gm_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
111
112
113
114
115
116
117
118
119
120
package main
import (
"fmt"
"net"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"sync/atomic"
"time"
"github.com/kpym/gm/internal/browser"
)
// availablePort provides the first available port after 8080
// or 8180 if no available ports are present.
func availablePort() (port string) {
for i := 8080; i < 8181; i++ {
port = strconv.Itoa(i)
if ln, err := net.Listen("tcp", "localhost:"+port); err == nil {
ln.Close()
break
}
}
return port
}
// serveFiles serve the local folder `serveDir`.
// If an .md (or corresponding .html) file is requested it is compiled and send as html.
func serveFiles() {
var lastMethodPath string
var ticker *time.Ticker // used to exit if no request is received for timeout seconds
var exit atomic.Bool // is set to true on every request and reset to false on every tick of the ticker
var livejsactive atomic.Bool // is set to true if the last request was for non static content
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// say thet we are alive
exit.Store(false)
// by default live.js is active (serving .md or related files)
// it is reset to false if the request is for static content later
livejsactive.Store(true)
// how should I print the info?
filename := filepath.Join(serveDir, r.URL.Path)
newMethodPath := fmt.Sprintf("\n%s '%s':", r.Method, r.URL.Path)
if newMethodPath != lastMethodPath {
lastMethodPath = newMethodPath
info(newMethodPath)
}
// serve the file
if strings.HasSuffix(filename, ".html") {
// try first to serve the corresponding .md file
// if it is not present, serve the .html as static file
filename = filename[0:len(filename)-5] + ".md"
}
if strings.HasSuffix(filename, "md") {
if r.Method == "HEAD" {
info(".")
if fstat, err := os.Stat(filename); err == nil {
w.Header().Set("Last-Modified", fstat.ModTime().UTC().Format(http.TimeFormat))
w.Header().Set("Content-Type", "text/html")
w.Write([]byte{})
}
return
}
if content, err := os.ReadFile(filename); err == nil {
if html, err := compile(content); err == nil {
info(" serve converted .md file.")
w.Write(html)
return
}
}
}
switch r.URL.Path {
case "/favicon.ico":
info(" serve internal png.")
w.Header().Set("Cache-Control", "max-age=86400") // 86400 s = 1 day
w.Header().Set("Expires", time.Now().Add(24*time.Hour).UTC().Format(http.TimeFormat))
w.Write(favIcon)
return
case "/live.js":
info(" serve live.js.")
w.Header().Set("Cache-Control", "max-age=86400") // 86400 s = 1 day
w.Header().Set("Expires", time.Now().Add(24*time.Hour).UTC().Format(http.TimeFormat))
w.Write(livejs)
return
}
info(" serve raw file.")
livejsactive.Store(false) // is serving file without live.js
w.Header().Set("Cache-Control", "no-store")
http.FileServer(http.Dir(serveDir)).ServeHTTP(w, r)
})
// start the exit timer ?
if liveupdate && timeout > 0 {
// if no livejs request is received in timeout seconds, exit
ticker = time.NewTicker(time.Duration(timeout) * time.Second)
defer ticker.Stop()
go func() {
for {
// wait for timeout seconds
<-ticker.C
// check if the last request was more than timeout seconds ago
// and if it was for non static content, so that live.js should be active
if exit.Swap(true) && livejsactive.Load() {
info("\nNo request for %d seconds. Exit.\n\n", timeout)
mainEnd()
}
}
}()
}
port := availablePort()
info("start serving '%s' folder to localhost:%s.\n", serveDir, port)
url := "http://localhost:" + port + "/" + serveFile
err := browser.Open(url)
try(err, "Can't open the web browser, but you can visit now:", url)
check(http.ListenAndServe("localhost:"+port, nil))
}