chapter 3 done

main
Nathan Wagner 1 year ago
parent 9cdbca7dc1
commit 5181aabd14

@ -3,12 +3,11 @@ package main
import ( import (
"fmt" "fmt"
"html/template" "html/template"
"log"
"net/http" "net/http"
"strconv" "strconv"
) )
func home(w http.ResponseWriter, r *http.Request) { func (app *application) home(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" { if r.URL.Path != "/" {
http.NotFound(w, r) http.NotFound(w, r)
return return
@ -20,29 +19,27 @@ func home(w http.ResponseWriter, r *http.Request) {
} }
ts, err := template.ParseFiles(files...) ts, err := template.ParseFiles(files...)
if err != nil { if err != nil {
log.Print(err.Error()) app.serverError(w, err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return return
} }
err = ts.ExecuteTemplate(w, "base", nil) err = ts.ExecuteTemplate(w, "base", nil)
if err != nil { if err != nil {
log.Print(err.Error()) app.serverError(w, err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
} }
} }
func snippetView(w http.ResponseWriter, r *http.Request) { func (app *application) snippetView(w http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(r.URL.Query().Get("id")) id, err := strconv.Atoi(r.URL.Query().Get("id"))
if err != nil || id < 1 { if err != nil || id < 1 {
http.NotFound(w, r) app.notFound(w)
return return
} }
fmt.Fprintf(w, "Display a specific snippet with ID %d...", id) fmt.Fprintf(w, "Display a specific snippet with ID %d...", id)
} }
func snippetCreate(w http.ResponseWriter, r *http.Request) { func (app *application) snippetCreate(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" { if r.Method != "POST" {
w.Header().Set("Allow", http.MethodPost) w.Header().Set("Allow", http.MethodPost)
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed) app.clientError(w, http.StatusMethodNotAllowed)
return return
} }
w.Write([]byte("Create a new snippet")) w.Write([]byte("Create a new snippet"))

@ -0,0 +1,21 @@
package main
import (
"fmt"
"net/http"
"runtime/debug"
)
func (app *application) serverError(w http.ResponseWriter, err error) {
trace := fmt.Sprintf("%s\n%s", err.Error(), debug.Stack())
app.errorLog.Output(2, trace)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
func (app *application) clientError(w http.ResponseWriter, status int) {
http.Error(w, http.StatusText(status), status)
}
func (app *application) notFound(w http.ResponseWriter) {
app.clientError(w, http.StatusNotFound)
}

@ -7,21 +7,25 @@ import (
"os" "os"
) )
type application struct {
errorLog *log.Logger
infoLog *log.Logger
}
func main() { func main() {
addr := flag.String("addr", ":4000", "HTTP network address") addr := flag.String("addr", ":4000", "HTTP network address")
flag.Parse() flag.Parse()
infoLog := log.New(os.Stdout, "INFO\t", log.Ldate|log.Ltime) infoLog := log.New(os.Stdout, "INFO\t", log.Ldate|log.Ltime)
errorLog := log.New(os.Stderr, "ERROR\t", log.Ldate|log.Ltime|log.Lshortfile) errorLog := log.New(os.Stderr, "ERROR\t", log.Ldate|log.Ltime|log.Lshortfile)
mux := http.NewServeMux() app := &application{
fileServer := http.FileServer(http.Dir("./ui/static/")) errorLog: errorLog,
mux.Handle("/static/", http.StripPrefix("/static", fileServer)) infoLog: infoLog,
mux.HandleFunc("/", home) }
mux.HandleFunc("/snippet/view", snippetView)
mux.HandleFunc("/snippet/create", snippetCreate)
srv := &http.Server{ srv := &http.Server{
Addr: *addr, Addr: *addr,
ErrorLog: errorLog, ErrorLog: errorLog,
Handler: mux, Handler: app.routes(),
} }
infoLog.Printf("Starting server on %s", *addr) infoLog.Printf("Starting server on %s", *addr)
err := srv.ListenAndServe() err := srv.ListenAndServe()

@ -0,0 +1,13 @@
package main
import "net/http"
func (app *application) routes() *http.ServerMux {
mux := http.NewServeMux()
fileServer := http.FileServer(http.Dir("./ui/static/"))
mux.Handle("/static", http.StripPrefix("/static", fileServer))
mux.HandleFunc("/", app.home)
mux.HandleFunc("/snippet/view", app.snippetView)
mux.HandleFunc("/snippet/create", app.snippetCreate)
return mux
}
Loading…
Cancel
Save