parent
a2adf8ad67
commit
cb537328ff
@ -0,0 +1,36 @@ |
||||
package config |
||||
|
||||
|
||||
import ( |
||||
"log" |
||||
|
||||
"github.com/BurntSushi/toml" |
||||
) |
||||
|
||||
type config struct { |
||||
Admin string `toml:"admin"` |
||||
Views string `toml:"views"` |
||||
Layouts string `toml:"layouts"` |
||||
Port string `toml:"port"` |
||||
|
||||
Database struct { |
||||
Driver string `toml:"driver"` |
||||
Url string `toml:"url"` |
||||
} `toml:"database"` |
||||
} |
||||
|
||||
var Settings config |
||||
|
||||
func Load(path string) { |
||||
metadata, err := toml.DecodeFile(path, &Settings) |
||||
|
||||
if err != nil { |
||||
log.Fatalf("error loading config.toml: %v", err) |
||||
} |
||||
|
||||
bad_keys := metadata.Undecoded() |
||||
|
||||
if len(bad_keys) > 0 { |
||||
log.Fatalf("unknown configuration keys: %v", bad_keys); |
||||
} |
||||
} |
@ -0,0 +1,12 @@ |
||||
package zed |
||||
|
||||
import ( |
||||
"log" |
||||
"fmt" |
||||
) |
||||
|
||||
func Fail(err error, format string, v ...any) error { |
||||
err_format := fmt.Sprintf("ERROR: %v; %s", err, format) |
||||
log.Printf(err_format, v...) |
||||
return err |
||||
} |
@ -0,0 +1,57 @@ |
||||
package zed |
||||
|
||||
import ( |
||||
"log" |
||||
"strings" |
||||
"io/fs" |
||||
"path/filepath" |
||||
"os" |
||||
"github.com/gofiber/fiber/v2" |
||||
"github.com/gofiber/template/html/v2" |
||||
) |
||||
|
||||
func RenderPages(pages_path string, target string, layout string) { |
||||
engine := html.New(pages_path, ".html") |
||||
engine.Load() |
||||
|
||||
err := filepath.WalkDir(pages_path, |
||||
func(path string, d fs.DirEntry, err error) error { |
||||
if !d.IsDir() { |
||||
if err != nil { return Fail(err, "path: %s", path); } |
||||
|
||||
dir := filepath.Dir(path) |
||||
err = os.MkdirAll(dir, 0750) |
||||
if err != nil { |
||||
return Fail(err, "making dir %s", dir); |
||||
} |
||||
|
||||
split_path := strings.Split(path, string(os.PathSeparator))[1:] |
||||
source_name := strings.Join(split_path, "/") // Render wants / even on windows
|
||||
ext := filepath.Ext(source_name) |
||||
template_name, found := strings.CutSuffix(source_name, ext) |
||||
|
||||
if found && ext == ".html" && template_name != layout { |
||||
prefixed_path := append([]string{target}, split_path...) |
||||
|
||||
target_path := filepath.Join(prefixed_path...) |
||||
|
||||
// compare time stamps and skip if not newer
|
||||
|
||||
out, err := os.OpenFile(target_path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) |
||||
if err != nil { return Fail(err, "writing file %s", target_path) } |
||||
|
||||
// generate a data-testid for all pages based on template name
|
||||
page_id := strings.ReplaceAll(template_name, "/", "-") + "-page" |
||||
err = engine.Render(out, template_name, fiber.Map{"PageId": page_id}, layout) |
||||
if err != nil { return Fail(err, "failed to render %s", path) } |
||||
|
||||
log.Printf("RENDER: %s -> %s", template_name, target_path) |
||||
out.Close() |
||||
} |
||||
} |
||||
|
||||
return nil |
||||
}) |
||||
|
||||
if err != nil { log.Fatalf("can't walk content") } |
||||
} |
Loading…
Reference in new issue