(transaction *sqlx.Tx, cruds map[string]dbresourceinterface.DbResourceInterface, hostSwitch HostRouterProvider, olricDb *olric.EmbeddedClient)
| 88 | } |
| 89 | |
| 90 | func 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 | |
| 123 | func 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) { |
no test coverage detected