-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathwebserver.go
45 lines (38 loc) · 911 Bytes
/
webserver.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
package main
import (
"log"
"net/http"
"net/http/cgi"
"os"
"path/filepath"
)
// The built-in web server, which serves URLs under http://203.0.113.1/
const localServer = "203.0.113.1"
func (c *config) startWebServer() {
if c.StaticFilesDir != "" {
c.ServeMux.Handle("/", http.FileServer(http.Dir(c.StaticFilesDir)))
}
if c.CGIBin != "" {
dir, err := os.Open(c.CGIBin)
if err != nil {
log.Println("Could not open CGI directory:", err)
return
}
defer dir.Close()
info, err := dir.Readdir(0)
if err != nil {
log.Println("Could not read CGI directory:", err)
return
}
for _, fi := range info {
if mode := fi.Mode(); (mode&os.ModeType == 0) && (mode.Perm()&0100 != 0) {
// It's an executable file.
name := "/" + fi.Name()
scriptPath := filepath.Join(c.CGIBin, fi.Name())
c.ServeMux.Handle(name, &cgi.Handler{
Path: scriptPath,
})
}
}
}
}