Compare commits
5 Commits
v0.13.0
...
eaa8debb73
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
eaa8debb73 | ||
|
|
3727e2ee5e | ||
|
|
7fabfcd14d | ||
|
|
19c13c43f6 | ||
|
|
fc7451ab40 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,4 +1,5 @@
|
|||||||
*.key
|
*.key
|
||||||
|
/exec-*
|
||||||
*.crt
|
*.crt
|
||||||
*.pem
|
*.pem
|
||||||
*.swp
|
*.swp
|
||||||
|
|||||||
38
main.go
38
main.go
@@ -31,8 +31,10 @@ var (
|
|||||||
func main() {
|
func main() {
|
||||||
fs = args.NewArgSet()
|
fs = args.NewArgSet()
|
||||||
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.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, "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.STRING, "md-css", "css to load for md", "/dev/null")
|
fs.Append(args.STRING, "md-css", "css to load for md", "/dev/null")
|
||||||
fs.Append(args.STRING, "md-class", "class to wrap md", "phb")
|
fs.Append(args.STRING, "md-class", "class to wrap md", "phb")
|
||||||
fs.Append(args.STRING, "d", "static path to serve", "./public")
|
fs.Append(args.STRING, "d", "static path to serve", "./public")
|
||||||
@@ -41,8 +43,10 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
d := fs.Get("d").GetString()
|
d := fs.Get("d").GetString()
|
||||||
|
userPass := fs.Get("u").GetString()
|
||||||
md := fs.Get("md").GetBool()
|
md := fs.Get("md").GetBool()
|
||||||
ro := fs.Get("ro").GetBool()
|
ro := fs.Get("ro").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()
|
||||||
if mdCss != "" {
|
if mdCss != "" {
|
||||||
@@ -91,15 +95,15 @@ func main() {
|
|||||||
}
|
}
|
||||||
p := strings.TrimPrefix(fs.Get("p").GetString(), ":")
|
p := strings.TrimPrefix(fs.Get("p").GetString(), ":")
|
||||||
|
|
||||||
http.Handle("/", http.HandlerFunc(handler(ro, d, md, mdCss, mdClass)))
|
http.Handle("/", http.HandlerFunc(handler(userPass, https, ro, d, md, mdCss, mdClass)))
|
||||||
|
|
||||||
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(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) http.HandlerFunc {
|
||||||
return gzip(endpoints(ro, withDel(ro, withMD(d, md, mdCss, mdClass, fserve(d)))))
|
return httpsOnly(https, gzip(basicAuth(userPass, endpoints(ro, withDel(ro, withMD(d, md, mdCss, mdClass, fserve(d)))))))
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeMeta(w http.ResponseWriter) {
|
func writeMeta(w http.ResponseWriter) {
|
||||||
@@ -120,6 +124,32 @@ func writeForm(w http.ResponseWriter) {
|
|||||||
`, ENDPOINT_UPLOAD)
|
`, ENDPOINT_UPLOAD)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func basicAuth(userPass string, foo http.HandlerFunc) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if userPass != "" {
|
||||||
|
u, p, ok := r.BasicAuth()
|
||||||
|
if !ok || u+":"+p != userPass {
|
||||||
|
w.Header().Set("WWW-Authenticate", "Basic")
|
||||||
|
w.WriteHeader(401)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
foo(w, r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func httpsOnly(https bool, foo http.HandlerFunc) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if https && r.URL.Scheme != "https" {
|
||||||
|
log.Printf("redirecting: %+v", r.URL)
|
||||||
|
r.URL.Scheme = "https"
|
||||||
|
http.Redirect(w, r, r.URL.String(), http.StatusSeeOther)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
foo(w, r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func gzip(foo http.HandlerFunc) http.HandlerFunc {
|
func gzip(foo http.HandlerFunc) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
if gziphttp.Can(r) {
|
if gziphttp.Can(r) {
|
||||||
@@ -208,7 +238,7 @@ func withDel(ro bool, foo http.HandlerFunc) http.HandlerFunc {
|
|||||||
foo(w, r)
|
foo(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Fprintln(w, `<a href=".."><input type="button" style="padding: .15em 4em .35em 4em" value=".."/></a>`+"\n")
|
fmt.Fprintln(w, `<a href=".."><input type="button" style="padding: .15em 4em .35em 4em" value=".."/></a>`)
|
||||||
w2 := httptest.NewRecorder()
|
w2 := httptest.NewRecorder()
|
||||||
foo(w2, r)
|
foo(w2, r)
|
||||||
b := bytes.Split(w2.Body.Bytes(), []byte("\n"))
|
b := bytes.Split(w2.Body.Bytes(), []byte("\n"))
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ func SetContentTypeIfMedia(w http.ResponseWriter, r *http.Request) {
|
|||||||
v = "image/jpeg"
|
v = "image/jpeg"
|
||||||
case ".md":
|
case ".md":
|
||||||
v = "text/markdown"
|
v = "text/markdown"
|
||||||
|
v = "text/html"
|
||||||
case ".gif":
|
case ".gif":
|
||||||
v = "image/gif"
|
v = "image/gif"
|
||||||
case ".png":
|
case ".png":
|
||||||
|
|||||||
Reference in New Issue
Block a user