implemented some template route stuff
This commit is contained in:
parent
8f29442ff4
commit
97a50a6a34
@ -1,13 +1,13 @@
|
|||||||
package servlet
|
package servlet
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"geniuscartel.xyz/vinegar/vinegarUtil"
|
"geniuscartel.xyz/vinegar/vinegarUtil"
|
||||||
"html/template"
|
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -26,6 +26,7 @@ type (
|
|||||||
VinegarServlet struct {
|
VinegarServlet struct {
|
||||||
Port string
|
Port string
|
||||||
Routes []*VinegarRoute
|
Routes []*VinegarRoute
|
||||||
|
ErrorRoutes map[int]*TemplateRoute
|
||||||
}
|
}
|
||||||
|
|
||||||
VinegarRoute struct {
|
VinegarRoute struct {
|
||||||
@ -38,7 +39,8 @@ type (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func NewServlet(port string) *VinegarServlet {
|
func NewServlet(port string) *VinegarServlet {
|
||||||
srv := VinegarServlet{Port: port}
|
errors := make(map[int]*TemplateRoute)
|
||||||
|
srv := VinegarServlet{Port: port, ErrorRoutes: errors}
|
||||||
|
|
||||||
return &srv
|
return &srv
|
||||||
}
|
}
|
||||||
@ -56,14 +58,21 @@ func (s *VinegarServlet) AddRoute(route *VinegarRoute) {
|
|||||||
s.Routes = append(s.Routes, route)
|
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) {
|
func (s *VinegarServlet) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||||
path := req.URL.Path
|
path := req.URL.Path
|
||||||
for _, route := range s.Routes {
|
for _, route := range s.Routes {
|
||||||
if route.Pattern.MatchString(path) {
|
if route.Pattern.MatchString(path) {
|
||||||
|
fmt.Printf("SERVING: [%s]=>{%s}\n", path, route.Pattern.String())
|
||||||
route.Handler(w, req)
|
route.Handler(w, req)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
s.SendError(w, 404, "Couldn't find your content.")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *VinegarServlet) Start() {
|
func (s *VinegarServlet) Start() {
|
||||||
@ -86,30 +95,17 @@ func (r *VinegarRoute) Announce() {
|
|||||||
log.Printf("Added route for [%s]\n", r.Pattern.String())
|
log.Printf("Added route for [%s]\n", r.Pattern.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
func SendError(w http.ResponseWriter, code int, msg string) {
|
func (s *VinegarServlet) SendError(w http.ResponseWriter, code int, msg string) {
|
||||||
errorFile := "templates/error.html"
|
fmt.Printf("Reached SendError. Rendering template for code %d with message: %s\n", code, msg)
|
||||||
errorResp := ErrorResponse{code, msg}
|
tmpl, exists := s.ErrorRoutes[code]
|
||||||
tmplManager := template.New("error")
|
if exists {
|
||||||
|
tmpl.TemplateManager.AddMixin("code", strconv.Itoa(code))
|
||||||
f, err := os.OpenFile(errorFile, os.O_RDONLY, 0777)
|
tmpl.TemplateManager.AddMixin("msg", msg)
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
}
|
||||||
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)
|
w.WriteHeader(code)
|
||||||
err = tmpl.Execute(w, errorResp)
|
_, err := w.Write([]byte(tmpl.TemplateManager.RenderTemplate(fmt.Sprintf("%d.html", code))))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@ -71,7 +71,7 @@ func createSingleFileServletFunction(route *FileRoute) VinegarHandlerFunction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !exists {
|
if !exists {
|
||||||
SendError(w, 404, "File not found.")
|
route.srv.SendError(w, 404, "File not found.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,7 +112,7 @@ func createCompressibleFileServletFunction(route *FileRoute, basePattern string,
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
SendError(w, 404, "Oops! Something went wrong.")
|
route.srv.SendError(w, 404, "Couldn't find your content.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -152,7 +152,7 @@ func createUncompressedFileServletFunction(route *FileRoute, basePattern string,
|
|||||||
return
|
return
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
SendError(w, 404, "Oops! Something went wrong.")
|
route.srv.SendError(w, 404, "Couldn't find your content.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return fun
|
return fun
|
||||||
|
|||||||
@ -2,7 +2,6 @@ package servlet
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
util "geniuscartel.xyz/vinegar/vinegarUtil"
|
util "geniuscartel.xyz/vinegar/vinegarUtil"
|
||||||
"html/template"
|
"html/template"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
@ -26,7 +25,8 @@ type (
|
|||||||
func NewTemplateManager(templatePath string, componentPath string) *TemplateManager {
|
func NewTemplateManager(templatePath string, componentPath string) *TemplateManager {
|
||||||
templates := make(map[string]*template.Template)
|
templates := make(map[string]*template.Template)
|
||||||
components := make(map[string]template.HTML)
|
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()
|
tm.loadAssets()
|
||||||
return &tm
|
return &tm
|
||||||
}
|
}
|
||||||
@ -75,7 +75,6 @@ func (tm *TemplateManager) RenderTemplate(key string) string {
|
|||||||
if !exists {
|
if !exists {
|
||||||
panic("Could not load template for key: " + key)
|
panic("Could not load template for key: " + key)
|
||||||
}
|
}
|
||||||
fmt.Println()
|
|
||||||
buff := strings.Builder{}
|
buff := strings.Builder{}
|
||||||
err := tmpl.Execute(&buff, tm)
|
err := tmpl.Execute(&buff, tm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user