backend: use a discrete ServeMux

This commit is contained in:
Cadey Ratio 2017-03-29 09:52:27 -07:00
parent 6022600110
commit 1b92034f41
1 changed files with 14 additions and 12 deletions

View File

@ -102,9 +102,11 @@ func init() {
} }
func main() { func main() {
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {}) mux := http.NewServeMux()
http.HandleFunc("/api/blog/posts", writeBlogPosts)
http.HandleFunc("/api/blog/post", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {})
mux.HandleFunc("/api/blog/posts", writeBlogPosts)
mux.HandleFunc("/api/blog/post", func(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query() q := r.URL.Query()
name := q.Get("name") name := q.Get("name")
@ -122,7 +124,7 @@ func main() {
fail: fail:
http.Error(w, "Not Found", http.StatusNotFound) http.Error(w, "Not Found", http.StatusNotFound)
}) })
http.HandleFunc("/api/resume", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/resume", func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(struct { json.NewEncoder(w).Encode(struct {
Body string `json:"body"` Body string `json:"body"`
}{ }{
@ -141,28 +143,28 @@ func main() {
log.Fatal("frontend: ", err) log.Fatal("frontend: ", err)
} }
http.Handle("/dist/", http.FileServer(fe)) mux.Handle("/dist/", http.FileServer(fe))
} else { } else {
log.Println("serving site frontend from filesystem") log.Println("serving site frontend from filesystem")
http.Handle("/dist/", http.FileServer(http.Dir("./frontend/static/"))) mux.Handle("/dist/", http.FileServer(http.Dir("./frontend/static/")))
} }
http.Handle("/static/", http.FileServer(http.Dir("."))) mux.Handle("/static/", http.FileServer(http.Dir(".")))
http.HandleFunc("/", writeIndexHTML) mux.HandleFunc("/", writeIndexHTML)
port := os.Getenv("PORT") port := os.Getenv("PORT")
if port == "" { if port == "" {
port = "9090" port = "9090"
} }
http.HandleFunc("/blog.rss", createFeed) mux.HandleFunc("/blog.rss", createFeed)
http.HandleFunc("/blog.atom", createAtom) mux.HandleFunc("/blog.atom", createAtom)
http.HandleFunc("/keybase.txt", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/keybase.txt", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./static/keybase.txt") http.ServeFile(w, r, "./static/keybase.txt")
}) })
n := negroni.Classic() n := negroni.Classic()
n.UseHandler(http.DefaultServeMux) n.UseHandler(mux)
log.Fatal(http.ListenAndServe(":"+port, n)) log.Fatal(http.ListenAndServe(":"+port, n))
} }