2024-07-01 20:34:16 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/gob"
|
|
|
|
"fmt"
|
|
|
|
"go-udemy-web-1/internal/config"
|
|
|
|
"go-udemy-web-1/internal/models"
|
|
|
|
"go-udemy-web-1/internal/render"
|
|
|
|
"html/template"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2024-07-03 08:03:25 +00:00
|
|
|
"os"
|
2024-07-01 20:34:16 +00:00
|
|
|
"path/filepath"
|
2024-07-13 14:35:17 +00:00
|
|
|
"testing"
|
2024-07-01 20:34:16 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/alexedwards/scs/v2"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/go-chi/chi/v5/middleware"
|
|
|
|
)
|
|
|
|
|
2024-07-28 13:48:03 +00:00
|
|
|
var functions = template.FuncMap{
|
|
|
|
"humanDate": render.HumanDate,
|
|
|
|
"formatDate": render.FormatDate,
|
|
|
|
"iterate": render.Iterate,
|
|
|
|
"add": render.Add,
|
|
|
|
}
|
2024-07-01 20:34:16 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
app config.AppConfig
|
|
|
|
session *scs.SessionManager
|
|
|
|
)
|
|
|
|
|
2024-07-13 14:35:17 +00:00
|
|
|
func TestMain(m *testing.M) {
|
2024-07-01 20:34:16 +00:00
|
|
|
gob.Register(models.Reservation{})
|
2024-07-28 13:48:03 +00:00
|
|
|
gob.Register(models.User{})
|
|
|
|
gob.Register(models.Room{})
|
|
|
|
gob.Register(models.Restriction{})
|
|
|
|
gob.Register(map[string]int{})
|
2024-07-01 20:34:16 +00:00
|
|
|
// change this to true when in production
|
|
|
|
app.InProduction = false
|
|
|
|
|
|
|
|
session = scs.New()
|
|
|
|
session.Lifetime = 24 * time.Hour
|
|
|
|
session.Cookie.Persist = true
|
|
|
|
session.Cookie.SameSite = http.SameSiteLaxMode
|
|
|
|
session.Cookie.Secure = app.InProduction
|
|
|
|
|
|
|
|
app.Session = session
|
|
|
|
|
2024-07-17 20:37:56 +00:00
|
|
|
mailChan := make(chan models.MailData)
|
|
|
|
app.MailChan = mailChan
|
|
|
|
defer close(mailChan)
|
|
|
|
|
|
|
|
listenForMail()
|
|
|
|
|
2024-07-01 20:34:16 +00:00
|
|
|
tc, err := CreateTestTemplateCache()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("cannot create template cache: %s", err)
|
|
|
|
}
|
|
|
|
app.TemplateCahce = tc
|
|
|
|
app.UseCache = true // Not to use ./templates
|
|
|
|
|
2024-07-03 08:03:25 +00:00
|
|
|
infoLog := log.New(os.Stdout, "INFO\t", log.Ldate|log.Ltime)
|
|
|
|
app.InfoLog = infoLog
|
|
|
|
|
|
|
|
errorLog := log.New(os.Stdout, "ERROR\t", log.Ldate|log.Ltime|log.Lshortfile)
|
|
|
|
app.ErrorLog = errorLog
|
|
|
|
|
2024-07-12 20:43:16 +00:00
|
|
|
repo := NewTestRepo(&app)
|
2024-07-01 20:34:16 +00:00
|
|
|
NewHandlers(repo)
|
|
|
|
|
2024-07-07 20:37:48 +00:00
|
|
|
render.NewRenderer(&app)
|
2024-07-01 20:34:16 +00:00
|
|
|
|
2024-07-13 14:35:17 +00:00
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
|
|
|
|
|
|
|
func getRoutes() http.Handler {
|
2024-07-01 20:34:16 +00:00
|
|
|
mux := chi.NewMux()
|
|
|
|
|
|
|
|
mux.Use(middleware.Recoverer)
|
2024-07-14 09:49:42 +00:00
|
|
|
mux.Use(WriteToConsole)
|
2024-07-01 20:34:16 +00:00
|
|
|
mux.Use(SessionLoad)
|
|
|
|
|
|
|
|
mux.Get("/", Repo.Home)
|
|
|
|
mux.Get("/about", Repo.About)
|
|
|
|
mux.Get("/contact", Repo.Contact)
|
|
|
|
mux.Get("/generals-quarters", Repo.Generals)
|
|
|
|
mux.Get("/majors-suite", Repo.Majors)
|
|
|
|
mux.Get("/availability", Repo.Availability)
|
|
|
|
mux.Post("/availability", Repo.PostAvailability)
|
|
|
|
mux.Post("/availability-json", Repo.AvailabilityJSON)
|
|
|
|
mux.Get("/make-reservation", Repo.MakeReservation)
|
|
|
|
mux.Post("/make-reservation", Repo.PostMakeReservation)
|
|
|
|
mux.Get("/reservation-summary", Repo.ReservationSummary)
|
|
|
|
|
2024-07-28 13:48:03 +00:00
|
|
|
mux.Get("/choose-room/{id}", Repo.ChooseRoom)
|
|
|
|
mux.Get("/book-room", Repo.BookRoom)
|
|
|
|
|
|
|
|
mux.Get("/user/login", Repo.ShowLogin)
|
|
|
|
mux.Post("/user/login", Repo.PostShowLogin)
|
|
|
|
mux.Get("/user/logout", Repo.Logout)
|
|
|
|
|
|
|
|
mux.Get("/admin/dashboard", Repo.AdminDashboard)
|
|
|
|
|
|
|
|
mux.Get("/admin/reservations-new", Repo.AdminNewReservations)
|
|
|
|
mux.Get("/admin/reservations-all", Repo.AdminAllReservations)
|
|
|
|
mux.Get("/admin/reservations-calendar", Repo.AdminReservationsCalendar)
|
|
|
|
mux.Post("/admin/reservations-calendar", Repo.AdminPostReservationsCalendar)
|
|
|
|
mux.Get("/admin/process-reservation/{src}/{id}/do", Repo.AdminProcessReservation)
|
|
|
|
mux.Get("/admin/delete-reservation/{src}/{id}/do", Repo.AdminDeleteReservation)
|
|
|
|
|
|
|
|
mux.Get("/admin/reservations/{src}/{id}/show", Repo.AdminShowReservation)
|
|
|
|
mux.Post("/admin/reservations/{src}/{id}", Repo.AdminPostShowReservation)
|
|
|
|
|
2024-07-01 20:34:16 +00:00
|
|
|
fileServer := http.FileServer(http.Dir("./static/"))
|
|
|
|
mux.Handle("/static/*", http.StripPrefix("/static", fileServer))
|
|
|
|
|
|
|
|
return mux
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteToConsole writes a log when user hits a page
|
|
|
|
func WriteToConsole(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fmt.Printf("Hit the page %s\n", r.URL.String())
|
|
|
|
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// SessionLoad loads and saves the session on every request
|
|
|
|
func SessionLoad(next http.Handler) http.Handler {
|
|
|
|
return session.LoadAndSave(next)
|
|
|
|
}
|
|
|
|
|
|
|
|
var pathToTemplates = "../../templates"
|
|
|
|
|
|
|
|
func CreateTestTemplateCache() (map[string]*template.Template, error) {
|
|
|
|
myCache := map[string]*template.Template{}
|
|
|
|
|
|
|
|
// get all of the files named *.page.tmpl from templates
|
|
|
|
pages, err := filepath.Glob(fmt.Sprintf("%s/*.page.tmpl", pathToTemplates))
|
|
|
|
if err != nil {
|
|
|
|
return myCache, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// range through all files ending with *page.tmpl
|
|
|
|
for _, page := range pages {
|
|
|
|
name := filepath.Base(page)
|
|
|
|
ts, err := template.New(name).Funcs(functions).ParseFiles(page)
|
|
|
|
if err != nil {
|
|
|
|
return myCache, err
|
|
|
|
}
|
|
|
|
|
|
|
|
matches, err := filepath.Glob(fmt.Sprintf("%s/*.layout.tmpl", pathToTemplates))
|
|
|
|
if err != nil {
|
|
|
|
return myCache, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(matches) > 0 {
|
|
|
|
ts, err = ts.ParseGlob(fmt.Sprintf("%s/*.layout.tmpl", pathToTemplates))
|
|
|
|
if err != nil {
|
|
|
|
return myCache, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
myCache[name] = ts
|
|
|
|
}
|
|
|
|
|
|
|
|
return myCache, nil
|
|
|
|
}
|
2024-07-17 20:37:56 +00:00
|
|
|
|
|
|
|
func listenForMail() {
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
<-app.MailChan
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|