package main import ( "encoding/json" "fmt" "log" "net/http" "os" "github.com/oarkflow/jsonschema" "github.com/oarkflow/mq/renderer" ) func main() { // Setup routes http.HandleFunc("/", indexHandler) http.HandleFunc("/comprehensive", func(w http.ResponseWriter, r *http.Request) { renderFormHandler(w, r, "comprehensive-validation.json", "Comprehensive Validation Demo") }) http.HandleFunc("/features", func(w http.ResponseWriter, r *http.Request) { renderFormHandler(w, r, "validation-features.json", "Validation Features Demo") }) http.HandleFunc("/complex", func(w http.ResponseWriter, r *http.Request) { renderFormHandler(w, r, "complex.json", "Complex Form Demo") }) fmt.Println("Server starting on :8080") fmt.Println("Available examples:") fmt.Println(" - http://localhost:8080/comprehensive - Comprehensive validation demo") fmt.Println(" - http://localhost:8080/features - Validation features demo") fmt.Println(" - http://localhost:8080/complex - Complex form demo") log.Fatal(http.ListenAndServe(":8080", nil)) } func renderFormHandler(w http.ResponseWriter, r *http.Request, schemaFile, title string) { // Load and compile schema schemaData, err := os.ReadFile(schemaFile) if err != nil { http.Error(w, fmt.Sprintf("Error reading schema file: %v", err), http.StatusInternalServerError) return } // Parse JSON schema var schemaMap map[string]interface{} if err := json.Unmarshal(schemaData, &schemaMap); err != nil { http.Error(w, fmt.Sprintf("Error parsing JSON schema: %v", err), http.StatusInternalServerError) return } // Compile schema compiler := jsonschema.NewCompiler() schema, err := compiler.Compile(schemaData) if err != nil { http.Error(w, fmt.Sprintf("Error compiling schema: %v", err), http.StatusInternalServerError) return } // Create renderer with basic template basicTemplate := `
{{.FieldsHTML}}
{{.ButtonsHTML}}
` renderer := renderer.NewJSONSchemaRenderer(schema, basicTemplate) // Render form with empty data html, err := renderer.RenderFields(map[string]any{}) if err != nil { http.Error(w, fmt.Sprintf("Error rendering form: %v", err), http.StatusInternalServerError) return } // Create complete HTML page fullHTML := createFullHTMLPage(title, html) w.Header().Set("Content-Type", "text/html") w.Write([]byte(fullHTML)) } func indexHandler(w http.ResponseWriter, r *http.Request) { html := "" + "" + "" + "JSON Schema Form Builder - Validation Examples" + "" + "" + "" + "

JSON Schema Form Builder - Validation Examples

" + "

This demo showcases comprehensive JSON Schema 2020-12 validation support with:

" + "" + "

Available Examples:

" + "" + "" + "" w.Header().Set("Content-Type", "text/html") w.Write([]byte(html)) } func createFullHTMLPage(title, formHTML string) string { template := "" + "" + "" + "" + title + "" + "" + "" + "" + "" + "" + "" + "
" + "← Back to Examples" + "
" + "

" + title + "

" + "

This form demonstrates comprehensive JSON Schema validation.

" + formHTML + "
" + "" + "" return template }