forked from JohnStarich/go
/
main.go
192 lines (172 loc) · 4.62 KB
/
main.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//go:build !windows
// +build !windows
// Command watch generates docs and starts an HTTP endpoint to serve them. Also runs a file watcher on the current module to regenerate docs on change events.
//
// watch is useful for testing godoc code comments and while developing on gopages itself.
// Accepts the same flags as gopages, -gh-pages flags are ignored.
package main
import (
"context"
"errors"
"flag"
"fmt"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/memfs"
"github.com/go-git/go-billy/v5/osfs"
"github.com/therve/go/gopages/cmd"
"github.com/therve/go/gopages/internal/flags"
"github.com/therve/go/gopages/internal/generate"
"github.com/therve/go/gopages/internal/generate/source"
"github.com/therve/go/gopages/internal/module"
"github.com/johnstarich/go/pipe"
)
const (
lastUpdatedHeader = "GoPages-Last-Updated"
)
func main() {
args, usageOutput, err := flags.Parse(os.Args[1:]...)
switch {
case err == nil:
case errors.Is(err, flag.ErrHelp):
fmt.Print(usageOutput)
return
default:
fmt.Print(usageOutput)
cmd.Exit(cmd.ExitCodeInvalidUsage)
}
args.Watch = true
var updatedTime string
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
wd, err := os.Getwd()
if err != nil {
panic(err)
}
modulePackage, err := module.Package(wd)
if err != nil {
panic(err)
}
linker, err := args.Linker(modulePackage)
if err != nil {
panic(err)
}
fs, err := pagesFileSystem(ctx, wd, &updatedTime, args, linker)
if err != nil {
panic(err)
}
fileServer := http.FileServer(fs)
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set(lastUpdatedHeader, updatedTime)
_, err := fs.Open(r.URL.Path)
if err == nil {
fileServer.ServeHTTP(w, r)
return
}
if !strings.HasSuffix(r.URL.Path, "/") {
// GitHub Pages automatically looks up a corresponding .html file if it exists
_, err := fs.Open(r.URL.Path + ".html")
if err == nil {
r.URL.Path += ".html"
fileServer.ServeHTTP(w, r)
return
}
}
r.URL.Path = "/404.html"
r.URL.RawPath = "/404.html"
fileServer.ServeHTTP(w, r)
})
server := http.Server{
Addr: ":8080",
Handler: mux,
ReadHeaderTimeout: time.Minute,
}
fmt.Println("Starting demo server on :8080...")
_ = server.ListenAndServe()
}
type pagesFSArgs struct {
flags.Args
Ctx context.Context
ModulePath string
UpdateTime *string
Linker source.Linker
}
var pagesFSPipe = pipe.New(pipe.Options{}).
Append(func(args []interface{}) pagesFSArgs {
return args[0].(pagesFSArgs)
}).
Append(func(args pagesFSArgs) (pagesFSArgs, string, error) {
modulePackage, err := module.Package(args.ModulePath)
return args, modulePackage, err
}).
Append(func(args pagesFSArgs, modulePackage string) (pagesFSArgs, billy.Filesystem, error) {
src := osfs.New("")
fs := memfs.New()
err := watch(args.Ctx, args.ModulePath, func() error {
err := generate.Docs(args.ModulePath, modulePackage, src, fs, args.Args, args.Linker)
*args.UpdateTime = time.Now().Format(time.RFC3339)
return err
})
return args, fs, err
}).
Append(func(args pagesFSArgs, fs billy.Filesystem) (http.FileSystem, error) {
rootedFS, err := fs.Chroot(args.OutputPath)
return &httpFSWrapper{base: args.BaseURL, Filesystem: rootedFS}, err
})
func pagesFileSystem(ctx context.Context, modulePath string, updateTime *string, args flags.Args, linker source.Linker) (http.FileSystem, error) {
out, err := pagesFSPipe.Do(pagesFSArgs{
Args: args,
Ctx: ctx,
ModulePath: modulePath,
UpdateTime: updateTime,
Linker: linker,
})
var fs http.FileSystem
if err == nil {
fs = out[0].(http.FileSystem)
}
return fs, err
}
type httpFSWrapper struct {
base string
billy.Filesystem
}
func (h *httpFSWrapper) Open(name string) (http.File, error) {
if !strings.HasPrefix(name, h.base) {
return nil, os.ErrNotExist
}
name = strings.TrimPrefix(name, h.base)
name = filepath.FromSlash(name)
info, err := h.Filesystem.Stat(name)
if err != nil {
return nil, err
}
var file billy.File
// memfs.Open doesn't work for directories, so create a false dir for those instead
if info.IsDir() {
file, err = memfs.New().Create(name)
} else {
file, err = h.Filesystem.Open(name)
}
return &httpFileWrapper{
File: file,
fs: h.Filesystem,
name: name,
}, err
}
type httpFileWrapper struct {
billy.File
name string
fs billy.Filesystem
}
func (h *httpFileWrapper) Readdir(count int) ([]os.FileInfo, error) {
return h.fs.ReadDir(h.name)
}
func (h *httpFileWrapper) Stat() (os.FileInfo, error) {
return h.fs.Stat(h.name)
}