parent
64af525bf8
commit
49946e7f26
@ -1,52 +0,0 @@ |
||||
root = "." |
||||
testdata_dir = "testdata" |
||||
tmp_dir = "tmp" |
||||
|
||||
[build] |
||||
args_bin = [] |
||||
bin = "webapp" |
||||
cmd = "make build" |
||||
delay = 1000 |
||||
exclude_dir = ["assets", "pages", "static", "views", "public", "tmp", "vendor", "testdata"] |
||||
exclude_file = [] |
||||
exclude_regex = ["_test.go"] |
||||
exclude_unchanged = false |
||||
follow_symlink = false |
||||
full_bin = "" |
||||
include_dir = [] |
||||
include_ext = ["go", "tpl", "tmpl", "html", "css", "js"] |
||||
include_file = [] |
||||
kill_delay = "0s" |
||||
log = "build-errors.log" |
||||
poll = false |
||||
poll_interval = 0 |
||||
post_cmd = [] |
||||
pre_cmd = [] |
||||
rerun = false |
||||
rerun_delay = 500 |
||||
send_interrupt = false |
||||
stop_on_error = false |
||||
|
||||
[color] |
||||
app = "" |
||||
build = "yellow" |
||||
main = "magenta" |
||||
runner = "green" |
||||
watcher = "cyan" |
||||
|
||||
[log] |
||||
main_only = false |
||||
silent = false |
||||
time = false |
||||
|
||||
[misc] |
||||
clean_on_exit = false |
||||
|
||||
[proxy] |
||||
app_port = 7001 |
||||
enabled = true |
||||
proxy_port = 7002 |
||||
|
||||
[screen] |
||||
clear_on_rebuild = false |
||||
keep_scroll = true |
||||
@ -1,70 +1,14 @@ |
||||
package common |
||||
|
||||
import ( |
||||
"log" |
||||
"strings" |
||||
"io/fs" |
||||
"path/filepath" |
||||
"os" |
||||
"github.com/gofiber/fiber/v2" |
||||
"github.com/gofiber/template/html/v2" |
||||
) |
||||
|
||||
func Page(path string) (func(c *fiber.Ctx) error) { |
||||
return func (c *fiber.Ctx) error { |
||||
return c.Render(path, fiber.Map{}) |
||||
} |
||||
} |
||||
|
||||
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...) |
||||
_, err := os.Stat(target_path) |
||||
page_id := strings.ReplaceAll(path, "/", "-") + "-page" |
||||
|
||||
if os.IsNotExist(err) { |
||||
target_dir := filepath.Dir(target_path) |
||||
log.Println("MAKING: ", target_dir) |
||||
os.MkdirAll(target_dir, 0750) |
||||
} |
||||
|
||||
// TODO: 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 func (c *fiber.Ctx) error { |
||||
return c.Render(path, fiber.Map{"PageId": page_id}) |
||||
} |
||||
|
||||
return nil |
||||
}) |
||||
|
||||
if err != nil { log.Fatalf("can't walk content") } |
||||
} |
||||
|
||||
Loading…
Reference in new issue