Render markdown as html by default
parent
f373524377
commit
84955c01b5
40
main.go
40
main.go
|
|
@ -1,11 +1,3 @@
|
|||
/*
|
||||
Serve is a very simple static file server in go
|
||||
Usage:
|
||||
-p="8100": port to serve on
|
||||
-d=".": the directory of static files to host
|
||||
Navigating to http://localhost:8100 will display the index.html or directory
|
||||
listing file.
|
||||
*/
|
||||
package main
|
||||
|
||||
import (
|
||||
|
|
@ -13,8 +5,10 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"local/args"
|
||||
"local/gziphttp"
|
||||
"local/notes-server/notes"
|
||||
"local/simpleserve/simpleserve"
|
||||
"log"
|
||||
"net/http"
|
||||
|
|
@ -37,23 +31,25 @@ var (
|
|||
func main() {
|
||||
fs = args.NewArgSet()
|
||||
fs.Append(args.STRING, "p", "port to serve", "8100")
|
||||
fs.Append(args.BOOL, "md", "whether to render markdown as html", true)
|
||||
fs.Append(args.STRING, "d", "static path to serve", "./public")
|
||||
if err := fs.Parse(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
d := fs.Get("d").GetString()
|
||||
md := fs.Get("md").GetBool()
|
||||
p := strings.TrimPrefix(fs.Get("p").GetString(), ":")
|
||||
|
||||
http.Handle("/", http.HandlerFunc(handler(d)))
|
||||
http.Handle("/", http.HandlerFunc(handler(d, md)))
|
||||
|
||||
log.Printf("Serving %s on HTTP port: %s\n", d, p)
|
||||
|
||||
log.Fatal(http.ListenAndServe(":"+p, nil))
|
||||
}
|
||||
|
||||
func handler(d string) http.HandlerFunc {
|
||||
return gzip(endpoints(withDel(fserve(d))))
|
||||
func handler(d string, md bool) http.HandlerFunc {
|
||||
return gzip(endpoints(withDel(withMD(d, md, fserve(d)))))
|
||||
}
|
||||
|
||||
func writeMeta(w http.ResponseWriter) {
|
||||
|
|
@ -129,6 +125,28 @@ func isDir(r *http.Request) bool {
|
|||
return true
|
||||
}
|
||||
|
||||
func withMD(dir string, md bool, foo http.HandlerFunc) http.HandlerFunc {
|
||||
n := ¬es.Notes{RO: true}
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
realpath := toRealPath(r.URL.Path)
|
||||
if md && !isDir(r) && path.Ext(realpath) == ".md" {
|
||||
b, err := ioutil.ReadFile(realpath)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
s, err := n.Gomarkdown(r.URL.Path, b)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
fmt.Fprintln(w, s)
|
||||
} else {
|
||||
foo(w, r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func withDel(foo http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if !isDir(r) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,7 @@
|
|||
# Hello
|
||||
|
||||
## World
|
||||
|
||||
For I am
|
||||
|
||||
**MARKDOWN**
|
||||
Loading…
Reference in New Issue