This commit is contained in:
sujit
2025-08-05 09:13:52 +05:45
parent f40518c53e
commit 0089bb828d
2 changed files with 46 additions and 46 deletions

View File

@@ -5,55 +5,10 @@ import (
"net/http"
"os"
"strings"
"sync"
"github.com/oarkflow/jsonschema"
"github.com/oarkflow/mq/renderer"
)
type RequestSchemaTemplate struct {
Schema *jsonschema.Schema `json:"schema"`
Renderer *renderer.JSONSchemaRenderer `json:"template"`
}
var cache = make(map[string]*RequestSchemaTemplate)
var mu = &sync.Mutex{}
func getCachedRenderer(schemaPath, template string) (*renderer.JSONSchemaRenderer, error) {
mu.Lock()
defer mu.Unlock()
path := fmt.Sprintf("%s:%s", schemaPath, template)
if cached, exists := cache[path]; exists {
return cached.Renderer, nil
}
schemaContent, err := os.ReadFile(schemaPath)
if err != nil {
return nil, fmt.Errorf("error reading schema file: %w", err)
}
compiler := jsonschema.NewCompiler()
schema, err := compiler.Compile(schemaContent)
if err != nil {
return nil, fmt.Errorf("error compiling schema: %w", err)
}
templatePath := fmt.Sprintf("templates/%s.html", template)
htmlLayout, err := os.ReadFile(templatePath)
if err != nil {
return nil, fmt.Errorf("failed to load template: %w", err)
}
renderer := renderer.NewJSONSchemaRenderer(schema, string(htmlLayout))
cachedTemplate := &RequestSchemaTemplate{
Schema: schema,
Renderer: renderer,
}
cache[path] = cachedTemplate
return cachedTemplate.Renderer, nil
}
func main() {
http.Handle("/form.css", http.FileServer(http.Dir("templates")))
http.HandleFunc("/render", func(w http.ResponseWriter, r *http.Request) {
@@ -69,7 +24,7 @@ func main() {
if !strings.Contains(schemaHTML, ".json") {
schemaHTML = fmt.Sprintf("%s.json", schemaHTML)
}
renderer, err := getCachedRenderer(schemaHTML, templateName)
renderer, err := renderer.Get(schemaHTML, templateName)
if err != nil {
http.Error(w, fmt.Sprintf("Failed to get cached template: %v", err), http.StatusInternalServerError)
return

View File

@@ -4,8 +4,10 @@ import (
"bytes"
"fmt"
"html/template"
"os"
"sort"
"strings"
"sync"
"github.com/oarkflow/jsonschema"
)
@@ -827,3 +829,46 @@ func renderButtonFromConfig(config map[string]interface{}, defaultType string) s
return fmt.Sprintf(`<button %s>%s</button>`,
strings.Join(attributes, " "), content)
}
type RequestSchemaTemplate struct {
Schema *jsonschema.Schema `json:"schema"`
Renderer *JSONSchemaRenderer `json:"template"`
}
var cache = make(map[string]*RequestSchemaTemplate)
var mu = &sync.Mutex{}
func Get(schemaPath, template string) (*JSONSchemaRenderer, error) {
mu.Lock()
defer mu.Unlock()
path := fmt.Sprintf("%s:%s", schemaPath, template)
if cached, exists := cache[path]; exists {
return cached.Renderer, nil
}
schemaContent, err := os.ReadFile(schemaPath)
if err != nil {
return nil, fmt.Errorf("error reading schema file: %w", err)
}
compiler := jsonschema.NewCompiler()
schema, err := compiler.Compile(schemaContent)
if err != nil {
return nil, fmt.Errorf("error compiling schema: %w", err)
}
templatePath := fmt.Sprintf("templates/%s.html", template)
htmlLayout, err := os.ReadFile(templatePath)
if err != nil {
return nil, fmt.Errorf("failed to load template: %w", err)
}
renderer := NewJSONSchemaRenderer(schema, string(htmlLayout))
cachedTemplate := &RequestSchemaTemplate{
Schema: schema,
Renderer: renderer,
}
cache[path] = cachedTemplate
return cachedTemplate.Renderer, nil
}