Skip to content

Commit

Permalink
embed
Browse files Browse the repository at this point in the history
  • Loading branch information
chappjc committed Aug 12, 2022
1 parent 4b009ce commit d86dd40
Show file tree
Hide file tree
Showing 8 changed files with 276 additions and 309 deletions.
5 changes: 3 additions & 2 deletions client/cmd/dexc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ func defaultHostByNetwork(network dex.Network) string {
}
}

// Config is the configuration for the DEX client application.
// Config is the configuration for the DEX client application. TODO: Replace
// ReloadHTML with NoEmbeddedHTML.
type Config struct {
AppData string `long:"appdata" description:"Path to application directory."`
Config string `long:"config" description:"Path to an INI configuration file."`
Expand All @@ -89,7 +90,7 @@ type Config struct {
NoWeb bool `long:"noweb" description:"disable the web server."`
Testnet bool `long:"testnet" description:"use testnet"`
Simnet bool `long:"simnet" description:"use simnet"`
ReloadHTML bool `long:"reload-html" description:"Reload the webserver's page template with every request. For development purposes."`
ReloadHTML bool `long:"reload-html" description:"Reload the webserver's page template from disk with every request. Prevents use of any embedded UI files. For development purposes."`
DebugLevel string `long:"log" description:"Logging level {trace, debug, info, warn, error, critical}"`
LocalLogs bool `long:"loglocal" description:"Use local time zone time stamps in log entries."`
CPUProfile string `long:"cpuprofile" description:"File for CPU profiling."`
Expand Down
2 changes: 1 addition & 1 deletion client/cmd/dexc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func mainCore() error {
Addr: cfg.WebAddr,
CustomSiteDir: cfg.SiteDir,
Logger: logMaker.Logger("WEB"),
ReloadHTML: cfg.ReloadHTML,
NoEmbed: cfg.ReloadHTML,
HttpProf: cfg.HTTPProfile,
Language: cfg.Language,
})
Expand Down
10 changes: 5 additions & 5 deletions client/webserver/live_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1801,11 +1801,11 @@ func TestServer(t *testing.T) {
}

s, err := New(&Config{
Core: tCore,
Addr: "[::1]:54321",
Logger: logger,
ReloadHTML: true,
HttpProf: true,
Core: tCore,
Addr: "[::1]:54321",
Logger: logger,
NoEmbed: true, // use files on disk, and reload on each page load
HttpProf: true,
})
if err != nil {
t.Fatalf("error creating server: %v", err)
Expand Down
1 change: 1 addition & 0 deletions client/webserver/site/dist/goembed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
placeholder
104 changes: 43 additions & 61 deletions client/webserver/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,31 @@ type templates struct {
templates map[string]pageTemplate
folder string
locale map[string]string
exec func(string, interface{}) (string, error)
embedded bool // used in exec and addTemplate
addErr error
}

// newTemplates constructs a new templates.
func newTemplates(folder, lang string, reload bool) *templates {
func newTemplates(folder, lang string, embedded bool) *templates {
t := &templates{
templates: make(map[string]pageTemplate),
folder: folder,
embedded: embedded,
}
t.exec = t.execTemplateToString
if reload {
var found bool
// Make sure we have the expected directory structure.
if !folderExists(t.srcDir()) {
t.addErr = fmt.Errorf("reload-html set but source directory not found at %s", t.srcDir())
} else if t.locale, found = locales.Locales[lang]; !found {
t.addErr = fmt.Errorf("no translation dictionary found for lang %q", lang)
}
t.exec = t.execWithReload
var found bool
if t.locale, found = locales.Locales[lang]; !found {
t.addErr = fmt.Errorf("no translation dictionary found for lang %q", lang)
return t
}
if embedded {
return t
}

// Make sure we have the expected directory structure.
if !folderExists(t.srcDir()) {
t.addErr = fmt.Errorf("reload-html set but source directory not found at %s", t.srcDir())
}

return t
}

Expand All @@ -68,7 +72,7 @@ func (t *templates) srcPath(name string) string {
return filepath.Join(t.srcDir(), name+".tmpl")
}

// retranslate rebuilds the locallized html template. Only used in development
// retranslate rebuilds the localized html template. Only used in development
// when the --reload-html flag is used.
func (t *templates) retranslate(name string, preloads ...string) error {
for _, iName := range append(preloads, name) {
Expand Down Expand Up @@ -107,7 +111,7 @@ func (t *templates) retranslate(name string, preloads ...string) error {
// must also be base names of files in the template folder, which will be loaded
// in order before the name template is processed. addTemplate returns itself
// and defers errors to facilitate chaining.
func (t *templates) addTemplate(name string, isEmbed bool, preloads ...string) *templates {
func (t *templates) addTemplate(name string, preloads ...string) *templates {
if t.addErr != nil {
return t
}
Expand All @@ -117,20 +121,20 @@ func (t *templates) addTemplate(name string, isEmbed bool, preloads ...string) *
}
files = append(files, t.filepath(name))

var temp *template.Template
tmpl := template.New(name).Funcs(templateFuncs)
var err error
if isEmbed {
temp, err = template.New(name).Funcs(templateFuncs).ParseFS(siteRes, files...)
if t.embedded {
tmpl, err = tmpl.ParseFS(siteRes, files...)
} else {
temp, err = template.New(name).Funcs(templateFuncs).ParseFiles(files...)
tmpl, err = tmpl.ParseFiles(files...)
}
if err != nil {
t.addErr = fmt.Errorf("error adding template %s: %w", name, err)
return t
}
t.templates[name] = pageTemplate{
preloads: preloads,
template: temp,
template: tmpl,
}
return t
}
Expand All @@ -143,57 +147,35 @@ func (t *templates) buildErr() error {
return err
}

// reloadTemplates relaods all templates. Use this during front-end development
// when editing templates.
func (t *templates) reloadTemplates() error {
var errorStrings []string
for name, tmpl := range t.templates {
// as this is used for development we do not use embed website.
t.addTemplate(name, false, tmpl.preloads...)
if t.buildErr() != nil {
log.Errorf(t.buildErr().Error())
}
}
if errorStrings == nil {
return nil
}
return fmt.Errorf(strings.Join(errorStrings, " | "))
}

// execTemplateToString executes the specified input template using the
// supplied data, and writes the result into a string. If the template fails to
// execute or isn't found, a non-nil error will be returned. Check it before
// writing to the client, otherwise you might as well execute directly into
// your response writer instead of the internal buffer of this function.
// exec executes the specified input template using the supplied data, and
// writes the result into a string. If the template fails to execute or isn't
// found, a non-nil error will be returned. Check it before writing to the
// client, otherwise you might as well execute directly into your response
// writer instead of the internal buffer of this function.
//
// The template will be reloaded if using on-disk (not embedded) templates.
//
// DRAFT NOTE: Might consider writing directly to the the buffer here. Could
// still set the error code appropriately.
func (t *templates) execTemplateToString(name string, data interface{}) (string, error) {
temp, ok := t.templates[name]
if !ok {
return "", fmt.Errorf("Template %s not known", name)
}
var page strings.Builder
err := temp.template.ExecuteTemplate(&page, name, data)
return page.String(), err
}

// execWithReload is the same as execTemplateToString, but will reload the
// template first.
func (t *templates) execWithReload(name string, data interface{}) (string, error) {
func (t *templates) exec(name string, data interface{}) (string, error) {
tmpl, found := t.templates[name]
if !found {
return "", fmt.Errorf("template %s not found", name)
return "", fmt.Errorf("template %q not found", name)
}

err := t.retranslate(name, tmpl.preloads...)
if err != nil {
return "", err
if !t.embedded {
err := t.retranslate(name, tmpl.preloads...)
if err != nil {
return "", err
}

t.addTemplate(name, tmpl.preloads...)
log.Debugf("reloaded HTML template %q", name)
}

t.addTemplate(name, false, tmpl.preloads...)
log.Debugf("reloaded HTML template %q", name)
return t.execTemplateToString(name, data)
var page strings.Builder
err := tmpl.template.ExecuteTemplate(&page, name, data)
return page.String(), err
}

// templateFuncs are able to be called during template execution.
Expand Down
Loading

0 comments on commit d86dd40

Please sign in to comment.