-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.go
226 lines (189 loc) · 4.9 KB
/
server.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// SPDX-FileCopyrightText: 2019 Shulhan <[email protected]>
// SPDX-License-Identifier: GPL-3.0-or-later
package ciigo
import (
"bytes"
"fmt"
"html/template"
"log"
"net/http"
"path"
"strings"
libhttp "git.sr.ht/~shulhan/pakakeh.go/lib/http"
"git.sr.ht/~shulhan/pakakeh.go/lib/memfs"
)
// InitHTTPServer create an HTTP server to serve HTML files in directory
// defined in "[ConvertOptions].Root".
//
// The address parameter is optional, if not set its default to ":8080".
// The htmlTemplate parameter is optional, if not set its default to
// embedded HTML template.
func (ciigo *Ciigo) InitHTTPServer(opts ServeOptions) (err error) {
var logp = `InitHTTPServer`
err = opts.init()
if err != nil {
return fmt.Errorf(`%s: %w`, logp, err)
}
if opts.Mfs == nil {
opts.IsDevelopment = true
var mfsopts = &memfs.Options{
Root: opts.Root,
Excludes: defExcludes,
TryDirect: true,
}
opts.Mfs, err = memfs.New(mfsopts)
if err != nil {
return fmt.Errorf(`%s: %w`, logp, err)
}
} else {
opts.Mfs.Opts.TryDirect = opts.IsDevelopment
}
ciigo.serveOpts = opts
var httpdOpts = libhttp.ServerOptions{
Memfs: opts.Mfs,
Address: opts.Address,
EnableIndexHTML: opts.EnableIndexHTML,
}
if opts.IsDevelopment {
httpdOpts.HandleFS = ciigo.onGet
}
ciigo.HTTPServer, err = libhttp.NewServer(httpdOpts)
if err != nil {
return fmt.Errorf(`%s: %w`, logp, err)
}
var epInSearch = libhttp.Endpoint{
Method: libhttp.RequestMethodGet,
Path: `/_internal/search`,
RequestType: libhttp.RequestTypeQuery,
ResponseType: libhttp.ResponseTypeHTML,
Call: ciigo.onSearch,
}
err = ciigo.HTTPServer.RegisterEndpoint(epInSearch)
if err != nil {
return fmt.Errorf(`%s: %w`, logp, err)
}
var pathHTMLTemplate string
if opts.IsDevelopment {
pathHTMLTemplate = opts.HTMLTemplate
}
ciigo.converter, err = NewConverter(pathHTMLTemplate)
if err != nil {
return fmt.Errorf(`%s: %w`, logp, err)
}
if !opts.IsDevelopment {
var tmplNode *memfs.Node
tmplNode, _ = opts.Mfs.Get(internalTemplatePath)
if tmplNode != nil {
ciigo.converter.tmpl, err = ciigo.converter.tmpl.Parse(string(tmplNode.Content))
if err != nil {
return fmt.Errorf(`%s: %s`, logp, err)
}
}
}
if opts.IsDevelopment {
ciigo.watcher, err = newWatcher(ciigo.converter, opts.ConvertOptions)
if err != nil {
return fmt.Errorf(`%s: %w`, logp, err)
}
ciigo.converter.convertFileMarkups(ciigo.watcher.fileMarkups, false)
}
return nil
}
// Serve start the HTTP web server.
func (ciigo *Ciigo) Serve() (err error) {
var logp = `Serve`
if ciigo.serveOpts.IsDevelopment {
err = ciigo.watcher.start()
if err != nil {
return fmt.Errorf(`%s: %w`, logp, err)
}
}
log.Printf(`ciigo: starting HTTP server at http://%s for %q`,
ciigo.HTTPServer.Options.Address,
ciigo.HTTPServer.Options.Memfs.Opts.Root)
err = ciigo.HTTPServer.Start()
if err != nil {
return fmt.Errorf(`%s: %w`, logp, err)
}
if ciigo.serveOpts.IsDevelopment {
ciigo.watcher.stop()
}
return nil
}
func (ciigo *Ciigo) onSearch(epr *libhttp.EndpointRequest) (resBody []byte, err error) {
var (
logp = `onSearch`
fhtml *fileHTML
buf bytes.Buffer
q string
results []memfs.SearchResult
)
q = epr.HTTPRequest.Form.Get(`q`)
results = ciigo.HTTPServer.Options.Memfs.Search(strings.Fields(q), 0)
err = ciigo.converter.tmplSearch.Execute(&buf, results)
if err != nil {
return nil, fmt.Errorf(`%s: %w`, logp, err)
}
fhtml = &fileHTML{
Body: template.HTML(buf.String()),
}
buf.Reset()
err = ciigo.converter.tmpl.Execute(&buf, fhtml)
if err != nil {
return nil, fmt.Errorf(`%s: %w`, logp, err)
}
resBody = buf.Bytes()
return resBody, nil
}
// onGet when user reload the page from browser, inspect the HTML file by
// checking if its older that the adoc.
// If yes, it will auto convert the adoc and return the new content of HTML
// files.
func (ciigo *Ciigo) onGet(
node *memfs.Node, _ http.ResponseWriter, req *http.Request,
) (out *memfs.Node) {
var (
logp = `onGet`
file string
)
if node == nil {
file = req.URL.Path
} else {
if node.IsDir() {
file = path.Join(node.Path, `index.html`)
} else {
if len(req.URL.Path) > len(node.Path) {
file = req.URL.Path
} else {
file = node.Path
}
}
}
if file[len(file)-1] == '/' {
file = path.Join(file, `index.html`)
}
var (
fmarkup *FileMarkup
isNew bool
)
fmarkup, isNew = ciigo.watcher.getFileMarkupByHTML(file)
if fmarkup == nil {
// File is not HTML or no markup files created from it.
return node
}
var err error
if isNew || ciigo.converter.shouldConvert(fmarkup) {
err = ciigo.converter.ToHTMLFile(fmarkup)
if err != nil {
log.Printf(`%s: failed to convert markup file %q: %s`,
logp, fmarkup.path, err)
return node
}
}
out, err = ciigo.serveOpts.Mfs.Get(file)
if err != nil {
log.Printf(`%s: failed to get %q: %s`, logp, file, err)
return node
}
return out
}