Refactoring to use internal packages
This commit is contained in:
15
internal/config/config.go
Normal file
15
internal/config/config.go
Normal file
@ -0,0 +1,15 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
|
||||
"github.com/alexedwards/scs/v2"
|
||||
)
|
||||
|
||||
// AppConfig holds the application config
|
||||
type AppConfig struct {
|
||||
TemplateCahce map[string]*template.Template
|
||||
UseCache bool
|
||||
InProduction bool
|
||||
Session *scs.SessionManager
|
||||
}
|
106
internal/handlers/handlers.go
Normal file
106
internal/handlers/handlers.go
Normal file
@ -0,0 +1,106 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"go-udemy-web-1/internal/config"
|
||||
"go-udemy-web-1/internal/models"
|
||||
"go-udemy-web-1/internal/render"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Repo the repository used by the handlers
|
||||
var Repo *Repository
|
||||
|
||||
// Repository is the repository type
|
||||
type Repository struct {
|
||||
App *config.AppConfig
|
||||
}
|
||||
|
||||
// NewRepo creates a new repository
|
||||
func NewRepo(a *config.AppConfig) *Repository {
|
||||
return &Repository{
|
||||
App: a,
|
||||
}
|
||||
}
|
||||
|
||||
// NewHandlers sets the repository for the handlers
|
||||
func NewHandlers(r *Repository) {
|
||||
Repo = r
|
||||
}
|
||||
|
||||
// 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.tmpl", &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 world!"
|
||||
|
||||
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.tmpl", &models.TemplateData{StringMap: stringMap})
|
||||
}
|
||||
|
||||
// Contact is the contact page handler
|
||||
func (m *Repository) Contact(w http.ResponseWriter, r *http.Request) {
|
||||
render.RenderTemplate(w, r, "contact.page.tmpl", &models.TemplateData{})
|
||||
}
|
||||
|
||||
// Generals is the General's Quarters page handler
|
||||
func (m *Repository) Generals(w http.ResponseWriter, r *http.Request) {
|
||||
render.RenderTemplate(w, r, "generals.page.tmpl", &models.TemplateData{})
|
||||
}
|
||||
|
||||
// Majors is the Major's Suite page handler
|
||||
func (m *Repository) Majors(w http.ResponseWriter, r *http.Request) {
|
||||
render.RenderTemplate(w, r, "majors.page.tmpl", &models.TemplateData{})
|
||||
}
|
||||
|
||||
// MakeReservation is the make reservation page handler
|
||||
func (m *Repository) MakeReservation(w http.ResponseWriter, r *http.Request) {
|
||||
render.RenderTemplate(w, r, "make-reservation.page.tmpl", &models.TemplateData{})
|
||||
}
|
||||
|
||||
// Availability is the search for availability page handler
|
||||
func (m *Repository) Availability(w http.ResponseWriter, r *http.Request) {
|
||||
render.RenderTemplate(w, r, "availability.page.tmpl", &models.TemplateData{})
|
||||
}
|
||||
|
||||
// PostAvailability is the search for availability page handler
|
||||
func (m *Repository) PostAvailability(w http.ResponseWriter, r *http.Request) {
|
||||
start := r.Form.Get("start")
|
||||
end := r.Form.Get("end")
|
||||
fmt.Fprintf(w, "Posted to search availability from %s to %s", start, end)
|
||||
}
|
||||
|
||||
type responseJSON struct {
|
||||
OK string `json:"ok"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
// AvailabilityJSON is the search for availability page handler
|
||||
func (m *Repository) AvailabilityJSON(w http.ResponseWriter, r *http.Request) {
|
||||
resp := responseJSON{
|
||||
OK: "true",
|
||||
Message: "Available!",
|
||||
}
|
||||
|
||||
out, err := json.MarshalIndent(resp, "", " ")
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
w.Write(out)
|
||||
}
|
13
internal/models/templatedata.go
Normal file
13
internal/models/templatedata.go
Normal file
@ -0,0 +1,13 @@
|
||||
package models
|
||||
|
||||
// TemplateData holds data sent from handlers to templates
|
||||
type TemplateData struct {
|
||||
StringMap map[string]string
|
||||
IntMap map[string]int
|
||||
FloatMap map[string]float32
|
||||
Data map[string]interface{}
|
||||
CSRFToken string
|
||||
Flash string
|
||||
Warning string
|
||||
Error string
|
||||
}
|
95
internal/render/render.go
Normal file
95
internal/render/render.go
Normal file
@ -0,0 +1,95 @@
|
||||
package render
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"go-udemy-web-1/internal/config"
|
||||
"go-udemy-web-1/internal/models"
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/justinas/nosurf"
|
||||
)
|
||||
|
||||
var app *config.AppConfig
|
||||
|
||||
// NewTemplates sets the config for the template package
|
||||
func NewTemplates(a *config.AppConfig) {
|
||||
app = a
|
||||
}
|
||||
|
||||
// AddDefaultData adds default template data
|
||||
func AddDefaultData(td *models.TemplateData, r *http.Request) *models.TemplateData {
|
||||
td.CSRFToken = nosurf.Token(r)
|
||||
return td
|
||||
}
|
||||
|
||||
// RenderTemplate renders a HTML template file
|
||||
func RenderTemplate(w http.ResponseWriter, r *http.Request, tmpl string, td *models.TemplateData) {
|
||||
var tc map[string]*template.Template
|
||||
if app.UseCache {
|
||||
// get the template cache from the app config
|
||||
tc = app.TemplateCahce
|
||||
} else {
|
||||
tc, _ = CreateTemplateCache()
|
||||
}
|
||||
|
||||
// get requested template from cache
|
||||
t, ok := tc[tmpl]
|
||||
if !ok {
|
||||
log.Fatal("Could not get template from template cache")
|
||||
}
|
||||
|
||||
// Write to a buffer to make sure that the template can be read and
|
||||
// written successfully
|
||||
buf := new(bytes.Buffer)
|
||||
|
||||
td = AddDefaultData(td, r)
|
||||
|
||||
err := t.Execute(buf, td)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
|
||||
// render the template
|
||||
_, err = buf.WriteTo(w)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
func CreateTemplateCache() (map[string]*template.Template, error) {
|
||||
myCache := map[string]*template.Template{}
|
||||
|
||||
// get all of the files named *.page.tmpl from ./templates
|
||||
pages, err := filepath.Glob("./templates/*.page.tmpl")
|
||||
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).ParseFiles(page)
|
||||
if err != nil {
|
||||
return myCache, err
|
||||
}
|
||||
|
||||
matches, err := filepath.Glob("./templates/*.layout.tmpl")
|
||||
if err != nil {
|
||||
return myCache, err
|
||||
}
|
||||
|
||||
if len(matches) > 0 {
|
||||
ts, err = ts.ParseGlob("./templates/*.layout.tmpl")
|
||||
if err != nil {
|
||||
return myCache, err
|
||||
}
|
||||
}
|
||||
|
||||
myCache[name] = ts
|
||||
}
|
||||
|
||||
return myCache, nil
|
||||
}
|
Reference in New Issue
Block a user