Reimplement most of my site's functionality

This commit is contained in:
Cadey Ratio 2017-05-20 15:06:30 -07:00
parent 64fe5c582a
commit 78f1dda411
12 changed files with 450 additions and 0 deletions

View File

@ -0,0 +1,14 @@
package main
import (
"crypto/md5"
"fmt"
)
// Hash is a simple wrapper around the MD5 algorithm implementation in the
// Go standard library. It takes in data and a salt and returns the hashed
// representation.
func Hash(data string, salt string) string {
output := md5.Sum([]byte(data + salt))
return fmt.Sprintf("%x", output)
}

View File

@ -0,0 +1,72 @@
package main
import (
"fmt"
"html/template"
"net/http"
"time"
"github.com/Xe/ln"
)
func logTemplateTime(name string, from time.Time) {
now := time.Now()
ln.Log(ln.F{"action": "template_rendered", "dur": now.Sub(from).String()})
}
func (s *Site) renderTemplatePage(templateFname string, data interface{}) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer logTemplateTime(templateFname, time.Now())
s.tlock.RLock()
defer s.tlock.RUnlock()
var t *template.Template
var err error
if s.templates[templateFname] == nil {
t, err = template.ParseFiles("templates/base.html", "templates/"+templateFname)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
ln.Error(err, ln.F{"action": "renderTemplatePage", "page": templateFname})
fmt.Fprintf(w, "error: %v", err)
}
ln.Log(ln.F{"action": "loaded_new_template", "fname": templateFname})
s.tlock.RUnlock()
s.tlock.Lock()
s.templates[templateFname] = t
s.tlock.Unlock()
s.tlock.RLock()
} else {
t = s.templates[templateFname]
}
err = t.Execute(w, data)
if err != nil {
panic(err)
}
})
}
func (s *Site) showPost(w http.ResponseWriter, r *http.Request) {
if r.RequestURI == "/blog/" {
http.Redirect(w, r, "/blog", http.StatusSeeOther)
return
}
var p *Post
for _, pst := range s.Posts {
if pst.Link == r.RequestURI[1:] {
p = pst
}
}
if p == nil {
w.WriteHeader(http.StatusNotFound)
s.renderTemplatePage("error.html", "no such post found: "+r.RequestURI).ServeHTTP(w, r)
return
}
s.renderTemplatePage("blogpost.html", p).ServeHTTP(w, r)
}

View File

