From 4632042ba6024f21d84a9bc2d0f4669a5847a887 Mon Sep 17 00:00:00 2001 From: Kelvin Ly Date: Sat, 22 Sep 2018 08:31:23 -0400 Subject: [PATCH] Initial working server --- .gitignore | 1 + .gitmodules | 3 ++ main.css | 5 ++++ main.go | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++ static | 1 + 5 files changed, 91 insertions(+) create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 main.css create mode 100644 main.go create mode 160000 static diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5b301f7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +kelvinly-server diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..fef61cf --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "static"] + path = static + url = https://github.com/cactorium/cactorium.github.io diff --git a/main.css b/main.css new file mode 100644 index 0000000..d36db28 --- /dev/null +++ b/main.css @@ -0,0 +1,5 @@ +body { + max-width: 1012px; + margin-right: auto; + margin-left: auto; +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..bd463b8 --- /dev/null +++ b/main.go @@ -0,0 +1,81 @@ +package main + +import ( + "bytes" + "fmt" + "io/ioutil" + "log" + "net/http" + "strings" + + gfm "github.com/shurcooL/github_flavored_markdown" + "github.com/shurcooL/github_flavored_markdown/gfmstyle" + //blackfriday "gopkg.in/russross/blackfriday.v2" +) + +const HTML_HEADER = ` + + + + %s | %s + + + + +
+` + +const HTML_FOOTER = `
+ +` + +func serveMarkdown(w http.ResponseWriter, r *http.Request, path string) { + if b, err := ioutil.ReadFile(path); err != nil { + w.WriteHeader(404) + w.Write([]byte(fmt.Sprintf("file %s not found", path))) + return + } else { + w.Header().Add("Content-Type", "text/html; charset=utf-8") + title := "" + if s := bytes.Index(b, []byte("# ")); s != -1 { + t := b[s+2:] + if e := bytes.Index(t, []byte("\n")); e != -1 { + t = t[:e] + title = string(t) + } + } + w.Write([]byte(fmt.Sprintf(HTML_HEADER, string(title), r.Host))) + html := gfm.Markdown(b) + w.Write(html) + w.Write([]byte(HTML_FOOTER)) + } +} + +func rootHandler(w http.ResponseWriter, r *http.Request) { + if r.Method == "GET" { + if r.URL.Path == "/" { + serveMarkdown(w, r, "static/README.md") + } else if strings.HasSuffix(r.URL.Path, ".md") { + if strings.Contains(r.URL.Path, "..") { + w.WriteHeader(403) + w.Write([]byte("\"..\" forbidden in URL")) + return + } + filepath := "static" + r.URL.Path + serveMarkdown(w, r, filepath) + } else { + http.ServeFile(w, r, "static"+r.URL.Path) + } + } else { + w.Write([]byte("unimplemented!")) + } +} + +func main() { + log.Print("installing handlers") + http.HandleFunc("/", rootHandler) + http.Handle("/gfm/", http.StripPrefix("/gfm", http.FileServer(gfmstyle.Assets))) + http.HandleFunc("/main.css", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "main.css") }) + log.Print("starting server") + log.Fatal(http.ListenAndServe(":80", nil)) +} diff --git a/static b/static new file mode 160000 index 0000000..348f838 --- /dev/null +++ b/static @@ -0,0 +1 @@ +Subproject commit 348f83876a169679b345844063391eb5778ea359