MCPcopy
hub / github.com/daptin/daptin / CreateTemplateHooks

Function CreateTemplateHooks

server/subsite/template_handler.go:90–121  ·  view source on GitHub ↗
(transaction *sqlx.Tx, cruds map[string]dbresourceinterface.DbResourceInterface, hostSwitch HostRouterProvider, olricDb *olric.EmbeddedClient)

Source from the content-addressed store, hash-verified

88}
89
90func CreateTemplateHooks(transaction *sqlx.Tx, cruds map[string]dbresourceinterface.DbResourceInterface, hostSwitch HostRouterProvider, olricDb *olric.EmbeddedClient) error {
91 allRouters := hostSwitch.GetAllRouter()
92 templateList, err := cruds["template"].GetAllObjects("template", transaction)
93 log.Infof("Got [%d] Templates from database", len(templateList))
94 if err != nil {
95 return err
96 }
97 if fileCache == nil {
98 fileCache, err = cache.NewFileCache(olricDb, "template-cache")
99 CheckErr(err, "Failed to create olric template cache")
100 }
101
102 handlerCreator := CreateTemplateRouteHandler(cruds, transaction)
103 for _, templateRow := range templateList {
104 log.Infof("ProcessTemplateRoute [%s] %v", templateRow["name"], templateRow["url_pattern"])
105 urlPattern := templateRow["url_pattern"].(string)
106 strArray := make([]string, 0)
107 err = json.Unmarshal([]byte(urlPattern), &strArray)
108 if err != nil {
109 log.Errorf("Failed to parse url pattern [%v] as string array [%v]", urlPattern, err)
110 continue
111 }
112 templateRenderHelper := handlerCreator(templateRow)
113 for _, urlMatch := range strArray {
114 log.Infof("TemplateRoute [%s] => %s", urlMatch, templateRow["name"])
115 for _, router := range allRouters {
116 router.Any(urlMatch, templateRenderHelper)
117 }
118 }
119 }
120 return nil
121}
122
123func CreateTemplateRouteHandler(cruds map[string]dbresourceinterface.DbResourceInterface, transaction *sqlx.Tx) func(template map[string]interface{}) func(ginContext *gin.Context) {
124 return func(templateInstance map[string]interface{}) func(ginContext *gin.Context) {

Callers 1

MainFunction · 0.92

Calls 6

NewFileCacheFunction · 0.92
makeFunction · 0.85
CheckErrFunction · 0.70
GetAllRouterMethod · 0.65
GetAllObjectsMethod · 0.65

Tested by

no test coverage detected