diff --git a/servlet/server.go b/servlet/server.go index b956b08..7411ed4 100644 --- a/servlet/server.go +++ b/servlet/server.go @@ -1,13 +1,13 @@ package servlet import ( + "fmt" "geniuscartel.xyz/vinegar/vinegarUtil" - "html/template" - "io/ioutil" "log" "net/http" "os" "regexp" + "strconv" ) const ( @@ -24,8 +24,9 @@ type ErrorResponse struct { type ( VinegarServlet struct { - Port string - Routes []*VinegarRoute + Port string + Routes []*VinegarRoute + ErrorRoutes map[int]*TemplateRoute } VinegarRoute struct { @@ -38,7 +39,8 @@ type ( ) func NewServlet(port string) *VinegarServlet { - srv := VinegarServlet{Port: port} + errors := make(map[int]*TemplateRoute) + srv := VinegarServlet{Port: port, ErrorRoutes: errors} return &srv } @@ -56,14 +58,21 @@ func (s *VinegarServlet) AddRoute(route *VinegarRoute) { s.Routes = append(s.Routes, route) } +func (s *VinegarServlet) AddErrorRoute(code int, route *TemplateRoute) { + route.Announce() + s.ErrorRoutes[code] = route +} + func (s *VinegarServlet) ServeHTTP(w http.ResponseWriter, req *http.Request) { path := req.URL.Path for _, route := range s.Routes { if route.Pattern.MatchString(path) { + fmt.Printf("SERVING: [%s]=>{%s}\n", path, route.Pattern.String()) route.Handler(w, req) return } } + s.SendError(w, 404, "Couldn't find your content.") } func (s *VinegarServlet) Start() { @@ -86,30 +95,17 @@ func (r *VinegarRoute) Announce() { log.Printf("Added route for [%s]\n", r.Pattern.String()) } -func SendError(w http.ResponseWriter, code int, msg string) { - errorFile := "templates/error.html" - errorResp := ErrorResponse{code, msg} - tmplManager := template.New("error") - - f, err := os.OpenFile(errorFile, os.O_RDONLY, 0777) - if err != nil { - panic(err) +func (s *VinegarServlet) SendError(w http.ResponseWriter, code int, msg string) { + fmt.Printf("Reached SendError. Rendering template for code %d with message: %s\n", code, msg) + tmpl, exists := s.ErrorRoutes[code] + if exists { + tmpl.TemplateManager.AddMixin("code", strconv.Itoa(code)) + tmpl.TemplateManager.AddMixin("msg", msg) } - defer f.Close() - - content, err := ioutil.ReadAll(f) - if err != nil { - panic(err) - } - - tmpl, err := tmplManager.Parse(string(content)) - if err != nil { - panic(err) - } - w.WriteHeader(code) - err = tmpl.Execute(w, errorResp) + _, err := w.Write([]byte(tmpl.TemplateManager.RenderTemplate(fmt.Sprintf("%d.html", code)))) if err != nil { panic(err) } + return } diff --git a/servlet/staticRoute.go b/servlet/staticRoute.go index b87bb50..5a1adcb 100644 --- a/servlet/staticRoute.go +++ b/servlet/staticRoute.go @@ -71,7 +71,7 @@ func createSingleFileServletFunction(route *FileRoute) VinegarHandlerFunction { } if !exists { - SendError(w, 404, "File not found.") + route.srv.SendError(w, 404, "File not found.") return } @@ -112,7 +112,7 @@ func createCompressibleFileServletFunction(route *FileRoute, basePattern string, return } } else { - SendError(w, 404, "Oops! Something went wrong.") + route.srv.SendError(w, 404, "Couldn't find your content.") return } } @@ -152,7 +152,7 @@ func createUncompressedFileServletFunction(route *FileRoute, basePattern string, return } else { - SendError(w, 404, "Oops! Something went wrong.") + route.srv.SendError(w, 404, "Couldn't find your content.") } } return fun diff --git a/servlet/templates.go b/servlet/templates.go index fcd9408..77f9af1 100644 --- a/servlet/templates.go +++ b/servlet/templates.go @@ -2,7 +2,6 @@ package servlet import ( "errors" - "fmt" util "geniuscartel.xyz/vinegar/vinegarUtil" "html/template" "io/ioutil" @@ -26,7 +25,8 @@ type ( func NewTemplateManager(templatePath string, componentPath string) *TemplateManager { templates := make(map[string]*template.Template) components := make(map[string]template.HTML) - tm := TemplateManager{templatePath: templatePath, componentPath: componentPath, templates: templates, components: components} + mixins := make(map[string]string) + tm := TemplateManager{templatePath: templatePath, componentPath: componentPath, templates: templates, components: components, mixins: mixins} tm.loadAssets() return &tm } @@ -75,7 +75,6 @@ func (tm *TemplateManager) RenderTemplate(key string) string { if !exists { panic("Could not load template for key: " + key) } - fmt.Println() buff := strings.Builder{} err := tmpl.Execute(&buff, tm) if err != nil {