@ -0,0 +1,191 @@
package main
import (
"html/template"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"sort"
"strings"
"sync"
"time"
"github.com/Xe/jsonfeed"
"github.com/Xe/ln"
"github.com/gorilla/feeds"
"github.com/russross/blackfriday"
"github.com/tj/front"
)
var port = os.Getenv("PORT")
func main() {
if port == "" {
port = "29384"
}
s, err := Build()
if err != nil {
ln.Fatal(ln.F{"err": err, "action": "Build"})
}
http.ListenAndServe(":"+port, s)
}
// Site is the parent object for https://christine.website's backend.
type Site struct {
Posts Posts
Resume template.HTML
rssFeed *feeds.Feed
jsonFeed *jsonfeed.Feed
mux *http.ServeMux
templates map[string]*template.Template
tlock sync.RWMutex
}
func (s *Site) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ln.Log(ln.F{"action": "Site.ServeHTTP"})
s.mux.ServeHTTP(w, r)
}
// Build creates a new Site instance or fails.
func Build() (*Site, error) {
type postFM struct {
Title string
Date string
}
s := &Site{
rssFeed: &feeds.Feed{
Title: "Christine Dodrill's Blog",
Link: &feeds.Link{Href: "https://christine.website/blog"},
Description: "My blog posts and rants about various technology things.",
Author: &feeds.Author{Name: "Christine Dodrill", Email: "me@christine.website"},
Created: bootTime,
Copyright: "This work is copyright Christine Dodrill. My viewpoints are my own and not the view of any employer past, current or future.",
},
jsonFeed: &jsonfeed.Feed{
Version: jsonfeed.CurrentVersion,
Title: "Christine Dodrill's Blog",
HomePageURL: "https://christine.website",
FeedURL: "https://christine.website/blog.json",
Description: "My blog posts and rants about various technology things.",
UserComment: "This is a JSON feed of my blogposts. For more information read: https://jsonfeed.org/version/1",
Icon: icon,
Favicon: icon,
Author: jsonfeed.Author{
Name: "Christine Dodrill",
Avatar: icon,
},
},
mux: http.NewServeMux(),
templates: map[string]*template.Template{},
}
err := filepath.Walk("./blog/", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
fin, err := os.Open(path)
if err != nil {
return err
}
defer fin.Close()
content, err := ioutil.ReadAll(fin)
if err != nil {
return err
}
var fm postFM
remaining, err := front.Unmarshal(content, &fm)
if err != nil {
return err
}
output := blackfriday.MarkdownCommon(remaining)
p := &Post{
Title: fm.Title,
Date: fm.Date,
Link: strings.Split(path, ".")[0],
Body: string(remaining),
BodyHTML: template.HTML(output),
}
s.Posts = append(s.Posts, p)
return nil
})
if err != nil {
return nil, err
}
sort.Sort(sort.Reverse(s.Posts))
resume, err := ioutil.ReadFile("./static/resume/resume.md")
if err != nil {
panic(err)
}
s.Resume = template.HTML(blackfriday.MarkdownCommon(resume))
for _, item := range s.Posts {
itime, _ := time.Parse("2006-01-02", item.Date)
s.rssFeed.Items = append(s.rssFeed.Items, &feeds.Item{
Title: item.Title,
Link: &feeds.Link{Href: "https://christine.website/" + item.Link},
Description: item.Summary,
Created: itime,
})
s.jsonFeed.Items = append(s.jsonFeed.Items, jsonfeed.Item{
ID: "https://christine.website/" + item.Link,
URL: "https://christine.website/" + item.Link,
Title: item.Title,
DatePublished: itime,
})
}
// Add HTTP routes here
s.mux.Handle("/", s.renderTemplatePage("index.html", nil))
s.mux.Handle("/resume", s.renderTemplatePage("resume.html", s.Resume))
s.mux.Handle("/blog", s.renderTemplatePage("blogindex.html", s.Posts))
s.mux.HandleFunc("/blog/", s.showPost)
s.mux.Handle("/static/", http.FileServer(http.Dir(".")))
return s, nil
}
const icon = "https://christine.website/static/img/avatar.png"
// Post is a single blogpost.
type Post struct {
Title string `json:"title"`
Link string `json:"link"`
Summary string `json:"summary,omitifempty"`
Body string `json:"-"`
BodyHTML template.HTML `json:"body"`
Date string `json:"date"`
}
// Posts implements sort.Interface for a slice of Post objects.
type Posts []*Post
func (p Posts) Len() int { return len(p) }
func (p Posts) Less(i, j int) bool {
iDate, _ := time.Parse("2006-01-02", p[i].Date)
jDate, _ := time.Parse("2006-01-02", p[j].Date)
return iDate.Unix() < jDate.Unix()
}
func (p Posts) Swap(i, j int) { p[i], p[j] = p[j], p[i] }

View File

@ -0,0 +1,45 @@
package main
import (
"net/http"
"time"
"github.com/Xe/ln"
)
var bootTime = time.Now()
// IncrediblySecureSalt *******
const IncrediblySecureSalt = "hunter2"
func (s *Site) createFeed(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/rss+xml")
w.Header().Set("ETag", Hash(bootTime.String(), IncrediblySecureSalt))
err := s.rssFeed.WriteRss(w)
if err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
ln.Error(err, ln.F{
"remote_addr": r.RemoteAddr,
"action": "generating_rss",
"uri": r.RequestURI,
"host": r.Host,
})
}
}
func (s *Site) createAtom(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/atom+xml")
w.Header().Set("ETag", Hash(bootTime.String(), IncrediblySecureSalt))
err := s.rssFeed.WriteAtom(w)
if err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
ln.Error(err, ln.F{
"remote_addr": r.RemoteAddr,
"action": "generating_rss",
"uri": r.RequestURI,
"host": r.Host,
})
}
}

