master
bel 2023-08-17 16:39:21 -06:00
parent a1b6ca009f
commit 1a4be5d384
1 changed files with 11 additions and 3 deletions

14
main.go
View File

@ -33,6 +33,7 @@ func main() {
fs.Append(args.STRING, "p", "port to serve", "8100") fs.Append(args.STRING, "p", "port to serve", "8100")
fs.Append(args.STRING, "u", "user:pass for basic auth", "") fs.Append(args.STRING, "u", "user:pass for basic auth", "")
fs.Append(args.BOOL, "md", "whether to render markdown as html", true) fs.Append(args.BOOL, "md", "whether to render markdown as html", true)
fs.Append(args.BOOL, "log", "emit access logs", false)
fs.Append(args.BOOL, "ro", "read only mode", false) fs.Append(args.BOOL, "ro", "read only mode", false)
fs.Append(args.BOOL, "https", "https only", false) fs.Append(args.BOOL, "https", "https only", false)
fs.Append(args.STRING, "md-css", "css to load for md", "/dev/null") fs.Append(args.STRING, "md-css", "css to load for md", "/dev/null")
@ -49,6 +50,7 @@ func main() {
https := fs.Get("https").GetBool() https := fs.Get("https").GetBool()
mdCss := fs.Get("md-css").GetString() mdCss := fs.Get("md-css").GetString()
mdClass := fs.Get("md-class").GetString() mdClass := fs.Get("md-class").GetString()
accessLogging := fs.GetBool("log")
if mdCss != "" { if mdCss != "" {
b, err := ioutil.ReadFile(mdCss) b, err := ioutil.ReadFile(mdCss)
if err != nil { if err != nil {
@ -95,15 +97,21 @@ func main() {
} }
p := strings.TrimPrefix(fs.Get("p").GetString(), ":") p := strings.TrimPrefix(fs.Get("p").GetString(), ":")
http.Handle("/", http.HandlerFunc(handler(userPass, https, ro, d, md, mdCss, mdClass))) http.Handle("/", http.HandlerFunc(handler(userPass, https, ro, d, md, mdCss, mdClass, accessLogging)))
log.Printf("Serving %s on HTTP port: %s\n", d, p) log.Printf("Serving %s on HTTP port: %s\n", d, p)
log.Fatal(http.ListenAndServe(":"+p, nil)) log.Fatal(http.ListenAndServe(":"+p, nil))
} }
func handler(userPass string, https, ro bool, d string, md bool, mdCss, mdClass string) http.HandlerFunc { func handler(userPass string, https, ro bool, d string, md bool, mdCss, mdClass string, accessLogging bool) http.HandlerFunc {
return httpsOnly(https, gzip(basicAuth(userPass, endpoints(ro, withDel(ro, withMD(d, md, mdCss, mdClass, fserve(d))))))) return withAccessLogging(accessLogging, httpsOnly(https, gzip(basicAuth(userPass, endpoints(ro, withDel(ro, withMD(d, md, mdCss, mdClass, fserve(d))))))))
}
func withAccessLogging(enabled bool, h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
h(w, r)
}
} }
func writeMeta(w http.ResponseWriter) { func writeMeta(w http.ResponseWriter) {