-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscalarui.go
More file actions
92 lines (78 loc) · 1.98 KB
/
scalarui.go
File metadata and controls
92 lines (78 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package scalarui
import (
"bytes"
_ "embed"
"encoding/json"
"html/template"
)
// Embed the HTML template
//
//go:embed template.html
var htmlTemplate string
// TemplateData represents the data passed to the HTML template
type TemplateData struct {
Title string
Description string
Favicon string
CustomCSS string
Variables map[string]string
ConfigJSON template.JS
HotReloadURL string
}
// ScalarUI represents a configured Scalar UI instance
type ScalarUI struct {
config *Config
}
// New creates a new ScalarUI instance with the given configuration
func New(config *Config) *ScalarUI {
if config == nil {
config = NewConfig()
}
return &ScalarUI{
config: config,
}
}
// NewWithDefaults creates a new ScalarUI instance with default configuration
func NewWithDefaults() *ScalarUI {
return New(NewConfig())
}
// SetConfig updates the configuration
func (s *ScalarUI) SetConfig(config *Config) {
s.config = config
}
// GetConfig returns the current configuration
func (s *ScalarUI) GetConfig() *Config {
return s.config
}
// Render generates the HTML string with the configured options
func (s *ScalarUI) Render() (string, error) {
return renderTemplate(s.config)
}
// renderTemplate renders the HTML template with the given configuration
func renderTemplate(config *Config) (string, error) {
// Convert config to JSON for JavaScript
configBytes, err := json.MarshalIndent(config, "", " ")
if err != nil {
return "", err
}
// Prepare template data
data := TemplateData{
Title: config.Title,
Description: config.Description,
Favicon: config.Favicon,
CustomCSS: config.CustomCSS,
Variables: config.Variables,
ConfigJSON: template.JS(configBytes),
HotReloadURL: config.HotReloadURL,
}
// Parse and execute template
tmpl, err := template.New("scalar").Parse(htmlTemplate)
if err != nil {
return "", err
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, data); err != nil {
return "", err
}
return buf.String(), nil
}