move to root, embed static assets

This commit is contained in:
Cadey Ratio 2017-05-20 15:40:12 -07:00
parent 78f1dda411
commit d9e24cd897
10 changed files with 213 additions and 9 deletions

9
gopreload.go Normal file
View File

@ -0,0 +1,9 @@
// gopreload.go
package main
/*
This file is separate to make it very easy to both add into an application, but
also very easy to remove.
*/
import _ "github.com/Xe/gopreload"

13
gops.go Normal file
View File

@ -0,0 +1,13 @@
package main
import (
"log"
"github.com/google/gops/agent"
)
func init() {
if err := agent.Listen(nil); err != nil {
log.Fatal(err)
}
}

View File

@ -11,7 +11,7 @@ import (
func logTemplateTime(name string, from time.Time) { func logTemplateTime(name string, from time.Time) {
now := time.Now() now := time.Now()
ln.Log(ln.F{"action": "template_rendered", "dur": now.Sub(from).String()}) ln.Log(ln.F{"action": "template_rendered", "dur": now.Sub(from).String(), "name": name})
} }
func (s *Site) renderTemplatePage(templateFname string, data interface{}) http.Handler { func (s *Site) renderTemplatePage(templateFname string, data interface{}) http.Handler {

View File

@ -11,6 +11,7 @@ import (
"sync" "sync"
"time" "time"
"github.com/GeertJohan/go.rice"
"github.com/Xe/jsonfeed" "github.com/Xe/jsonfeed"
"github.com/Xe/ln" "github.com/Xe/ln"
"github.com/gorilla/feeds" "github.com/gorilla/feeds"
@ -30,6 +31,7 @@ func main() {
ln.Fatal(ln.F{"err": err, "action": "Build"}) ln.Fatal(ln.F{"err": err, "action": "Build"})
} }
ln.Log(ln.F{"action": "http_listening", "port": port})
http.ListenAndServe(":"+port, s) http.ListenAndServe(":"+port, s)
} }
@ -48,7 +50,7 @@ type Site struct {
} }
func (s *Site) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (s *Site) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ln.Log(ln.F{"action": "Site.ServeHTTP"}) ln.Log(ln.F{"action": "Site.ServeHTTP", "user_ip_address": r.RemoteAddr, "path": r.RequestURI})
s.mux.ServeHTTP(w, r) s.mux.ServeHTTP(w, r)
} }
@ -132,12 +134,17 @@ func Build() (*Site, error) {
sort.Sort(sort.Reverse(s.Posts)) sort.Sort(sort.Reverse(s.Posts))
resume, err := ioutil.ReadFile("./static/resume/resume.md") cb, err := rice.FindBox("css")
if err != nil { if err != nil {
panic(err) return nil, err
} }
s.Resume = template.HTML(blackfriday.MarkdownCommon(resume)) sb, err := rice.FindBox("static")
if err != nil {
return nil, err
}
s.Resume = template.HTML(blackfriday.MarkdownCommon(sb.MustBytes("resume/resume.md")))
for _, item := range s.Posts { for _, item := range s.Posts {
itime, _ := time.Parse("2006-01-02", item.Date) itime, _ := time.Parse("2006-01-02", item.Date)
@ -160,8 +167,12 @@ func Build() (*Site, error) {
s.mux.Handle("/", s.renderTemplatePage("index.html", nil)) s.mux.Handle("/", s.renderTemplatePage("index.html", nil))
s.mux.Handle("/resume", s.renderTemplatePage("resume.html", s.Resume)) s.mux.Handle("/resume", s.renderTemplatePage("resume.html", s.Resume))
s.mux.Handle("/blog", s.renderTemplatePage("blogindex.html", s.Posts)) s.mux.Handle("/blog", s.renderTemplatePage("blogindex.html", s.Posts))
s.mux.HandleFunc("/blog.rss", s.createFeed)
s.mux.HandleFunc("/blog.atom", s.createAtom)
s.mux.HandleFunc("/blog.json", s.createJsonFeed)
s.mux.HandleFunc("/blog/", s.showPost) s.mux.HandleFunc("/blog/", s.showPost)
s.mux.Handle("/static/", http.FileServer(http.Dir("."))) s.mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(sb.HTTPBox())))
s.mux.Handle("/css/", http.StripPrefix("/css/", http.FileServer(cb.HTTPBox())))
return s, nil return s, nil
} }

152
rice-box.go Normal file

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"encoding/json"
"net/http" "net/http"
"time" "time"
@ -37,7 +38,25 @@ func (s *Site) createAtom(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Internal server error", http.StatusInternalServerError) http.Error(w, "Internal server error", http.StatusInternalServerError)
ln.Error(err, ln.F{ ln.Error(err, ln.F{
"remote_addr": r.RemoteAddr, "remote_addr": r.RemoteAddr,
"action": "generating_rss", "action": "generating_atom",
"uri": r.RequestURI,
"host": r.Host,
})
}
}
func (s *Site) createJsonFeed(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("ETag", Hash(bootTime.String(), IncrediblySecureSalt))
e := json.NewEncoder(w)
e.SetIndent("", "\t")
err := e.Encode(s.jsonFeed)
if err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
ln.Error(err, ln.F{
"remote_addr": r.RemoteAddr,
"action": "generating_jsonfeed",
"uri": r.RequestURI, "uri": r.RequestURI,
"host": r.Host, "host": r.Host,
}) })

View File

@ -1,8 +1,8 @@
<html> <html>
<head> <head>
{{ template "title" . }} {{ template "title" . }}
<link rel="stylesheet" href="/static/css/hack.css" /> <link rel="stylesheet" href="/css/hack.css" />
<link rel="stylesheet" href="/static/css/solarized-dark.css" /> <link rel="stylesheet" href="/css/solarized-dark.css" />
<style> <style>
.main { .main {
padding: 20px 10px; padding: 20px 10px;