1
static/css/hack.css Normal file

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1 @@
.solarized-dark{background-color:#073642;color:#78909c}.solarized-dark h1,.solarized-dark h2,.solarized-dark h3,.solarized-dark h4,.solarized-dark h5,.solarized-dark h6{color:#1e88e5}.solarized-dark h1 a,.solarized-dark h2 a,.solarized-dark h3 a,.solarized-dark h4 a,.solarized-dark h5 a,.solarized-dark h6 a{color:#1e88e5;border-bottom-color:#1e88e5}.solarized-dark h1 a:hover,.solarized-dark h2 a:hover,.solarized-dark h3 a:hover,.solarized-dark h4 a:hover,.solarized-dark h5 a:hover,.solarized-dark h6 a:hover{background-color:#1e88e5;color:#fff}.solarized-dark pre{background-color:#073642;padding:0;border:none}.solarized-dark pre code{color:#009688}.solarized-dark h1 a,.solarized-dark h2 a,.solarized-dark h3 a,.solarized-dark h4 a,.solarized-dark h5 a{color:#78909c}.solarized-dark code,.solarized-dark strong{color:#90a4ae}.solarized-dark code{font-weight:100}.solarized-dark .progress-bar-filled{background-color:#558b2f}.solarized-dark .progress-bar-filled:after,.solarized-dark .progress-bar-filled:before{color:#90a4ae}.solarized-dark table{color:#78909c}.solarized-dark table td,.solarized-dark table th{border-color:#b0bec5}.solarized-dark table tbody td:first-child{color:#b0bec5}.solarized-dark .form-group label{color:#78909c;border-color:#90a4ae}.solarized-dark .form-group.form-textarea label:after{background-color:#073642}.solarized-dark .form-control{color:#78909c;border-color:#90a4ae}.solarized-dark .form-control:focus{border-color:#cfd8dc;color:#cfd8dc}.solarized-dark textarea.form-control{color:#78909c}.solarized-dark .card{border-color:#90a4ae}.solarized-dark .card .card-header{background-color:transparent;color:#78909c;border-bottom:1px solid #90a4ae}.solarized-dark .btn.btn-ghost.btn-default{border-color:#607d8b;color:#607d8b}.solarized-dark .btn.btn-ghost.btn-default:focus,.solarized-dark .btn.btn-ghost.btn-default:hover{border-color:#90a4ae;color:#90a4ae;z-index:1}.solarized-dark .btn.btn-ghost.btn-default:focus,.solarized-dark .btn.btn-ghost.btn-default:hover{border-color:#e0e0e0;color:#e0e0e0}.solarized-dark .btn.btn-ghost.btn-primary:focus,.solarized-dark .btn.btn-ghost.btn-primary:hover{border-color:#64b5f6;color:#64b5f6}.solarized-dark .btn.btn-ghost.btn-success:focus,.solarized-dark .btn.btn-ghost.btn-success:hover{border-color:#81c784;color:#81c784}.solarized-dark .btn.btn-ghost.btn-info:focus,.solarized-dark .btn.btn-ghost.btn-info:hover{border-color:#4dd0e1;color:#4dd0e1}.solarized-dark .btn.btn-ghost.btn-error:focus,.solarized-dark .btn.btn-ghost.btn-error:hover{border-color:#e57373;color:#e57373}.solarized-dark .btn.btn-ghost.btn-warning:focus,.solarized-dark .btn.btn-ghost.btn-warning:hover{border-color:#ffb74d;color:#ffb74d}.solarized-dark .avatarholder,.solarized-dark .placeholder{background-color:transparent;border-color:#90a4ae}.solarized-dark .menu .menu-item{color:#78909c;border-color:#90a4ae}.solarized-dark .menu .menu-item.active,.solarized-dark .menu .menu-item:hover{color:#fff;border-color:#78909c}

45
templates/base.html Normal file
View File

@ -0,0 +1,45 @@
<html>
<head>
{{ template "title" . }}
<link rel="stylesheet" href="/static/css/hack.css" />
<link rel="stylesheet" href="/static/css/solarized-dark.css" />
<style>
.main {
padding: 20px 10px;
}
.hack h1 {
padding-top: 0;
}
footer.footer {
border-top: 1px solid #ccc;
margin-top: 80px;
margin-top: 5rem;
padding: 48px 0;
padding: 3rem 0;
}
img {
max-width: 100%;
max-height: 100%;
}
</style>
{{ template "styles" . }}
</head>
<body class="hack solarized-dark">
{{ template "scripts" . }}
<div class="container">
<header>
<p><a href="/">Christine Dodrill</a> - <a href="/blog">Blog</a> - <a href="/contact">Contact</a> - <a href="/resume">Resume</a></p>
</header>
{{ template "content" . }}
<footer>
<blockquote>Copyright 2017 Christine Dodrill. Any and all opinions listed here are my own and not representative of my employer.</blockquote>
</footer>
</div>
</body>
</html>
{{ define "scripts" }}{{ end }}
{{ define "styles" }}{{ end }}

22
templates/blogindex.html Normal file
View File

@ -0,0 +1,22 @@
{{ define "title" }}
<title>Blog - Christine Dodrill</title>
<style>
.blogpost-card {
text-align: center;
}
</style>
{{ end }}
{{ define "content" }}
<div class="grid">
{{ range . }}
<div class="card cell -4of12 blogpost-card">
<header class="card-header">{{ .Title }}</header>
<div class="card-content">
<p>Posted on {{ .Date }} <br> <a href="{{ .Link }}">Read Post</a></p>
</div>
</div>
{{ end }}
</div>
{{ end }}

11
templates/blogpost.html Normal file
View File

@ -0,0 +1,11 @@
{{ define "title" }}
<title>{{ .Title }} - Christine Dodrill</title>
{{ end }}
{{ define "content" }}
{{ .BodyHTML }}
<hr />
<i>Content posted on {{ .Date }}, opinions and preferences of the author may have changed since then.</i>
{{ end }}

9
templates/error.html Normal file
View File

@ -0,0 +1,9 @@
{{ define "title" }}
<title>Error - Christine Dodrill</title>
{{ end }}
{{ define "content" }}
<pre>
{{ . }}
</pre>
{{ end }}

30
templates/index.html Normal file
View File

@ -0,0 +1,30 @@
{{ define "title" }}<title>Christine Dodrill</title>{{ end }}
{{ define "content" }}
<div class="grid">
<div class="cell -3of12 content">
<img src="/static/img/avatar.png" width="192px">
<br />
<a href="/contact" class="justify-content-center">Contact Me</a>
</div>
<div class="cell -9of12 content">
<h1>Christine Dodrill</h1>
<h4>Web and Backend Services Devops Specialist</h4>
<h5>Skills</h5>
<ul>
<li>Go, Lua, Nim, Haskell, C, Python (3.x) and other languages</li>
<li>Docker (deployment, development & more)</li>
<li>Mashups of data</li>
<li>Package maintainer for Alpine Linux</li>
</ul>
<h5>Highlighted Projects</h5>
<ul>
<li><a href="https://github.com/Xe/PonyAPI">PonyAPI</a> - My Little Pony: Friendship is Magic Episode information API</li>
<li><a href="https://github.com/PonyvilleFM/aura">Aura</a> - PonyvilleFM live DJ recording bot</li>
<li><a href="https://github.com/Elemental-IRCd/elemental-ircd">Elemental-IRCd</a> - IRC Server Software</li>
<li><a href="https://github.com/Xe/site">This website</a> - The backend and templates for this website</li>
</ul>
</div>
</div>
{{ end }}

9
templates/resume.html Normal file
View File

@ -0,0 +1,9 @@
{{ define "title" }}<title>Resume - Christine Dodrill</title>{{ end }}
{{ define "content" }}
{{ . }}
<hr />
<a href="/static/resume/resume.md">Plain-text version of this resume here</a>
{{ end }}