Setting up routes and building a render function
This commit is contained in:
		
							
								
								
									
										7
									
								
								cmd/web/handlers.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								cmd/web/handlers.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,7 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import "net/http"
 | 
			
		||||
 | 
			
		||||
func (app *application) VirtualTerminal(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										99
									
								
								cmd/web/render.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										99
									
								
								cmd/web/render.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,99 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"embed"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"html/template"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"strings"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type templateData struct {
 | 
			
		||||
	StringMap       map[string]string
 | 
			
		||||
	IntMap          map[string]int
 | 
			
		||||
	FloatMap        map[string]int
 | 
			
		||||
	Data            map[string]interface{}
 | 
			
		||||
	CSRFToken       string
 | 
			
		||||
	Flash           string
 | 
			
		||||
	Warning         string
 | 
			
		||||
	Error           string
 | 
			
		||||
	API             string
 | 
			
		||||
	CSSVersion      string
 | 
			
		||||
	IsAuthenticated int
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var functions = template.FuncMap{}
 | 
			
		||||
 | 
			
		||||
//go:embed templates
 | 
			
		||||
var templateFS embed.FS
 | 
			
		||||
 | 
			
		||||
func (app *application) addDefaultData(td *templateData, r *http.Request) *templateData {
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (app *application) renderTemplate(
 | 
			
		||||
	w http.ResponseWriter,
 | 
			
		||||
	r *http.Request,
 | 
			
		||||
	page string,
 | 
			
		||||
	td *templateData,
 | 
			
		||||
	partials ...string,
 | 
			
		||||
) error {
 | 
			
		||||
	var t *template.Template
 | 
			
		||||
	var err error
 | 
			
		||||
	templateToRender := fmt.Sprintf("templates/%s.page.tmpl", page)
 | 
			
		||||
 | 
			
		||||
	_, templateInMap := app.templateCache[templateToRender]
 | 
			
		||||
 | 
			
		||||
	if app.config.env == "production" && templateInMap {
 | 
			
		||||
		t = app.templateCache[templateToRender]
 | 
			
		||||
	} else {
 | 
			
		||||
		t, err = app.parseTemplate(partials, page, templateToRender)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			app.errorLog.Println(err)
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if td == nil {
 | 
			
		||||
		td = &templateData{}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	td = app.addDefaultData(td, r)
 | 
			
		||||
 | 
			
		||||
	err = t.Execute(w, td)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		app.errorLog.Println(err)
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (app *application) parseTemplate(
 | 
			
		||||
	partials []string,
 | 
			
		||||
	page, templateToRender string,
 | 
			
		||||
) (*template.Template, error) {
 | 
			
		||||
	var t *template.Template
 | 
			
		||||
	var err error
 | 
			
		||||
 | 
			
		||||
	// build partials
 | 
			
		||||
	if len(partials) > 0 {
 | 
			
		||||
		for i, x := range partials {
 | 
			
		||||
			partials[i] = fmt.Sprintf("tempaltes/%s.partial.tmpl", x)
 | 
			
		||||
		}
 | 
			
		||||
		t, err = template.New(fmt.Sprintf("%s.page.tmpl", page)).
 | 
			
		||||
			Funcs(functions).
 | 
			
		||||
			ParseFS(templateFS, "templates/base.layout.tmpl", strings.Join(partials, ","), templateToRender)
 | 
			
		||||
	} else {
 | 
			
		||||
		t, err = template.New(fmt.Sprintf("%s.page.tmpl", page)).
 | 
			
		||||
			Funcs(functions).
 | 
			
		||||
			ParseFS(templateFS, "templates/base.layout.tmpl", templateToRender)
 | 
			
		||||
	}
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		app.errorLog.Println(err)
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	app.templateCache[templateToRender] = t
 | 
			
		||||
	return t, nil
 | 
			
		||||
}
 | 
			
		||||
@ -8,5 +8,8 @@ import (
 | 
			
		||||
 | 
			
		||||
func (app *application) routes() http.Handler {
 | 
			
		||||
	mux := chi.NewRouter()
 | 
			
		||||
 | 
			
		||||
	mux.Get("/virtual-terminal", app.VirtualTerminal)
 | 
			
		||||
 | 
			
		||||
	return mux
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										3
									
								
								cmd/web/templates/base.layout.tmpl
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								cmd/web/templates/base.layout.tmpl
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,3 @@
 | 
			
		||||
{{define "base"}}
 | 
			
		||||
 | 
			
		||||
{{end}}
 | 
			
		||||
		Reference in New Issue
	
	Block a user