Skip to content

Commit

Permalink
Use ClientError and ServerError Logs
Browse files Browse the repository at this point in the history
  • Loading branch information
abneed committed May 23, 2022
1 parent 588015c commit fa15335
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
25 changes: 6 additions & 19 deletions internal/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package handlers
import (
"encoding/json"
"fmt"
"log"
"net/http"

"github.com/abneed/bookings/helpers"
"github.com/abneed/bookings/internal/config"
"github.com/abneed/bookings/internal/forms"
"github.com/abneed/bookings/internal/models"
Expand Down Expand Up @@ -34,25 +34,13 @@ func NewHandlers(r *Repository) {

// Home is the home page handler
func (m *Repository) Home(w http.ResponseWriter, r *http.Request) {
remoteIP := r.RemoteAddr
m.App.Session.Put(r.Context(), "remote_ip", remoteIP)

render.RenderTemplate(w, r, "home.page.html", &models.TemplateData{})
}

// About is the about page handler
func (m *Repository) About(w http.ResponseWriter, r *http.Request) {
// perform some logic
stringMap := make(map[string]string)
stringMap["test"] = "Hello, again."

remoteIP := m.App.Session.GetString(r.Context(), "remote_ip")
stringMap["remote_ip"] = remoteIP

// send the data to the template
render.RenderTemplate(w, r, "about.page.html", &models.TemplateData{
StringMap: stringMap,
})
render.RenderTemplate(w, r, "about.page.html", &models.TemplateData{})
}

// Reservation renders the make a reservation page and displays form
Expand All @@ -71,7 +59,7 @@ func (m *Repository) Reservation(w http.ResponseWriter, r *http.Request) {
func (m *Repository) PostReservation(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
log.Println(err)
helpers.ServerError(w, err)
return
}

Expand Down Expand Up @@ -141,11 +129,10 @@ func (m *Repository) AvailabilityJSON(w http.ResponseWriter, r *http.Request) {

out, err := json.MarshalIndent(resp, "", " ")
if err != nil {
log.Println(err)
helpers.ServerError(w, err)
return
}

log.Println(string(out))

w.Header().Set("Content-Type", "application/json")
w.Write(out)
}
Expand All @@ -158,7 +145,7 @@ func (m *Repository) Contact(w http.ResponseWriter, r *http.Request) {
func (m *Repository) ReservationSummary(w http.ResponseWriter, r *http.Request) {
reservation, ok := m.App.Session.Get(r.Context(), "reservation").(models.Reservation)
if !ok {
log.Println("cannot get item from session")
m.App.ErrorLog.Println("Can't get error from session")
m.App.Session.Put(r.Context(), "error", "Can't get reservation from session")
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
return
Expand Down
7 changes: 7 additions & 0 deletions internal/handlers/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"html/template"
"log"
"net/http"
"os"
"path/filepath"
"time"

Expand All @@ -29,6 +30,12 @@ func getRoutes() http.Handler {
// change this to true when in production
app.InProduction = false

infoLog := log.New(os.Stdout, "INFO\t", log.Ldate|log.Ltime)
app.InfoLog = infoLog

errorLog := log.New(os.Stdout, "ERROR\t", log.Ldate|log.Lshortfile)
app.ErrorLog = errorLog

session = scs.New()
session.Lifetime = 24 * time.Hour
session.Cookie.Persist = true
Expand Down
7 changes: 7 additions & 0 deletions internal/render/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package render

import (
"encoding/gob"
"log"
"net/http"
"os"
"testing"
Expand All @@ -22,6 +23,12 @@ func TestMain(m *testing.M) {
// change this to true when in production
testApp.InProduction = false

infoLog := log.New(os.Stdout, "INFO\t", log.Ldate|log.Ltime)
testApp.InfoLog = infoLog

errorLog := log.New(os.Stdout, "ERROR\t", log.Ldate|log.Lshortfile)
testApp.ErrorLog = errorLog

session = scs.New()
session.Lifetime = 24 * time.Hour
session.Cookie.Persist = true
Expand Down

0 comments on commit fa15335

Please sign in to comment.