package servlet import ( "net/http" "strings" ) type ( // TemplateRoute defines a template route in the router. // It contains: // // VinegarWebRoute: The base VinegarWebRoute containing the URL pattern and handler. // // srv: The VinegarHttpServlet instance that this route is attached to. // // fileRoot: The base file path to serve files from for this template route. // // TemplateManager: The TemplateManager instance that manages templates for this route. // // UseCache: Whether to use caching for this template route. TemplateRoute struct { *VinegarWebRoute srv *VinegarWebServlet fileRoot string TemplateManager *TemplateManager UseCache bool } TemplateRouteHandlerFunc func(w http.ResponseWriter, r *http.Request, tm *TemplateManager) ) // NewTemplateRoute creates and configures a new TemplateRoute. // // It accepts the following parameters: // // servlet - The VinegarWebServlet instance to attach the route to // // urlPattern - The URL regex pattern that the route will match // // templatePath - Filepath to the template files // // componentPath - Filepath to template component files // // handler - The handler function to call for this route // // It does the following: // // - Creates a TemplateManager instance using templatePath and componentPath // // - Generates a default route pattern by removing wildcards from urlPattern // // - Creates a VinegarWebRoute using the default pattern and a handler // that calls the provided handler function // // - Creates a new TemplateRoute instance configured with the VinegarWebRoute, // servlet, TemplateManager, and other settings // // - Returns a pointer to the TemplateRoute func NewTemplateRoute(servlet *VinegarWebServlet, urlPattern string, templatePath string, componentPath string, handler TemplateRouteHandlerFunc) *TemplateRoute { defaultPrune := strings.Replace(urlPattern, ".*", "", -1) tm := NewTemplateManager(templatePath, componentPath) rootRoute := NewServletRoute(defaultPrune, createTemplateRouteFunction(tm, handler)) route := TemplateRoute{ VinegarWebRoute: rootRoute, srv: servlet, fileRoot: "", TemplateManager: tm, UseCache: false, } return &route } func createTemplateRouteFunction(tm *TemplateManager, handler TemplateRouteHandlerFunc) VinegarHandlerFunction { fun := func(w http.ResponseWriter, r *http.Request) { handler(w, r, tm) } return fun }