-
-
Save adamprocter/901add74452efd6e4ebd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"html/template" | |
"net/http" | |
"path/filepath" | |
"strings" | |
"io/ioutil" | |
"github.com/russross/blackfriday" | |
) | |
type Post struct { | |
Title string | |
Date string | |
Summary string | |
Body template.HTML | |
File string | |
} | |
func handlerequest(w http.ResponseWriter, r *http.Request) { | |
if (r.URL.Path[1:] == "") { | |
posts := getPosts() | |
t := template.New("index.html") | |
t, _ = t.ParseFiles("index.html") | |
t.Execute(w, posts) | |
} else { | |
f := "posts/" + r.URL.Path[1:] + ".md" | |
fileread, _ := ioutil.ReadFile(f) | |
lines := strings.Split(string(fileread), "\n") | |
title := string(lines[0]) | |
date := string(lines[1]) | |
summary := string(lines[2]) | |
body := strings.Join(lines[3:len(lines)], "\n") | |
htmlBody := template.HTML(blackfriday.MarkdownCommon([]byte(body))) | |
post := Post{title, date, summary, htmlBody, r.URL.Path[1:]} | |
t := template.New("post.html") | |
t, _ = t.ParseFiles("post.html") | |
t.Execute(w, post) | |
} | |
} | |
func getPosts() []Post{ | |
a := []Post{} | |
files, _ := filepath.Glob("posts/*") | |
for _, f := range files { | |
file := strings.Replace(f, "posts/", "", -1) | |
file = strings.Replace(file, ".md", "", -1) | |
fileread, _ := ioutil.ReadFile(f) | |
lines := strings.Split(string(fileread), "\n") | |
title := string(lines[0]) | |
date := string(lines[1]) | |
summary := string(lines[2]) | |
body := strings.Join(lines[3:len(lines)], "\n") | |
htmlBody := template.HTML(blackfriday.MarkdownCommon([]byte(body))) | |
a = append(a, Post{title, date, summary, htmlBody, file}) | |
} | |
return a | |
} | |
func main() { | |
http.HandleFunc("/", handlerequest) | |
http.ListenAndServe(":8000", nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment