-
Notifications
You must be signed in to change notification settings - Fork 762
/
serve.go
73 lines (70 loc) · 1.71 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
package main
import (
"context"
"errors"
"log"
"net"
"net/http"
"os"
"path/filepath"
"strconv"
)
func sendThatFile(basepath string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
filename := filepath.Join(basepath, filepath.Clean(string([]rune{filepath.Separator})+r.URL.Path))
f, err := os.Open(filename)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
http.NotFound(w, r)
return
}
http.Error(w, "whoops", http.StatusInternalServerError)
return
}
finfo, err := f.Stat()
if err != nil {
http.Error(w, "whoops", http.StatusInternalServerError)
return
}
http.ServeContent(w, r, filename, finfo.ModTime(), f)
}
}
func main() {
args := os.Args
if len(args) < 2 {
log.Fatal("requires subdirectory path [and optional port [and optional port file name]]")
}
basedir := args[1]
port := "0"
if len(args) > 2 {
port = args[2]
}
http.HandleFunc("/", sendThatFile(basedir))
server := http.Server{
Addr: ":" + port,
BaseContext: func(l net.Listener) context.Context {
if tcp, ok := l.Addr().(*net.TCPAddr); ok {
if len(args) > 3 {
f, err := os.CreateTemp(filepath.Dir(args[3]), filepath.Base(args[3]))
if err != nil {
log.Fatalf("%v", err)
}
tempName := f.Name()
bytes := []byte(strconv.Itoa(tcp.Port))
if n, err := f.Write(bytes); err != nil || n != len(bytes) {
if err != nil {
log.Fatalf("%v", err)
}
log.Fatalf("short write: %d != %d", n, len(bytes))
}
f.Close()
if err := os.Rename(tempName, args[3]); err != nil {
log.Fatalf("rename: %v", err)
}
}
}
return context.Background()
},
}
log.Fatal(server.ListenAndServe())
}