diff options
author | Matthew Lemon <y@yulqen.org> | 2024-04-23 11:16:38 +0100 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2024-04-23 11:16:38 +0100 |
commit | 0f951dcf029d4af284467543a3afdf5bf6581a20 (patch) | |
tree | a48384210cdc168e3bd3ccff6d6d516eeed9e748 /cmd/web/helpers.go | |
parent | 8b084e9fe7a5f3a04c32daf9a24f7f2cf67300f9 (diff) |
switched to Django
Diffstat (limited to '')
-rw-r--r-- | cmd/web/helpers.go | 51 |
1 files changed, 0 insertions, 51 deletions
diff --git a/cmd/web/helpers.go b/cmd/web/helpers.go deleted file mode 100644 index 882f506..0000000 --- a/cmd/web/helpers.go +++ /dev/null @@ -1,51 +0,0 @@ -package main - -import ( - "fmt" - "net/http" - "runtime/debug" -) - -func (app *application) serverError(w http.ResponseWriter, r *http.Request, err error) { - var ( - method = r.Method - uri = r.URL.RequestURI() - trace = string(debug.Stack()) - ) - - app.logger.Printf(err.Error(), method, uri, trace) - app.logger.Printf("Crash! %s %s %s", method, uri, trace) - http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) -} - -// The clientError helper sends a specific status code and corresponding description -// to the user. We'll use this later in the book to send responses like 400 "Bad -// Request" when there's a problem with the request that the user sent. -func (app *application) clientError(w http.ResponseWriter, status int) { - http.Error(w, http.StatusText(status), status) -} - -// notFound helper is a convenience wrapper around clientError which sends a 404 Not Found response -func (app *application) notFound(w http.ResponseWriter) { - app.clientError(w, http.StatusNotFound) -} - -func (app *application) render(w http.ResponseWriter, r *http.Request, status int, page string, data templateData) { - // retrieve the appropriate template set from the cache based on the page name. - // If no entry exists in the cache with the provided name, create a new error - // and call serverError() helper that we made earlier and return. - ts, ok := app.templateCache[page] - if !ok { - err := fmt.Errorf("the template %s does not exist", page) - app.serverError(w, r, err) - return - } - - // Write out the provided HTTP status code('200 OK', '400 Bad Request', etc). - w.WriteHeader(status) - - err := ts.ExecuteTemplate(w, "base", data) - if err != nil { - app.serverError(w, r, err) - } -} |