Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Adding support for more file types #5

Merged
merged 1 commit into from

1 participant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 11, 2014
This page is out of date. Refresh to see the latest.
Showing with 52 additions and 14 deletions.
  1. +52 −14 main.go
View
66 main.go
@@ -7,11 +7,14 @@ import (
"log"
"net/http"
"os"
+ "strings"
"github.com/russross/blackfriday"
)
func main() {
+ // Makrdown suffix suffixes
+ mdSuffixes := []string{".markdown", ".mdown", ".mkdn", ".md", ".mkd", ".mdwn", ".mdtxt", ".mdtext", ".text"}
extensions := 0
extensions |= blackfriday.EXTENSION_NO_INTRA_EMPHASIS
extensions |= blackfriday.EXTENSION_TABLES
@@ -23,9 +26,9 @@ func main() {
var port int
flag.IntVar(&port, "port", 7070, "Port number for http server")
flag.Parse()
- var file = flag.Arg(0)
- if file == "" {
- file = "README.md"
+ var defaultFile = flag.Arg(0)
+ if defaultFile == "" {
+ defaultFile = "README.md"
}
htmlFlags := 0
htmlFlags |= blackfriday.HTML_USE_XHTML
@@ -35,20 +38,55 @@ func main() {
htmlFlags |= blackfriday.HTML_TOC
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
- input, err := os.Open(file) // For read access.
- if err != nil {
- log.Fatal(err)
+ urlPath := r.URL.Path[1:]
+ var path string
+ _, err := os.Stat(urlPath)
+ if os.IsNotExist(err) {
+ _, errDefault := os.Stat(defaultFile)
+ if os.IsNotExist(errDefault) {
+ _, errIndex := os.Stat("index.html")
+ if os.IsNotExist(errIndex) {
+ http.Error(w, fmt.Sprintf("Failed to find file to read tried README.md and index.html .... %s", path), http.StatusInternalServerError)
+ return
+ } else {
+ path = "index.html"
+ }
+ } else {
+ path = defaultFile
+ }
+ } else {
+ path = urlPath
}
- b, err := ioutil.ReadAll(input)
- if err != nil {
- http.Error(w, fmt.Sprintf("Failed to read file %s", file), http.StatusInternalServerError)
+
+ md := false
+ for _, s := range mdSuffixes {
+ if strings.HasSuffix(path, s) {
+ md = true
+ break
+ }
+ }
+ if md {
+ input, err := os.Open(path) // For read access.
+ if err != nil {
+ log.Fatal(err)
+ }
+ b, err := ioutil.ReadAll(input)
+ if err != nil {
+ http.Error(w, fmt.Sprintf("Failed to read file %s", path), http.StatusInternalServerError)
+ return
+ }
+ w.Header().Set("Content-Type", "text/html; charset=utf-8")
+ w.WriteHeader(http.StatusOK)
+ renderer := blackfriday.HtmlRenderer(htmlFlags, "mdserve Markdown http serve", "")
+ output := blackfriday.Markdown(b, renderer, extensions)
+ w.Write(output)
return
+ } else if strings.HasSuffix(r.URL.Path, ".json") {
+ w.Header().Set("Content-Type", "application/json; charset=utf-8")
+ } else if strings.HasSuffix(r.URL.Path, ".html") {
+ w.Header().Set("Content-Type", "text/html; charset=utf-8")
}
- w.WriteHeader(http.StatusOK)
- w.Header().Set("Content-Type", "text/html; charset=utf-8")
- renderer := blackfriday.HtmlRenderer(htmlFlags, "mdserve Markdown http serve", "")
- output := blackfriday.Markdown(b, renderer, extensions)
- w.Write(output)
+ http.ServeFile(w, r, path)
})
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
Something went wrong with that request. Please try again.