~kota/granola

39f55230c32ef08d95c1fc69cb7ecc7c62221cc4 — Dakota Walsh 3 months ago 92edfc4
Add info and error loggers

This is done by creating "granola" type which we can use for any
information we need on all our handlers. This is generally the preferred
idiom over using globals in go.
2 files changed, 37 insertions(+), 16 deletions(-)

M cmd/granola/handlers.go
M cmd/granola/main.go
M cmd/granola/handlers.go => cmd/granola/handlers.go +9 -8
@@ 1,12 1,11 @@
package main

import (
	"log"
	"net/http"
	"text/template"
)

func home(w http.ResponseWriter, r *http.Request) {
func (g *granola) home(w http.ResponseWriter, r *http.Request) {
	if r.URL.Path != "/" {
		http.NotFound(w, r)
		return


@@ 21,18 20,20 @@ func home(w http.ResponseWriter, r *http.Request) {

	ts, err := template.ParseFiles(files...)
	if err != nil {
		log.Println(err)
		g.errLog.Println(err)
		http.Error(w, "Internal Server Error", http.StatusInternalServerError)
		return
	}

	err = ts.ExecuteTemplate(w, "base", nil)
	if err != nil {
		log.Println(err)
		g.errLog.Println(err)
		http.Error(w, "Internal Server Error", http.StatusInternalServerError)
		return
	}
}

func newUser(w http.ResponseWriter, r *http.Request) {
func (g *granola) newUser(w http.ResponseWriter, r *http.Request) {
	if r.Method != "POST" {
		w.Header().Set("Allow", "POST")
		http.Error(w, "Method Not Allowed", 405)


@@ 42,7 43,7 @@ func newUser(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("Create a new user..."))
}

func newBook(w http.ResponseWriter, r *http.Request) {
func (g *granola) newBook(w http.ResponseWriter, r *http.Request) {
	if r.Method != "POST" {
		w.Header().Set("Allow", "POST")
		http.Error(w, "Method Not Allowed", 405)


@@ 52,7 53,7 @@ func newBook(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("Create a new book..."))
}

func newClub(w http.ResponseWriter, r *http.Request) {
func (g *granola) newClub(w http.ResponseWriter, r *http.Request) {
	if r.Method != "POST" {
		w.Header().Set("Allow", "POST")
		http.Error(w, "Method Not Allowed", 405)


@@ 62,7 63,7 @@ func newClub(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("Create a new club..."))
}

func newPost(w http.ResponseWriter, r *http.Request) {
func (g *granola) newPost(w http.ResponseWriter, r *http.Request) {
	if r.Method != "POST" {
		w.Header().Set("Allow", "POST")
		http.Error(w, "Method Not Allowed", 405)

M cmd/granola/main.go => cmd/granola/main.go +28 -8
@@ 4,24 4,44 @@ import (
	"flag"
	"log"
	"net/http"
	"os"
)

type granola struct {
	errLog  *log.Logger
	infoLog *log.Logger
}

func main() {
	addr := flag.String("addr", ":4000", "HTTP network address")
	flag.Parse()

	infoLog := log.New(os.Stdout, "INFO ", log.Ldate|log.Ltime)
	errLog := log.New(os.Stderr, "ERROR ", log.Ldate|log.Ltime|log.Lshortfile)

	g := &granola{
		errLog:  errLog,
		infoLog: infoLog,
	}

	mux := http.NewServeMux()

	fileServer := http.FileServer(http.Dir("./ui/static"))
	mux.Handle("/static/", http.StripPrefix("/static", fileServer))

	mux.HandleFunc("/", home)
	mux.HandleFunc("/new/user", newUser)
	mux.HandleFunc("/new/book", newBook)
	mux.HandleFunc("/new/club", newClub)
	mux.HandleFunc("/new/post", newPost)
	mux.HandleFunc("/", g.home)
	mux.HandleFunc("/new/user", g.newUser)
	mux.HandleFunc("/new/book", g.newBook)
	mux.HandleFunc("/new/club", g.newClub)
	mux.HandleFunc("/new/post", g.newPost)

	srv := &http.Server{
		Addr:     *addr,
		ErrorLog: errLog,
		Handler:  mux,
	}

	log.Printf("Starting server on %s", *addr)
	err := http.ListenAndServe(*addr, mux)
	log.Fatal(err)
	infoLog.Printf("Starting server on %s", *addr)
	err := srv.ListenAndServe()
	errLog.Fatal(err)
}