summaryrefslogblamecommitdiffstats
path: root/cmd/web/handlers.go
blob: 4b76103d831fe719259d41dd3d424f86ca60a469 (plain) (tree)
1
2
3
4
5
6
7


            
             
                  

                       





                                                         






                                                                                        
                                                                                   



                                                                                                      



                                                                                  
                                                   













                                                                          





                                                                      

                                           
                                                 
                                                   













                                                                          
 
package main

import (
	"log"
	"net/http"
	"runtime/debug"
	"text/template"
)

func (app *application) notFound(w http.ResponseWriter) {
	http.Error(w, http.StatusText(401), 401)
}

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.Error(err.Error(), "method", method, "uri", uri, "trace", trace)
	log.Printf("Crash! %s %s %s", method, uri, trace)
	http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}

func (app *application) listOrganisation(w http.ResponseWriter, r *http.Request) {
	files := []string{
		"./ui/html/base.tmpl.html",
		"./ui/html/pages/organisations/list.tmpl.html",
		"./ui/html/partials/nav.tmpl.html",
	}

	ts, err := template.ParseFiles(files...)
	if err != nil {
		app.serverError(w, r, err) // Use the serverError() helper
		return
	}

	err = ts.ExecuteTemplate(w, "base", nil)
	if err != nil {
		app.serverError(w, r, err) // Use the serverError() helper
	}
}

func (app *application) home(w http.ResponseWriter, r *http.Request) {
	if r.URL.Path != "/" {
		app.notFound(w)
		return
	}

	files := []string{
		"./ui/html/base.tmpl.html",
		"./ui/html/pages/home.tmpl.html",
		"./ui/html/partials/nav.tmpl.html",
	}

	ts, err := template.ParseFiles(files...)
	if err != nil {
		app.serverError(w, r, err) // Use the serverError() helper
		return
	}

	err = ts.ExecuteTemplate(w, "base", nil)
	if err != nil {
		app.serverError(w, r, err) // Use the serverError() helper
	}

	// w.Write([]byte("Hello from DED"))
}